Topic 124 / Advanced
Stream processing: windows, late events, replay and reprocessing
Chapter 7.5 · Jobs, Workflows, Batch and Streams
What and why
Batch processing (→ 123) needs a bounded dataset and accepts latency measured in minutes to hours; some use cases — fraud alerts, live dashboards, real-time aggregates — need continuous processing of data as it arrives, with no "end of the batch" to wait for. Stream processing runs continuous computation over an unbounded sequence of events, from stateless transforms (map, filter — one event in, one event out, no memory of prior events) to stateful operations (aggregations and joins that must remember something across events, usually within a window). Builds on: → 111 (Kafka architecture — a log with retention and offsets is what makes a stream processable and replayable).
Diagram
| Window type | Shape |
|---|---|
| Tumbling | Fixed, non-overlapping (0–5 min, 5–10 min, …) |
| Sliding | Fixed size, overlapping (every minute, over the last 5 minutes) |
| Session | Closes after a gap of inactivity — length varies per session |
event time: 10:00:58 (event happened)
processing time: 10:01:12 (processor saw it, 14s later)
window [10:00–10:01] already closed if the watermark tolerated only 5s of lateness
How it works
- Stateless transforms (filter invalid events, reshape a field) process each event independently and need no memory — the simplest, most scalable kind.
- Stateful operations (count events per user per minute, join two streams by key) must accumulate state across events, scoped to a window so state doesn't grow forever.
- Event time (when the event actually happened, carried in its payload) differs from processing time (when the stream processor saw it) — network delay and retries mean these routinely diverge, and correctness usually requires grouping by event time.
- A late event — one whose event time belongs to a window that, by processing time, has already closed — needs an explicit policy: a watermark (a heuristic estimate of how late an event can realistically be) decides how long a window stays open before finalizing, trading completeness against latency; events later than the watermark are dropped, counted into a correction, or routed to a side output.
- Because the underlying log retains history and tracks offsets (→ 111), replay and reprocessing — resetting the job to an earlier offset and re-running it, typically after a bug fix in the processing logic — is a first-class recovery mechanism, not a special case.
Practical example
(illustrative). A 1-minute tumbling window counting "logins per minute" closes its 10:00–10:01 window and emits a count of 500 once its watermark (set to tolerate up to 5 seconds of lateness) passes. An event timestamped 10:00:58 that arrives at the processor at 10:01:12 — 14 seconds late — exceeds the 5-second watermark and is routed to a late-events output instead of silently corrupting the already-emitted count of 500.
When to use it
- Use when the business value of a result depends on arriving within seconds, not minutes or hours — fraud detection, live metrics, real-time personalization.
- Avoid when periodic, bounded processing is accurate enough and simpler to reason about and test — most reporting needs are batch (→ 123), not streaming.
- Failure scenarios: no watermark policy at all, so either every window waits forever for stragglers or every late event is silently dropped with no visibility into how much data that is; treating processing time as event time, which looks correct in testing (near-zero delay) and silently misattributes events to the wrong window under real network jitter in production.
- Simpler alternative: if a few minutes of lag is actually acceptable, a short-interval batch job (→ 123) run every few minutes avoids watermark and late-event complexity entirely, for a small latency cost.
Practice
(mechanism; 25–30 min, lab Kafka). Implement a 1-minute tumbling window that counts events by key from a Kafka topic, using each event's embedded timestamp — not arrival time — to assign it to a window. Inject a handful of artificially late events (timestamped into an already-closed window) and confirm the watermark policy corrects or visibly reports them rather than silently dropping them unnoticed. Then reset the consumer group's offset and reprocess the same input, confirming identical output. Done when late events are visibly handled, not silently lost, and a full replay reproduces identical counts.
Shared lab setupKey takeaway
Stream processing trades batch's simplicity for near-real-time results, and needs an explicit, visible policy for lateness — replay from the retained log is what makes correcting a bug in that policy recoverable rather than catastrophic. Done when you can: choose a window type for a given aggregation and state your watermark trade-off in one sentence. Used next in → 129 (consistency models govern what a stream consumer can assume about staleness); Project 19.
References
Designing Data-Intensive Applications — see references.md.
Source directory