Kafka Streams
Hopping Windows
Deduplication
Data Processing
Key Management

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.

Practice system design

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:

  1. Filtering incoming data streams to discard duplicates which can be done by employing a Transformer or Processor API.
  2. Storing the latest record for each key in a persistent store.
  3. 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:

java
1import org.apache.kafka.common.serialization.Serdes;
2import org.apache.kafka.streams.KafkaStreams;
3import org.apache.kafka.streams.StreamsBuilder;
4import org.apache.kafka.streams.kstream.*;
5
6import java.time.Duration;
7
8public class DeduplicationExample {
9    public static void main(String[] args) {
10        StreamsBuilder builder = new StreamsBuilder();
11        KStream<String, String> source = builder.stream("source-topic");
12
13        Duration windowSize = Duration.ofMinutes(5);
14        Duration advanceInterval = Duration.ofMinutes(1);
15
16        TimeWindows windows = TimeWindows.of(windowSize).advanceBy(advanceInterval);
17
18        source
19            .groupByKey(Grouped.with(Serdes.String(), Serdes.String()))
20            .windowedBy(windows)
21            .reduce((aggValue, newValue) -> newValue, Materialized.as("deduplicated-store"))
22            .toStream()
23            .map((windowedKey, value) -> new KeyValue<>(windowedKey.key(), value))
24            .to("output-topic", Produced.with(Serdes.String(), Serdes.String()));
25
26        KafkaStreams streams = new KafkaStreams(builder.build(), new StreamsConfig(getProperties()));
27        streams.start();
28    }
29}

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

ConceptDescription
Window SizeDefines the length of each window period. Records within this period are grouped together.
Advance IntervalDetermines how frequently a new window begins.
Overlapping WindowsWindows can overlap if the advance interval is smaller than the window size.
DeduplicationEnsures that within a given window, only unique records per key are maintained.
Use CaseUseful 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.