Skip to content
tezvyn:
Menu

Watermarks: Defining 'Done' in Event Streams

Source: nightlies.apache.orgMediumHow cards are made

A watermark tells a stream processor when a time window is 'complete' for unordered events. It's a timestamped signal declaring 'no more data is expected before this point,' allowing aggregations to be finalized. The key footgun is balancing lateness vs.

Why it exists

In distributed systems, data rarely arrives in the order it was created. Network latency and clock skew mean an event from 10:01 AM might arrive after an event from 10:02 AM. If you need to calculate a result over a time window, like 'clicks per minute,' how do you know when you've seen all events for that minute? You can't wait forever.

The mental model

A watermark is like a train conductor shouting "All aboard!" before closing the doors. It's a declaration moving through the stream that says, "I am confident no more events older than timestamp T will arrive." This signal of completeness allows the system to stop waiting, close the time window, and finalize its calculation.

How it works

A watermark is a special record in the data stream that contains a timestamp. When an operator (e.g., a window aggregator) receives a watermark with timestamp T, it knows that it will not see any more regular events with a timestamp less than T. It can then trigger computations for all time windows that end before T. Watermark generation strategies typically track the maximum event time seen so far and subtract a fixed 'allowed lateness' period. This creates a heuristic trade-off between waiting for late data and processing results quickly.

When to use it

Use watermarks for any stateful stream processing that relies on event time. This is the foundation for time-windowed aggregations like tumbling windows (e.g., hourly sums), sliding windows (e.g., a 5-minute moving average), and session windows (e.g., grouping user activity with gaps of inactivity). It's what separates true event-time processing from simpler processing-time logic.

When not to use it

Watermarks are unnecessary if you are using processing time, where you only care about when the system observes an event, not when the event occurred. They are also not needed for stateless operations (like a simple map or filter) or in the rare case where your event stream is guaranteed to be perfectly ordered by event time.

One canonical example

A Flink job counts user clicks in 1-minute tumbling windows. Events arrive out of order. When the system generates and processes a watermark for timestamp 12:35:00, it triggers the final calculation for the 12:33:00-12:34:00 window. The system sums all the clicks it has buffered for that window and emits a single result. Any click event with a timestamp in that window that arrives after the watermark has passed is considered late and will be dropped or handled separately.

Interview question

Why are watermarks crucial in event-time stream processing for aggregations like 'clicks per minute'?

  • a.They provide a mechanism to define when a time window is considered complete, despite out-of-order event arrival.Correct
  • b.They synchronize the processing time of different operators across a distributed system.
  • c.They prevent data loss by buffering all events indefinitely until all related data arrives.
  • d.They guarantee that all events arrive in perfect chronological order, simplifying windowing logic.
Why?

The card states that a watermark tells a stream processor when a time window is 'complete' for unordered events, allowing aggregations to be finalized. Option D is incorrect because watermarks handle out-of-order events but do not reorder them or guarantee perfect chronological arrival; they signal completeness despite disorder.

Just read this? Test yourself on what you have been reading.

Read the original → nightlies.apache.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles