Özer Cem KELAHMETKELAHMET
AI Simulations · Data management

Streaming windows and watermarks: how Spark and Flink decide an event is too late

In a stream, events arrive out of order and some arrive very late. Set the window size and the watermark delay and watch which events are counted, which are dropped, and how much latency every result pays for completeness.

Download Markdown

Interactive simulation. It needs JavaScript; the text below explains what it shows.

What you are looking at

Each dot on the chart is an event: along the bottom, when it happened; up the side, when it reached the stream processor. Events on the solid diagonal arrived instantly; the higher a dot sits above it, the later it arrived. The dashed staircase marks, for each window, the last moment an event for that window is still accepted. A dot above the staircase arrived after its window was finished and is dropped.

The table lists the 26 events with their lateness, the end of the window each belongs to, whether it was dropped and how long its result waited. The bars show each event's lateness, so the outliers from the network outage stand out at once.

The algorithm

Stream processors such as Spark Structured Streaming and Apache Flink aggregate by event time: an event counts in the window of when it happened, not when it arrived. With tumbling windows of W seconds, an event at time e belongs to the window ending at (floor(e / W) + 1) × W.

The difficulty is knowing when a window is complete, since a late event could always still come. A watermark answers that: it is the engine's estimate of how far event time has progressed, typically the largest event time seen minus a fixed delay D. When the watermark passes the end of a window, the window is final. In append mode its result is emitted and its state is dropped, and any event for it that arrives afterwards is discarded.

The simulation assumes the stream is mostly on time, so the largest event time seen is close to the current arrival time. An event is then dropped when its arrival time minus D is past the end of its window.

With the defaults, 10-second windows and a 5-second delay, the median event arrives 1.1 seconds late but the average is 2.7 seconds because of a few outliers. Two events are dropped: the one at 36 s arrived at 52 s, long after its window closed at 40 s, and the one at 48 s arrived at 57.5 s, after its window closed at 50 s. Results wait 10.5 seconds on average from event to output: the rest of the window plus the delay.

Things to try

  • Raise the delay to 12 seconds: no event is dropped, but results now wait 17.5 seconds on average instead of 10.5.
  • Set the delay to 0: results are fastest (5.5 seconds on average), but 6 of the 26 events, 23%, miss their window and are lost.
  • Make the windows 30 seconds long: with the same 5-second delay no event is dropped, because fewer events straddle a boundary, but results wait 20.9 seconds on average.
  • Find the smallest delay that drops less than 5% of events (here 8 seconds), a common way to set a watermark from measured lateness.
  • Paste event and arrival times from your own stream to see its lateness distribution.

How it works in real systems

In Spark Structured Streaming, withWatermark sets the delay on an event-time column. In append mode a window is emitted once the watermark passes it; in update mode results are revised as events arrive and the watermark mainly bounds state size. Flink generates watermarks with strategies such as bounded out-of-orderness, and adds allowed lateness and side outputs so late events can be captured instead of silently dropped. Kafka Streams uses grace periods for the same purpose. In every engine the trade-off is the same: a longer wait means more complete results, more state held in memory and more latency.

Sources

Last updated Nov 23, 2026
Type to search.
    ↑↓ navigate↵ open