LESSON 05 · Time and windows

Decide when a window is complete

Grouping events by when they happened rather than when they arrived requires a watermark, an estimate of when a time window has all its data.

SYSTEM VIEW

The data flow

Diagram view
Watermark—
12:00–12:01—
Late—

Watch a window job count phone events by the time they happened, close a minute when its watermark passes, then decide what to do with an event that arrives late.

  1. Phones stamp each event when it happens: its event time. Processing time is when the window job handles it. Back from a 10-minute outage, the job handles the backlog in one minute: by processing time, about 4,500 events against a usual 410, a spike that never happened.

    Usual minute
    about 410
    Outage
    10 minutes
    Minute after
    about 4,500

    What changed

    • Window job back from a 10-minute outage
    • Backlog handled in one minute
    • Dashboard spikes to 4,500/min
  2. By event time, each event goes under its own stamp. 12:00:24, held up on the network, reaches the topic after 12:00:41, yet the window job files it ahead of 12:00:41. A tumbling window takes each whole minute, here 12:00 to 12:01; a sliding window overlaps, and a session window closes after a gap.

    Window
    12:00 to 12:01
    Kind
    tumbling, 1 minute

    What changed

    • Events filed by event time
    • 12:00:24 filed before 12:00:41
    • Window 12:00 to 12:01
  3. When is a minute complete? The watermark is the job’s estimate that events older than time T have probably all arrived: here the latest event time seen minus 30 seconds. 12:00:57 sets it to 12:00:27, short of 12:01, so the window stays open.

    Watermark
    latest − 30 s
    Latest seen
    12:00:57

    What changed

    • 12:00:57 arrives
    • Watermark at 12:00:27
    • Window still open
  4. An event stamped 12:01:30 moves the watermark to 12:01:00, the end of the window. The window from 12:00 to 12:01 closes and emits its count to the dashboard: 412 events, of which the strip shows a few.

    Latest seen
    12:01:30
    Count
    412

    What changed

    • 12:01:30 arrives
    • Watermark at 12:01:00
    • 12:00 to 12:01 emits 412
  5. A phone reconnects and sends an event stamped 12:00:50. It is the newest record in the topic, but by event time it belongs to the window that already closed: it lands behind the watermark. It is late.

    What changed

    • 12:00:50 arrives after 12:01:30
    • Behind the watermark: late
  6. The window already emitted 412, so the job needs a policy. Drop the event and count the drops; update the emitted result to 413; or route the event to a side output for later correction. Try each.

    Late event
  7. The delay trades freshness for completeness. With 1 minute instead of 30 seconds, 12:01:30 would move the watermark only to 12:00:30: the window would stay open and 12:00:50 arrive in time, but the count would wait until events reach 12:02:00. A short delay gives fast results and more late events.

    30 s delay
    faster, more late
    1 min delay
    slower, fewer late

    What changed

    • A 1-minute delay: 12:00:30
    • The window would wait for 12:00:50
  8. The job takes the minimum watermark over its partitions. P1 has had no event since 12:01:40, so its watermark stays at 12:01:10 while P0’s passes 12:02, and the window from 12:01 to 12:02 stays open. Device clocks can be wrong too, so validate timestamps at ingest.

    P0 watermark
    12:02:01
    P1 watermark
    12:01:10
    Job takes
    the minimum

    What changed

    • P0 reaches 12:02:31
    • P1 idle since 12:01:40
    • Watermark stuck at 12:01:10

Space plays · ← → step · Home resets

THE IDEA

How it works

Event time is when something happened, stamped at the source. Processing time is when your system handles it. They drift apart because of network delays, phones going offline, and consumers catching up. Count by processing time and a consumer recovering from a 10-minute outage piles old events into the current minute, drawing a spike that never happened. Event time gives the true shape, but raises a question: how do you know a period is finished?

A window groups events by time. A tumbling window uses fixed, non-overlapping periods such as each minute, a sliding window overlaps, as in the last five minutes updated each minute, and a session window closes after a gap in activity. A watermark is the processor’s estimate that events older than time T have probably all arrived. When it passes the end of a window, that window closes.

Suppose the watermark is the latest event time seen minus 30 seconds. An event stamped 12:01:30 moves the watermark to 12:01:00, so the window from 12:00 to 12:01 closes and emits its count, say 412. Then a phone reconnects with an event stamped 12:00:50. It is late, and you choose: drop it and count the drops, update the emitted result, or route it to a side output for later correction.

A short delay gives fast results and more late events; a long one gives the reverse. Because a processor typically takes the minimum watermark across its inputs, one idle partition can stall every window. Device clocks can also be wrong, so validate timestamps at ingest.

DECISION POINT

A delivery app feeds one event stream into live per-city delay alerts and monthly courier pay. Couriers lose signal in tunnels for up to 20 minutes. How do you set the watermark and late-data policy for each?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Event time says when something happened; processing time says when you handled it.
  • A watermark is an estimate, so late events remain possible and need a policy.
  • A longer watermark delay trades freshness for completeness.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

After a 10-minute consumer outage, a per-minute chart shows a huge spike at the moment of recovery, though traffic was flat. What is wrong?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.