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.
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:
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:
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
| Factor | Description |
| Timestamp Extraction | Extract the timestamp from each message. Implement using custom TimestampExtractor. |
| Watermark Emission | Periodically update and emit watermarks using a Punctuator. |
| Handling Late Arrivals | Use buffering mechanisms to process records that arrive after their respective watermarks. |
| Event Time Processing | Manually 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
- How does Kafka Streams work with Partitions that contain incomplete Data?
- how does kafka synchronize data timestamps between different brokers and how is timestamp created in kafka
- How does Kinesis achieve Kafka style Consumer Groups?
- How does max.poll.records affect the consumer poll
- How does one Kafka consumer read from more than one partition?
- How does RabbitMQ actually store the message physically?
- How does RabbitMQ compare to Mule
- How does rabbitmq heartbeat work

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.