Why do 10-minute streaming windows drop late mobile events?
Question
A streaming job counts clicks in 10-minute tumbling event-time windows, starting with [10:00, 10:10). The watermark tracks max_event_time - 2 min, and allowed lateness is 0.
Six events arrive in processing order:
| Event | Event time | Arrives at | Delay |
|---|---|---|---|
e1 | 10:04 | 10:04 | 0 min |
e2 | 10:09 | 10:09 | 0 min |
e3 | 10:13 | 10:13 | 0 min |
e4 | 10:09 | 10:13 | 4 min |
e5 | 10:16 | 10:16 | 0 min |
e6 | 10:09 | 10:16 | 7 min |
- Why does
[10:00, 10:10)emit2clicks instead of4? - How do
allowed_lateness = 3 minand a side output change the outcome fore4ande6? - What trade-offs come with raising watermark delay or allowed lateness?
Show hints (3)
- "TRACE THE WATERMARK"
Update the watermark after each event as
max(watermark, event_time - 2 min). Check which event pushes it past10:10. - "EXTEND WINDOW LIFE"
With
allowed_lateness = 3 min, window[10:00, 10:10)stays open until the watermark reaches10:13. Check the watermark whene4ande6arrive. - "CATCH EXPIRED EVENTS"
Once the watermark passes
10:13, the window state is purged. Route older events to a side output instead of dropping them silently.
Show checked explanation
When e3 (10:13) arrives, the watermark jumps to 10:11, closing [10:00, 10:10) at 2 clicks and dropping both e4 and e6. Adding allowed_lateness = 3 min keeps the window open until watermark 10:13, so e4 updates the count to 3. Event e6 arrives at watermark 10:14 and goes to a side output for batch reconciliation.
def process(events, wm_delay=2, allowed=3): wm = 0 counts, late_out = {}, [] for eid, ev_min in events: wm = max(wm, ev_min - wm_delay) end = (ev_min // 10 + 1) * 10 if wm >= end + allowed: late_out.append(eid) else: counts[end] = counts.get(end, 0) + 1 return counts, late_out stream = [("e1", 4), ("e2", 9), ("e3", 13), ("e4", 9), ("e5", 16), ("e6", 9)] print("default:", process(stream, allowed=0)) print("fixed: ", process(stream, allowed=3))
default: ({10: 2, 20: 2}, ['e4', 'e6'])
fixed: ({10: 3, 20: 2}, ['e6'])Author-checked on CPython 3.12.10: ran the watermark simulator, compared event-time vs processing-time windowing, and tested allowed lateness and side-output routing against Apache Flink and Beam documentation. Checked by an editor other than the author.
Walkthrough
Event-time windows group clicks by when the user tapped, not when the server received the packet. That matters on mobile networks, where tunnels and offline retries delay uploads by several minutes. Switching to processing time would misplace e4 and e6 into [10:10, 10:20), giving 2 clicks in the first window and 4 in the second.
A watermark tells the engine that event time has advanced and older windows can emit. Here the watermark moves forward monotonically as max(watermark, event_time - 2 min). Trace every event for [10:00, 10:10):
| Event | Event time | Watermark | allowed=0 | allowed=3 min + side output |
|---|---|---|---|---|
e1, e2 | 10:04, 10:09 | 10:02, 10:07 | Count = 2 | Count = 2 |
e3 | 10:13 | 10:11 | Fires [10:00, 10:10) = 2 | Fires [10:00, 10:10) = 2 |
e4 (4 min late) | 10:09 | 10:11 | Dropped (10:11 >= 10:10) | Accepted; re-fires 3 (10:11 < 10:13) |
e5 | 10:16 | 10:14 | [10:10, 10:20) count = 2 | Purges [10:00, 10:10) (10:14 >= 10:13) |
e6 (7 min late) | 10:09 | 10:14 | Dropped | Routed to side output for batch merge |
Three levers, three trade-offs:
- Larger watermark delay (for example
10 min) catches more late clicks before the first emit, but delays every initial dashboard update by 8 extra minutes. - Allowed lateness (
3 minhere) keeps the first emit fast while lettinge4trigger a corrected update. You pay with extra window state in memory or disk, and downstream sinks must handle upserts or retractions instead of append-only writes. - Side output plus batch reconciliation bounds streaming state while preventing silent loss. Events past
allowed_lateness, likee6or a phone offline for hours, land in a late-events store and merge into warehouse tables on a scheduled job.
Common mistakes
- Switching from event time to processing time to stop drops. That silently puts
e4ande6into[10:10, 10:20)and skews traffic charts. - Setting watermark delay to hours so zero events are late. Every live metric then waits hours before emitting its first number.
- Enabling allowed lateness while writing to an append-only sink. Re-firing
[10:00, 10:10)appends both2and3, turning one window into5clicks. - Leaving late data dropped after allowed lateness with no side output or metric. Mobile outages then disappear from revenue reports silently.
Follow-up questions
- "What happens if one Kafka partition goes idle and sends no events for 15 minutes?" An operator's watermark is the minimum across all input partitions. One idle partition freezes the global watermark, stalling window emits until you configure idle-source detection so the engine temporarily ignores quiet partitions.
- "How should downstream tables store re-fired results from allowed lateness?" Key the sink table by
(window_start, metric_key)and upsert each re-fired total, or consume retraction plus update records. Append-only sinks double-count whenever a window re-fires. - "Why not set allowed lateness to 7 days so you never need a side output?" Every open window holds state until
window_end + allowed_lateness. Keeping 7 days of 10-minute windows across millions of keys bloats state size, slows checkpoints, and hurts recovery time.
What a strong answer covers
Traces the watermark jumping to 10:11 on e3 and dropping e4 and e6. Combines a short watermark delay, bounded allowed lateness with upsert-safe sinks, and a side output for late reconciliation.
Open in interactive practice deck →