Kafka Streams - Hopping windows - deduplicate keys
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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.
Related reading
- kafka streams - how to set a new key for KTable
- Kafka Streams - Is it possible to run remote interactive queries without a local Kafka Streams instance
- kafka streams - joining partitioned topics
- Kafka streams - joining two ktables invokes join function twice
- Kafka streams - KSQL - Split messages and publish to another topic
- Kafka Streams - KTable from topic with retention policy
- Kafka Streams - missing source topic
- Kafka Streams - Processor API - Forward to different topics

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.