# 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.

<div data-sim="specx" data-spec="{&quot;level&quot;:&quot;advanced&quot;,&quot;head&quot;:&quot;Streaming windows and watermarks: when is late too late?&quot;,&quot;sub&quot;:&quot;Events carry the time they happened but arrive later. Choose a window size and a watermark delay and see which events are counted and how long results wait.&quot;,&quot;params&quot;:[{&quot;id&quot;:&quot;window&quot;,&quot;label&quot;:&quot;Window size&quot;,&quot;min&quot;:5,&quot;max&quot;:30,&quot;step&quot;:5,&quot;value&quot;:10,&quot;unit&quot;:&quot;s&quot;,&quot;note&quot;:&quot;Tumbling windows on event time&quot;},{&quot;id&quot;:&quot;delay&quot;,&quot;label&quot;:&quot;Watermark delay&quot;,&quot;min&quot;:0,&quot;max&quot;:30,&quot;step&quot;:1,&quot;value&quot;:5,&quot;unit&quot;:&quot;s&quot;,&quot;note&quot;:&quot;How long the engine waits for late events&quot;}],&quot;data&quot;:{&quot;columns&quot;:[&quot;event&quot;,&quot;arrival&quot;],&quot;rows&quot;:[[1,1.6],[3,3.4],[4,6.1],[7,7.9],[9,10.2],[11,11.5],[12,24.5],[14,14.8],[16,17.1],[18,19.6],[21,21.9],[22,30.5],[25,25.7],[27,28.4],[29,31.2],[32,33.1],[34,34.9],[36,52.0],[38,39.2],[41,41.8],[43,44.6],[45,46.1],[48,57.5],[51,51.9],[53,54.4],[56,57.0]],&quot;note&quot;:&quot;Twenty-six events from a sensor feed: when each happened and when it reached the stream processor, in seconds. Most arrive within two seconds; a few are delayed by a network outage. Paste your own event and arrival times.&quot;},&quot;row&quot;:[{&quot;id&quot;:&quot;late&quot;,&quot;label&quot;:&quot;lateness (s)&quot;,&quot;expr&quot;:&quot;arrival - event&quot;,&quot;tex&quot;:&quot;\\ell_i = a_i - e_i&quot;},{&quot;id&quot;:&quot;wend&quot;,&quot;label&quot;:&quot;window end&quot;,&quot;expr&quot;:&quot;(floor(event/window) + 1)*window&quot;,&quot;tex&quot;:&quot;w_i = \\left(\\left\\lfloor e_i / W \\right\\rfloor + 1\\right) W&quot;},{&quot;id&quot;:&quot;dropped&quot;,&quot;label&quot;:&quot;dropped&quot;,&quot;expr&quot;:&quot;gt(arrival - delay, wend)&quot;,&quot;fmt&quot;:&quot;i&quot;,&quot;tex&quot;:&quot;\\text{drop}_i = [\\,a_i - D &gt; w_i\\,]&quot;},{&quot;id&quot;:&quot;wait&quot;,&quot;label&quot;:&quot;event to result (s)&quot;,&quot;expr&quot;:&quot;wend + delay - event&quot;}],&quot;derived&quot;:[{&quot;id&quot;:&quot;ndrop&quot;,&quot;label&quot;:&quot;Events dropped as too late&quot;,&quot;expr&quot;:&quot;sum(dropped)&quot;,&quot;fmt&quot;:&quot;i&quot;},{&quot;id&quot;:&quot;drop_rate&quot;,&quot;label&quot;:&quot;Share dropped&quot;,&quot;expr&quot;:&quot;mean(dropped)&quot;,&quot;fmt&quot;:&quot;p&quot;},{&quot;id&quot;:&quot;avg_late&quot;,&quot;label&quot;:&quot;Average lateness (s)&quot;,&quot;expr&quot;:&quot;mean(late)&quot;},{&quot;id&quot;:&quot;med_late&quot;,&quot;label&quot;:&quot;Median lateness (s)&quot;,&quot;expr&quot;:&quot;med(late)&quot;},{&quot;id&quot;:&quot;avg_wait&quot;,&quot;label&quot;:&quot;Average wait for a result (s)&quot;,&quot;expr&quot;:&quot;mean(wait)&quot;,&quot;tex&quot;:&quot;\\bar T = \\frac{1}{n}\\sum_i (w_i + D - e_i)&quot;}],&quot;chart&quot;:{&quot;title&quot;:&quot;When events happened and when they arrived&quot;,&quot;x&quot;:{&quot;var&quot;:&quot;t&quot;,&quot;label&quot;:&quot;Event time (s)&quot;,&quot;from&quot;:0,&quot;to&quot;:60},&quot;y&quot;:{&quot;from&quot;:0,&quot;to&quot;:60},&quot;series&quot;:[{&quot;label&quot;:&quot;Arrives instantly&quot;,&quot;expr&quot;:&quot;t&quot;},{&quot;label&quot;:&quot;Last accepted arrival for its window&quot;,&quot;expr&quot;:&quot;(floor(t/window) + 1)*window + delay&quot;,&quot;dash&quot;:true}],&quot;points&quot;:{&quot;x&quot;:&quot;event&quot;,&quot;y&quot;:&quot;arrival&quot;,&quot;label&quot;:&quot;Events (above the dashed steps = dropped)&quot;}},&quot;bars&quot;:{&quot;title&quot;:&quot;Lateness of each event (s)&quot;,&quot;y&quot;:&quot;late&quot;,&quot;label&quot;:&quot;event&quot;},&quot;steps&quot;:[{&quot;title&quot;:&quot;Assign event-time windows&quot;,&quot;text&quot;:&quot;Each event belongs to a {window}-second window by when it happened, not when it arrived.&quot;,&quot;show&quot;:[]},{&quot;title&quot;:&quot;Measure lateness&quot;,&quot;text&quot;:&quot;Events arrive on average {avg_late} s after they happen (median {med_late} s): the median hides the outliers.&quot;,&quot;show&quot;:[&quot;avg_late&quot;,&quot;med_late&quot;]},{&quot;title&quot;:&quot;Advance the watermark&quot;,&quot;text&quot;:&quot;The watermark trails the stream by {delay} s. A window is final once the watermark passes its end.&quot;,&quot;show&quot;:[]},{&quot;title&quot;:&quot;Drop what is too late&quot;,&quot;text&quot;:&quot;An event arriving after its window is final is dropped: {ndrop} events, {drop_rate:p} of the stream.&quot;,&quot;show&quot;:[&quot;ndrop&quot;,&quot;drop_rate&quot;]},{&quot;title&quot;:&quot;Pay for completeness with latency&quot;,&quot;text&quot;:&quot;Results wait for the window to close plus the delay: {avg_wait} s on average from event to result.&quot;,&quot;show&quot;:[&quot;avg_wait&quot;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## 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

- [Apache Spark: Structured Streaming programming guide, handling late data and watermarking](https://spark.apache.org/docs/latest/structured-streaming-programming-guide.html)
- [Apache Flink documentation: Timely stream processing](https://nightlies.apache.org/flink/flink-docs-stable/docs/concepts/time/)
- [Akidau et al.: The Dataflow Model (Google Research)](https://research.google/pubs/the-dataflow-model-a-practical-approach-to-balancing-correctness-latency-and-cost-in-massive-scale-unbounded-out-of-order-data-processing/)
