Part 5 of 8 · 2 min read
Time
Event time, watermark and windows.
Which time? Each click has two times: the event time, when it happened (set by the client), and the processing time, when the server handled it. They can differ a lot because of queues and delays. Using processing time is simple but inaccurate. Using event time is more accurate, but client clocks may be wrong and late events must be handled. The book recommends event time, because accuracy matters most.
Watermark. A one-minute window closes at the end of the minute, so a click that arrives a few seconds late would miss it. A watermark extends each window by a short extra time, say 15 seconds, before it is closed. A long watermark catches more late events but adds delay. A short one is faster and less accurate. Events later than the watermark are not worth a complicated design, since a nightly reconciliation (below) can fix the small error.
A worked example (made up). The window 12:00 to 12:01 stays open until 12:01:15. A click with event time 12:00:50 that arrives at 12:01:10 is counted. A click with event time 12:00:30 that arrives at 12:01:20 misses the window and is left to reconciliation.
Window types.
- Tumbling window: fixed, non-overlapping slices, such as each minute. Good for "clicks per minute".
- Sliding window: a window that moves forward, always covering the last M minutes. Good for "the last 5 minutes" and for top-N lists.
- Others, such as hopping and session windows, exist and are named in the book.
Hot ads. A very popular ad can overload the one node that holds its counts. A fix is to split a hot key into several sub-keys, count each part on a different node, and add the parts in the reduce step.
Watermark
A watermark is extra time that a window stays open for late events before its result is final.
Example: with a 15-second watermark, the 12:00 window is final at 12:01:15.
Check yourself: 1. Why prefer event time to processing time? 2. What is the trade-off in the watermark length? 3. How is a hot ad spread across nodes?