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.
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?
Alerts need speed, so use a short delay such as 30 seconds and drop late events, because an alert about 20 minutes ago no longer helps anyone. Pay needs completeness, so do not rely on a window closing on time: keep late events through a side output, or compute pay in a batch job after devices have had time to sync. One stream can feed both under different policies.
Use this as a guide for self review; written answers are not graded.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.