Course contents 24 / 35

24 / 35 · Streams and State

Time, windows and late data

Assign sales to event-time windows, interpret a watermark and revise a result under an explicit lateness policy.

The register records a sale at 10:04. The processor receives it at 10:06:30. If the report counts by purchase time, the sale belongs to a period that has already ended on the processor’s clock. Counting by arrival would answer a different question.

Which clock assigns the event?#

Event time is the time the record says the event occurred. Processing time is the clock of the system handling the record. Event time can arrive out of order when registers buffer sales or network paths differ. Assume the register timestamps share one time scale and the example’s three events have distinct identities. The processor groups by event time and can use processing time to decide when to publish a provisional total. Akidau et al., 2015, §§2.1–2.3

Sale amount, centsEvent timeProcessing arrival
10010:0110:04:50
5010:0310:05:20
7010:0410:06:30

A window gives a group a time boundary. Five-minute tumbling windows are adjacent, nonoverlapping intervals. Use the half-open interval [10:00,10:05)[10{:}00,10{:}05) and its neighbor [10:05,10:10)[10{:}05,10{:}10). The three sales belong to the first window. A sale at 10:05 belongs to the second. Sliding windows may overlap. With five-minute windows starting every minute, a sale at 10:04:30 belongs to five windows starting from 10:00 through 10:04. The window type changes the number of state updates per event. Apache Flink 2.1, Window Assigners

What does the watermark let us emit?#

A watermark describes progress in event time. With imperfect information, it is an estimate that earlier events are unlikely to appear. A trigger decides when a result is emitted. It can use a watermark, a processing clock or a record count. Group membership and publication time are two separate rules. Akidau et al., 2015, §2.3 Apache Beam, Watermarks and late data

At processing time 10:05:40, assume the watermark reaches 10:05 and the system emits 150 for the first window. A sale with event time 10:04 arrives later, when the watermark is 10:06. Under an allowed lateness of two minutes, the toy policy retains the state until the watermark reaches 10:07. The sale arrives before cleanup, so its 70 cents produce a revised value of 220 for the same window. This is an event-time example with a release and cleanup rule. It does not require a particular engine’s trigger configuration.

Figure

One event can revise an earlier window

One event can revise an earlier windowFirst release · 150. Late event at 10:04 · +70. Revised result · 220First release150Late event at 10:04+70Revised result220
The early result is based on two events. The late event belongs to the same event-time interval and revises it.

Flink’s time windows support keeping state for configured allowed lateness and can emit a late firing when accepted data changes a window. That output is another version of the same result. A sink may replace the key (store, window_start) or accept an explicit change. Adding the two absolute totals would be wrong. Apache Flink 2.1, Allowed Lateness

Decide what happens after cleanup#

Once the watermark reaches 10:07 in the toy policy, the operator discards the window state. An even later sale cannot simply be added to that discarded state. The system can drop it under its policy, route it for inspection, or reconcile it through a historical recomputation. Longer allowed lateness preserves more chances to revise a result and keeps more windows in state. A source that does not advance its watermark can also delay emission and cleanup. The stream topology needs a rule for how idle inputs affect progress. Apache Flink 2.1, Windows Apache Beam, Watermarks and late data

Next#

A window can be revised only while its earlier work is available. State and checkpoints shows how to store that work and restore it together with a position in the input.

References#

  1. Tyler Akidau et al. The Dataflow Model, PVLDB 8(12), 2015, §§2.1–2.3. Separates time domains, windows, watermarks and triggers.
  2. Apache Software Foundation. Apache Flink 2.1 Windows. Window Assigners, Window Lifecycle and Allowed Lateness explain grouping, cleanup and late firings.
  3. Apache Software Foundation. Apache Beam Programming Guide. Describes progress and events arriving after a watermark.