Kafka Streams - Hopping windows - deduplicate keys
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Kafka Streams is a client library for building applications and microservices, where the input and output data are stored in Kafka topics. Kafka Streams combines the simplicity of writing and deploying standard Java and Scala applications on the client side with the benefits of Kafka's server-side cluster technology. One of the powerful features of Kafka Streams is its ability to work with windowing, specifically 'Hopping Windows' for scenarios where it's important to deduplicate records with the same key.
Understanding Hopping Windows
Hopping Windows are a type of windowing mechanism that allow users to control how to group records with the same key that arrive within a defined period. These windows are defined by two parameters:
- Window Size: The duration of the window for which the records are aggregated.
- Advance Interval (Hop): The interval at which the window progresses over the stream.
Unlike tumbling windows, which are non-overlapping, hopping windows can overlap if the advance interval is smaller than the window size. This characteristic makes them particularly useful for analysis that requires overlapping intervals.
How Deduplication Works Within Hopping Windows
Key deduplication in hopping windows refers to the process where only unique records for each key are maintained during the life span of a window. This is crucial for use cases where only the latest state for a given key is required, and duplicate entries (entries with same key and window) need to be removed.
In Kafka Streams, this can typically be achieved using a combination of windowing techniques and state stores. A common approach might involve:
- Filtering incoming data streams to discard duplicates which can be done by employing a
TransformerorProcessorAPI. - Storing the latest record for each key in a persistent store.
- Emitting the deduplicated data downstream.
Deduplication Example Using Kafka Streams:
Below is an example Kafka Streams application in Java that demonstrates deduplication with hopping windows:
In this example, records are grouped by key and windowed using hopping windows. The reduce method ensures that only the latest value for each key is kept by always selecting the newValue when a key collision happens.
Table Summarizing Key Concepts of Hopping Windows and Deduplication
| Concept | Description |
| Window Size | Defines the length of each window period. Records within this period are grouped together. |
| Advance Interval | Determines how frequently a new window begins. |
| Overlapping Windows | Windows can overlap if the advance interval is smaller than the window size. |
| Deduplication | Ensures that within a given window, only unique records per key are maintained. |
| Use Case | Useful in scenarios where a sliding look at the data is needed, with updates on recent happenings. |
Conclusion
Kafka Streams’ support for hopping windows offers powerful capabilities for time-sensitive data processing, allowing developers to manage data streams more effectively. With the added ability to deduplicate records based on keys, it provides a robust solution for applications where maintaining the most recent state is crucial.

