Kafka Streams
Watermarks
Data Processing
Stream Processing
Computing Techniques

How does kafka streams compute watermarks?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka Streams is a client library for building applications and microservices where the input and output data are stored in Kafka clusters. It enables you to do stateful and stateless processing on real-time data. One of the core concepts in stream processing is time, and particularly, event time, which is crucial for handling out-of-order data or late-arriving records. In this context, watermarks play an essential role in how Kafka Streams handles event time processing.

Understanding Watermarks

In stream processing, a watermark is a metadata entity that tracks the progress of event-time processing. It represents a point in time in event streams such that no records with a timestamp older than the watermark should be expected to arrive. Essentially, watermarks allow applications to handle late-arriving data by providing a mechanism to signal when it's safe to process records up to a certain point in time.

Kafka Streams and Event Time Processing

Unlike some other stream processing frameworks, Kafka Streams does not natively support watermarks or event-time processing out-of-the-box in the same way frameworks like Apache Flink or Google Cloud Dataflow do. However, you can implement custom event-time processing in Kafka Streams through punctuators and timestamp extractors, which you manage manually.

Implementing Watermarks in Kafka Streams

Given that Kafka Streams does not manage watermarks automatically, you are required to handle this mechanism yourself if your application demands event-time processing. Here are the steps and considerations when implementing your own watermark logic in Kafka Streams:

1. Timestamp Extraction

The first step is to correctly extract timestamps from the Kafka messages. Kafka Streams allows you to define a custom TimestampExtractor that reads the timestamp from each incoming record:

java
1import org.apache.kafka.streams.processor.TimestampExtractor;
2
3public class EventTimeExtractor implements TimestampExtractor {
4    @Override
5    public long extract(ConsumerRecord<Object, Object> record, long previousTimestamp) {
6        // Implementation goes here, typically extracting timestamp from the record value
7        return record.value().getTimestamp();
8    }
9}

2. Periodic Watermark Emission

Next, you will need to periodically update and emit the watermark. This can be done using a Punctuator that triggers actions at specific time intervals:

java
1import org.apache.kafka.streams.processor.PunctuationType;
2import org.apache.kafka.streams.processor.Cancellable;
3import org.apache.kafka.streams.processor.ProcessorContext;
4
5public void init(ProcessorContext context) {
6    Punctuator watermarkPunctuator = timestamp -> {
7        // Logic to update and emit watermark based on the observed records
8    };
9    
10    Cancellable watermarkSchedule = context.schedule(
11        Duration.ofSeconds(10),
12        PunctuationType.WALL_CLOCK_TIME,
13        watermarkPunctuator
14    );
15}

3. Handling Late Arrivals

With watermarks being emitted, you might need a way to handle records that are arriving late (i.e., records with a timestamp before the latest emitted watermark). Normally, you could buffer these records and process them once you know all earlier records have arrived.

Challenges and Considerations

  • Accuracy vs. Latency: More frequent updates to watermarks can provide higher accuracy event-time processing but at the cost of potentially higher computational overhead and lower throughput.
  • Out-of-orderness: Depending on the nature of the source data, records might arrive significantly out of order, which complicates the watermark logic significantly.

Summary Table

FactorDescription
Timestamp ExtractionExtract the timestamp from each message. Implement using custom TimestampExtractor.
Watermark EmissionPeriodically update and emit watermarks using a Punctuator.
Handling Late ArrivalsUse buffering mechanisms to process records that arrive after their respective watermarks.
Event Time ProcessingManually implemented since Kafka Streams does not support native event-time processing with watermarks.

Conclusion

While Kafka Streams offers a flexible platform for stream processing, implementing custom event time processing with watermarks requires careful planning and additional coding efforts. By understanding and implementing these techniques, developers can more effectively manage event time in their Kafka Streams applications, crucial for real-time analytics and processing scenarios where the exact order and timing of events matter.


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.