How to Handle Late-Arriving Events in Streams
Process out-of-order and late-arriving events with event-time windows, watermarks and allowed lateness so aggregations stay accurate.
On this page
Introduction
In a batch world every record is present before you compute. In a streaming world events arrive out of order and late: a mobile client batches uploads, a Kafka partition lags, a retry delivers an old message after a newer one. If you aggregate by arrival time, the numbers are simply wrong, and they keep changing as stragglers show up. Event-time processing fixes this by basing windows on the time the event happened rather than when it arrived, and by using watermarks to decide when a window is complete enough to emit.
Event Time vs Processing Time
Every event needs a timestamp that reflects when it occurred at the source, not when your system saw it. Processing-time aggregation is easy but unstable: the same query over the same data returns different results depending on network delays. Event-time aggregation is deterministic given the data, because the window boundaries come from the events themselves.
The gap between event time and processing time is the source of lateness. Record both timestamps: event_time for windowing, and ingest_time for monitoring how far behind the stream is. A growing gap between the max event time and the max ingest time is the signal that a producer or partition is lagging, which is a separate operational problem from out-of-order arrival.
{
"event_id": "evt_9f2c",
"type": "purchase",
"event_time": "2026-10-23T10:02:11.482Z",
"ingest_time": "2026-10-23T10:02:39.910Z",
"amount_cents": 1999
}
// event_time drives windowing; ingest_time monitors lag
Watermarks and the Lateness Horizon
A watermark is a claim that no event with a timestamp earlier than it will arrive. It lets the engine close a window and emit a result. A perfect watermark is impossible over an unbounded source, so practical systems use a bounded delay: the watermark trails the maximum observed event time by a configured amount, and events older than the watermark are considered late.
The watermark delay is a trade-off. A large delay waits longer for stragglers, improving accuracy at the cost of latency. A small delay emits quickly but drops more late events. Choose it from the observed distribution of (max_event_time - event_time) in your stream, and revisit it when the data sources change.
# Watermark = max_event_time_seen - allowed_delay
#
# observed lateness histogram (example)
# <= 5s 98.2%
# <= 30s 99.6%
# <= 5m 99.94%
# > 5m 0.06%
#
# choose allowed_delay = 30s: good accuracy, low added latency
# events older than the watermark: too_late, routed to a side output
Allowed Lateness and Side Outputs
Rather than dropping everything after the watermark, most engines let you keep a window open for a further “allowed lateness” period. A late event within that period retracts or updates the window result, so the emitted answer converges toward the correct one. Events later than that are too late for the window and go to a side output where you can inspect, alert on, or reprocess them.
Decide the semantics of a retraction explicitly. An aggregation that emits an updated total must be understood downstream as retractable: a sum that goes down is normal here, not a bug. Document which outputs are append-only and which are retractable, because consumers that assume append-only will double count.
-- Tumbling 1-minute event-time window with 30s allowed lateness.
SELECT
window_start,
window_end,
COUNT(*) AS events,
SUM(amount_cents) AS total_cents
FROM TABLE(
TUMBLE(TABLE purchases, DESCRIPTOR(event_time), INTERVAL '1' MINUTE)
)
GROUP BY window_start, window_end;
-- late events within allowed lateness update window_start/window_end rows
Idempotent Sinks and Upserts
Because late events change already-emitted results, the sink must accept updates. Write results keyed by (window_start, window_end) and upsert, rather than appending a new row per emission, or the same window will appear many times with different totals. This is the same idempotent-load discipline used in batch pipelines, applied to a stream.
If the sink is append-only, you must emit a changelog: an insert, then retract-and-insert pairs for each update. Downstream state must apply those operations in order. Most data lakes handle this with a merge or a delete-insert keyed on the window, which is why a primary key on the window is worth designing in from the start.
-- Upsert window results by their window key so late events update,
-- not duplicate, the emitted row.
INSERT INTO minute_totals (window_start, window_end, events, total_cents)
VALUES ($1, $2, $3, $4)
ON CONFLICT (window_start, window_end)
DO UPDATE SET events = EXCLUDED.events,
total_cents = EXCLUDED.total_cents;
Reprocessing and the Batch Reconciliation
Streaming results should agree with a batch recomputation over the same event-time range. Run a daily batch job that recomputes closed windows from the raw event log and compares to the streaming output. Differences reveal dropped late events, watermark settings that are too aggressive, or duplicate delivery.
Keep the raw events, or a compacted form of them, long enough to reprocess the windows that matter. The raw log is the source of truth; streaming results are a materialised view you can rebuild. When a comparison fails, the batch result usually wins, and you fix the streaming configuration rather than patching the number.
Monitoring Lateness
Emit a histogram of event lateness (max event time minus event time) and a counter of events that fall outside allowed lateness. A growing too-late counter means producers are buffering longer or a partition is stuck, and it tells you to raise allowed lateness or fix the producer. Also monitor the watermark itself: a watermark that stops advancing means the stream is blocked.
Alert on the reconciliation gap between streaming and batch totals. A gap that is normally zero and suddenly is not is the clearest possible signal that lateness handling has broken, and it is far easier to act on than staring at per-partition lag numbers.
Common Pitfalls and Conclusion
Do not window by processing time and hope lateness is small; it is not deterministic and it drifts with load. Do not set the watermark delay to zero, or every straggler becomes a dropped event. Do not append every window update to an append-only sink, which double counts. And do not drop late events silently; route them to a side output so the loss is visible and measurable.
Handling late events is about committing to event time, bounding lateness with a watermark and an allowed-lateness window, writing results idempotently so updates do not duplicate, and reconciling with batch. Do that and out-of-order arrival stops being a source of wrong numbers and becomes a monitored, tunable property of the pipeline.
Check the reconciliation gap after each change to confirm the streaming windows still match the batch recomputation.
Sources: Flink: Timely stream processing, Beam: Event-time and watermarks, The Dataflow Model (Akidau et al.).
FAQ
- How do I choose the watermark delay?
- From the observed lateness distribution in your stream. Pick a delay that covers the bulk of events (for example 99.9%) while keeping added latency acceptable, and revisit it when producers or network paths change.
- What should happen to events that arrive after allowed lateness?
- Route them to a side output rather than dropping them silently. Side outputs let you count, inspect and reprocess too-late events, and a growing count is an operational signal that lateness assumptions no longer hold.
Report a problem with this articleOpens a pre-filled issue on GitHub.