Stream processing and event time
Define window completeness, watermark behavior, state retention, and sink recovery for out-of-order event streams.
On this page
The question for today
What you’ll understand
- Distinguish a watermark progress claim from a guarantee of complete data.
- Trace late events through an explicit window correction policy.
- Connect checkpointed state and source positions to sink-side delivery semantics.
A purchase occurs at 11:59 but reaches an analytics job at 12:04. An hourly revenue chart cannot answer what to do with it until the system defines which clock determines membership and when a result can be revised. Streaming correctness is therefore partly a product contract. Low delay, bounded state, and acceptance of arbitrarily late information cannot all be obtained without tradeoffs. Make those tradeoffs visible in the output rather than hiding them behind a successful job status.
Assign a time and a window deliberately
Event time belongs to the event's domain, while processing time is when an operator handles it. An event-time window places events using the former even after reordering or replay. Define time-zone treatment, clock quality, and timestamp validation at ingestion. A device with a wildly future timestamp can distort a naive maximum-seen timestamp policy. A stable business event identifier is separately needed to detect duplicate deliveries; assigning the same time to two events does not make them duplicates.
For tumbling windows, use an explicit half-open interval so a boundary event has one home. A 12:00 event belongs to the next hour when the preceding hour ends at 12:00 exclusively. Aggregates may be incremental, but some operations require retaining more than a count. Distinct users, joins, and corrections need identities or sufficient reversible state. Retention determines which late updates remain computable.
Treat watermarks as declared progress
A watermark represents an operator's view of event-time progress. A common bounded-out-of-orderness strategy trails the maximum observed timestamp by a configured delay. It does not prove that a disconnected device has no older event. In multi-input processing, progress is generally constrained by the slowest active input. Idle-input detection can allow progress to continue, but when that input returns its old records need the same explicit lateness policy as any other delayed arrival.
Choose whether a window emits an early estimate, a watermark-triggered result, and later revisions. A late record can update a retained window, go to a correction stream, or be rejected after a specified horizon. Declare replacement versus delta semantics; summing full totals double-counts. Monitor lateness, watermark lag, and backlog separately; throughput alone cannot establish completeness.
Recover state and output as one logical story
A checkpoint records compatible operator state and source positions so a failed job can restore and replay. That alone does not prevent an external sink from seeing the same effect twice: the job can write a result and crash before recording progress. End-to-end guarantees require a sink protocol, such as a transactional commit coordinated with checkpoints or idempotent writes keyed by window and revision. An email or HTTP action cannot simply be assumed to participate in a database transaction.
State retention grows with the number of live keys and windows, including allowed lateness. Estimate it before enabling a generous correction horizon. Account for checkpoint copies, serialization overhead, and in-flight records separately. Test replay across the exact failure gap between sink acknowledgement and checkpoint completion. After restart, verify the corrected hour, not merely resumed consumption.
Work it through
Process an hour after its first result
The [11:00,12:00) window has revenue 900. A 30-unit purchase with event time 11:59 arrives at processing time 12:04. The watermark is 12:02 and retained windows accept corrections until event-time progress passes window end plus ten minutes.
The event belongs to the earlier hour because 11:59 lies in [11:00,12:00), regardless of the 12:04 arrival clock.
The watermark has already crossed 12:00, so the original 900 result may have been emitted. The event is late relative to that progress.
The retention boundary is 12:10; watermark 12:02 has not crossed it, so the declared policy can revise the retained total to 930.
Emit either replacement total 930 with a revision identity or delta +30 under a declared sink contract. Emitting a new full total as another additive contribution would incorrectly produce 1,830.
The correct final total is 930 under this stated correction policy; arrival time alone cannot determine the answer.
The living notebook
Explore the model
Interactive lab · queue
Compare consumption capacity with time completeness
Vary event arrival λ and processing μ in the fluid backlog max(0,4+(λ−μ)t). The separate M/M/1 residence-time figure assumes Poisson arrivals and exponential service. The lab contains no timestamps, windows, watermarks, or checkpoint state: use its backlog only to contrast throughput progress with event-time completeness.
- Fluid backlog
- 4
- Net rate · λ − μ
- −1 /s
- M/M/1 mean residence
- 1 s
Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA drained processing queue does not establish that an event-time window can never receive a correction.
Pause & reason
Check your understanding
Your turn
Try it yourself
Estimate retained aggregate state for 200,000 active keys, 80 bytes per key-window, five-minute windows, and a 20-minute active-plus-lateness horizon. What important costs does the estimate exclude?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Find the number of simultaneously retained windows, then multiply by keys and bytes per aggregate.
Compare with the solution
Twenty minutes divided by five gives four retained windows. State is approximately 200,000 × 80 × 4 = 64,000,000 bytes, or 64 decimal MB. This omits data-structure overhead, keys, checkpoint copies, serialization buffers, and any event-level state needed for exact distinct counts or joins. Real boundary behavior can retain an additional window, so measure the implementation and timing policy.
Keep the idea
Take it with you
- Window membership, emission, and late correction are distinct decisions.
- Watermarks express progress assumptions and can be limited by slow or idle inputs.
- Checkpoint recovery needs a compatible sink protocol for end-to-end effect correctness.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.