Skip to content

Event time, windows and late events

Every event in a stream has two times. Event time is when it happened, as stamped by its source and carried in the record. Processing time is when the processor sees it, by the processor’s own clock. Flink’s documentation puts it this way: an hourly processing-time window holds all the records that reached an operator between the moments the system clock showed the full hour.

Processing time is simple and has the lowest delay, but it measures the pipeline as much as the world: if the network is slow for a minute, the count of “clicks in this minute” changes, though no one clicked any differently. Event time answers the question people actually ask, how many things happened in that minute, and it gives the same answer when the same log is replayed next week. The price is that events do not arrive in the order they happened. A phone that taps at 12:00:03 and uploads from the other side of a tunnel at 12:07 is a record that comes in seven minutes late.

Mental model. Each event carries its own wristwatch time, and the processor has a clock on its wall. A count that follows the wall puts every event in the minute in which it was seen. A count that follows the wristwatches puts it in the minute in which it happened, and then has to decide how long to wait for the stragglers.

The timestamps themselves come from machines whose clocks can be wrong, which is a subject of the consistency and time module. Here the timestamps are taken as given.

A stream has no end, so a count over it needs a window: a stretch of time to count in. Flink’s documentation describes three common kinds. A tumbling window has a fixed size and does not overlap: with a size of 1 minute, each event falls in exactly one window. A sliding window has a size and a slide, and overlaps when the slide is smaller than the size: with a size of 60 seconds and a slide of 30 seconds, each event falls in 2 windows, the size divided by the slide. A session window groups the events of a burst of activity and closes when no event has come for a set period, so it has no fixed start or end.

If events can arrive late, when can a window be closed? Flink’s answer is a watermark: a watermark of time t declares that event time has reached t in the stream, so there should be no more elements with a timestamp of t or earlier. Watermarks are a mechanism to signal progress in event time, and the window can fire when the watermark passes its end. Some events can be arbitrarily delayed, so no rule can be certain. The Dataflow paper makes the same point, that a processor never knows whether it has seen all the data, only that more can arrive. A watermark is a promise that the system makes and can break, and an event that arrives behind it is a late event.

By default, Flink’s allowed lateness is 0, and elements that arrive behind the watermark are dropped. Set it to more, and an element that arrives after the watermark passed the window’s end, but before it passes the end plus the allowed lateness, is still added to the window, and the window fires again with an updated result.

The table below is a toy, not a lab, and its figures come from a few lines of arithmetic in the lesson’s tests. There are six events, and two one-minute tumbling windows, the first minute from 0 to 60 seconds of event time and the second from 60 to 120. The toy’s rule for the watermark is the highest event time seen so far, minus 10 seconds.

Event Event time Arrives at
A 10 s 12 s
B 50 s 55 s
C 58 s 66 s
D 65 s 67 s
E 110 s 112 s
F 20 s 115 s

Count them four ways:

How they are counted First minute Second minute
By arrival time (processing time) 2 4
By event time, all events 4 2
By event time, watermark 10 s behind, no allowed lateness 3 2
The same, with 60 s of allowed lateness 4 2

Counting by arrival gets both minutes wrong: it puts C and F, which happened in the first minute, in the second, and gives 2 and 4 against the true 4 and 2. Counting by event time with a watermark gives the first minute at event E, which arrives at 112 s, when the watermark reaches 100 s and passes the end of the window: A, B and C make 3. Event F, stamped 20 s, arrives at 115 s, after its window has closed, so it is late and is dropped, and the first minute stays at 3 when the truth is 4. With 60 seconds of allowed lateness the window is kept until the watermark reaches 120 s, F is added, and the window fires again with 4. The second minute is still open at the end of the data, with D and E in it.

Challenge

Event time or processing time?

Event time is when something happened, as stamped by its source. Processing time is when the processor sees it, by the processor’s own clock. Put each line under the clock it depends on.

Sorting needs JavaScript.

Categories

Event time
The timestamp carried by the record.
Processing time
The clock of the machine doing the work.
  1. Line 1 of 8A phone records a tap at 12:00:03, loses signal in a tunnel and uploads it at 12:07. The tap belongs to the minute starting at 12:00.
  2. Line 2 of 8A job counts the records that reached it between 12:00 and 12:01 on its own clock.
  3. Line 3 of 8Orders per hour as customers experienced them, even when some orders reach the system late.
  4. Line 4 of 8Limit a worker to handling 1,000 records per second.
  5. Line 5 of 8Re-run a job over last week’s stored log and get the same counts as the live run did.
  6. Line 6 of 8A window that closes when the machine’s clock reaches the next full hour, whatever the records say.
  7. Line 7 of 8Put a sensor’s readings in the order the sensor wrote them, although the network delivered them out of order.
  8. Line 8 of 8Raise an alarm when no record has arrived for 30 seconds.

Check your answers at least once before looking at the answers.

The toy shows the trade that every stream job makes. A watermark that trails the newest event by 0 seconds fires the first minute at 67 s, 7 seconds after its end, and F is late. With 10 seconds, the first minute fires at 112 s, 52 seconds after its end. The wait is not buying F: F is stamped 20 s and arrives when event time has reached 110 s, so for the window to still be open the watermark would have to trail by more than 50 seconds, and then the first minute could not fire before event time passed 110 s, which in this data it never does. A longer wait gives more complete answers, and later ones. A shorter wait gives early answers that are more often wrong, and need either a correction or a loss.

What happens if a phone is offline for an hour?

Section titled “What happens if a phone is offline for an hour?”

Its events come in an hour late, which is 3,600 seconds. A watermark delay or an allowed lateness of 60 seconds does not cover that, and keeping windows open for an hour means keeping their state for an hour. There is no setting that is right for every case, so the decision has to be made on purpose, and there are three honest options: drop the late events and count how many were dropped, keep the windows open longer and accept that results are revised, or let a later batch job correct the figures. The second one changes what the consumers downstream must expect, since Flink’s documentation says the elements emitted by a late firing should be treated as updated results of an earlier computation. The third is the subject of the last lesson of this module.

  • Event time is when something happened, from the record. Processing time is when the processor saw it. Counting by event time gives the answer people mean, and gives the same answer on replay.
  • Tumbling windows do not overlap, a sliding window of 60 seconds that slides by 30 puts each event in 2 windows, and a session window closes after a quiet period.
  • A watermark says that event time has reached a point and no earlier event should come. It can be wrong, and an event behind it is late.
  • In the toy, counting by arrival gives 2 and 4 against a true 4 and 2. With a watermark 10 seconds behind the first minute is 3 and event F is dropped, and with 60 seconds of allowed lateness it is updated to 4.
  • Waiting longer for late events gives fuller results later, and waiting less gives earlier results that need corrections. An offline phone’s hour of delay needs a decision, not a default.

Next: offsets, replay and what exactly-once really means.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.