Watermarks

Concepts covered: Structured Streaming

A window from 12:00 to 12:10 can always receive one more row: a phone that went offline can upload a 12:04 event an hour later. Without a rule for when to stop waiting, a windowed count keeps every window it ever opened, and its state grows until the executors run out of memory. A watermark is that rule, a moving event-time line behind which Spark treats data as finished.

from pyspark.sql import functions as F

trip_counts = (
    trip_events
    .withWatermark("event_time", "10 minutes")
    .groupBy(F.window("event_time", "10 minutes"), "city")
    .count()
)

query = (
    trip_counts.writeStream
    .outputMode("append")
    .format("parquet")
    .option("path", "/lake/trip_counts")
    .option("checkpointLocation", "/chk/trip_counts")
    .start()
)

The watermark equals the largest event time seen so far, minus the delay. Spark computes it at the end of each micro-batch, taking the maximum across all partitions, and applies it during the next batch. It never moves backwards, and until the first batch finishes there is none, so the first batch drops nothing.

Which late rows are dropped

For a windowed aggregation the test runs on the row's window, and the row's own timestamp only picks that window: Spark drops a row when its window ends at or before the watermark. Trace 4 batches of the query above, 10-minute windows with a 10-minute delay:

BatchArriving eventsWatermark in useWhat happensWatermark after
012:02, 12:07, 12:14noneWindows 12:00 to 12:10 (count 2) and 12:10 to 12:20 (count 1) open12:04
112:21, 12:0312:0412:03 kept, since its window ends at 12:10; window 12:20 to 12:30 opens12:11
2none (no-data batch)12:11Window 12:00 to 12:10 ends before 12:11: written with count 3, then removed12:11
312:08, 12:1312:1112:08 dropped, its window is gone; 12:13 joins window 12:10 to 12:2012:11

The 12:03 row arrived 18 minutes behind the newest event and still counted, because its window was open. The 12:08 row was 13 minutes behind and was dropped, because its window had closed. So the delay is a floor: a row within the delay of the newest event time is always kept, and a row further behind survives until the watermark passes its window's end.

What the watermark frees

The same test clears state. When the watermark passes a window's end, Spark deletes that window's key from the store, so state holds only windows still open: roughly a delay plus a window length of data. In append mode, deletion is also when the window is written, once, with its final count, which is why append output trails event time by at least the delay. Update mode writes a window's new count on every batch that changes it and still deletes closed windows. Complete mode rewrites the whole result each batch, needs every window it ever saw, and the watermark frees nothing.

Windows are 10 minutes long, the delay is 10 minutes, and the watermark stands at 14:27. Which arriving row does Spark drop?

The cell replays a delivery log in arrival order. For each event it takes the newest event time received so far minus the delay, and flags the event if its own time sits at or behind that line. That is the test a watermarked dropDuplicates applies, since it has no window to check. If you change delay_seconds from 600 to 60 and rerun the cell, more events fall behind; count how many.

The cell moves the line after every row, but Spark moves the watermark only at the end of a batch and applies it in the next one. Each flag therefore marks the earliest point at which Spark could drop that event: an event that arrives in the same batch as the newer event is still kept, so Spark drops the same rows or fewer. The cell also tests each event's own time, the rule for dropDuplicates; a windowed count tests the end of the event's window instead.

About this lesson

This section is part of the Structured Streaming: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.