How to Implement Event-Time Windowing with Watermarks
Build tumbling, sliding and session windows over event time with watermarks, so stream aggregates reflect when things happened, not when they arrived.
On this page
Introduction
A stream aggregate is only correct if it groups events by when they happened, not by when the processor saw them. Event-time windows group by the timestamp inside each event, which is stable and reproducible; watermarks decide when a window is complete enough to emit, balancing accuracy against latency. Tumbling windows are fixed and non-overlapping, sliding windows overlap, and session windows group activity separated by gaps. Choosing the right window and watermark is most of what makes a streaming aggregate trustworthy.
Event Time Versus Processing Time
Processing-time windows group by when the event reached the processor, which is easy but non-deterministic: the same data replayed at a different speed produces different aggregates. Event-time windows group by the timestamp in the event, so the result depends only on the data, which is what makes a streaming result reproducible and comparable to a batch result.
The gap between event time and processing time is lateness, and it is why event-time processing needs watermarks. A watermark is the system’s estimate that no events earlier than a certain time will arrive, which lets it close and emit a window. Without it, a window can never be considered complete.
# event-time vs processing-time
event_time: group by the timestamp in the event (deterministic, replayable)
processing_time: group by arrival time (easy, but non-deterministic)
# lateness = max(event time seen) - max(processing time)
# watermark = max(event time seen) - allowed_delay (see next sections)
Tumbling Windows
A tumbling window divides time into fixed, non-overlapping intervals, such as one minute each, and each event belongs to exactly one window. It is the simplest window and is right for periodic aggregation where every event counts once, such as per-minute counts or per-hour totals.
The window boundaries are aligned to a fixed origin, so a one-minute tumbling window at 10:02:30 covers 10:02:00 to 10:03:00. That alignment makes windows comparable across time and across partitions, which is what a dashboard needs. Each window emits once, when the watermark passes its end.
-- Tumbling 1-minute event-time window with a bounded lateness allowance.
SELECT
window_start,
window_end,
COUNT(*) AS events,
SUM(amount_cents) AS total
FROM TABLE(
TUMBLE(TABLE purchases, DESCRIPTOR(event_time), INTERVAL '1' MINUTE)
)
GROUP BY window_start, window_end;
-- each event belongs to exactly one window; emit when the watermark passes
Sliding Windows
A sliding window of size S and slide D covers a range of S ending at a boundary every D. With a slide smaller than the size, an event belongs to multiple overlapping windows, which is what you want for a moving average or a rolling count. It smooths the series at the cost of more windows and more work.
Choose the size and slide from the question: a five-minute window sliding every minute gives a rolling five-minute total updated each minute. A large size with a small slide multiplies the number of windows each event contributes to, which raises state and cost, so keep the slide as large as the smoothing requires.
-- Sliding 5-minute window, sliding every 1 minute: rolling totals.
SELECT
window_start, window_end,
COUNT(*) AS events,
SUM(amount_cents) AS rolling_total
FROM TABLE(
HOP(TABLE purchases, DESCRIPTOR(event_time), INTERVAL '1' MINUTE, INTERVAL '5' MINUTE)
)
GROUP BY window_start, window_end;
-- each event appears in ~5 windows; more windows = more state and cost
Session Windows
A session window groups activity separated by a gap of inactivity: a user’s clicks within thirty minutes of each other form one session, and a pause longer than the gap starts a new one. Sessions have no fixed boundaries; they are defined by the gap, and their length varies with behaviour.
Session windows are the right model for user behaviour, where the unit is a visit, not a clock interval. They are stateful because a session is open until the gap passes, so a long idle period delays the session’s completion and the watermark must account for the gap. The result is a per-session aggregate: events, duration, pages viewed.
-- Session window: activity grouped by a 30-minute inactivity gap.
SELECT
user_id,
session_start, session_end,
COUNT(*) AS events,
TIMESTAMPDIFF(MINUTE, session_start, session_end) AS duration_min
FROM TABLE(
SESSION(TABLE clicks PARTITION BY user_id, DESCRIPTOR(event_time), INTERVAL '30' MINUTE)
)
GROUP BY user_id, session_start, session_end;
-- a gap > 30m closes the session; watermark must account for the gap
Watermarks and Allowed Lateness
The watermark trails the maximum observed event time by an allowed delay chosen from the observed lateness distribution. A larger delay waits longer for stragglers, raising accuracy; a smaller delay emits sooner, raising latency pressure but dropping more late events. Set it from data, not a guess.
After the watermark, an additional allowed-lateness window keeps a window updatable for a further period, so an event that arrives just after the watermark can still retract and correct the emitted result. Events later than that go to a side output rather than being silently dropped, so the loss is visible.
# watermark + allowed lateness
watermark = max_event_time_seen - allowed_delay
allowed lateness = extra window after the watermark for late corrections
too-late events = beyond allowed lateness -> side output (not dropped)
# larger allowed_delay => more accuracy, more latency
# choose from the observed lateness distribution, revisit when sources change
Emitting Results: Append Versus Update
A window can emit once, as an append-only result, or emit early and update as more events arrive, as a retractable result. Append-only is simpler for consumers but only correct once the watermark has passed; updating results are correct earlier and converge, but consumers must understand that a total can change.
Decide the semantics explicitly and document it: which streams are append-only and which are retractable. A consumer that treats a retractable result as append-only double counts when the window updates. The safest default for a consumer is to key updates on the window and upsert, so an update replaces rather than duplicates.
# emit semantics
append-only: emit once, after the watermark; simple for consumers
retractable: emit early + update on lateness; correct sooner, changes over time
# consumer rule: key the sink on (window_start, window_end) and UPSERT
# => updates replace; duplicates avoided; late corrections applied
Common Pitfalls and Conclusion
Do not window by processing time and expect reproducibility. Do not set the watermark delay without measuring lateness. Do not drop late events silently. Do not use a tiny slide with a large size and then wonder why state is huge. And do not let a consumer treat an updating result as append-only.
Event-time windowing groups events by when they happened, making aggregates reproducible; tumbling windows suit periodic totals, sliding windows suit rolling metrics, and session windows suit user behaviour. Watermarks close windows, allowed lateness corrects them, and side outputs keep the loss visible. Choose the window from the question and the watermark from the data, then a streaming aggregate means the same thing on every replay.
Validate the window output against a batch recomputation before trusting it in production.
Sources: Flink: Windows, Beam: Windowing and triggers, Flink: Generating watermarks.
FAQ
- Tumbling versus sliding windows?
- Tumbling windows are fixed and non-overlapping, each event in one window, good for periodic totals. Sliding windows overlap, each event in several windows, good for rolling metrics. Use the largest slide that still smooths enough, because a small slide multiplies state and cost.
- What happens to events later than the watermark?
- Within the allowed lateness they retract and correct the window result; beyond it they are "too late" and should go to a side output rather than being dropped, so you can count, inspect and reprocess them and see that lateness assumptions failed.
Report a problem with this articleOpens a pre-filled issue on GitHub.