Skip to content

Batch & stream processing · 2 of 3

Late events and windows

Step 1 · The pattern

Count orders per minute

A stream processor counts orders per minute for a live dashboard. It puts each event in the minute it arrives. Every phone has signal, every event arrives within a moment of happening, so arrival time and event time agree. (Time is compressed: a minute here is one second.)

  • an event
  • right minute

In short

Late events and windows: how it works and how it breaks

A stream processor that counts things per minute has to decide which minute each event belongs to. The easy answer is the minute it arrived. But events arrive late: a phone in a tunnel keeps its events and sends them when it reconnects, a service retries after a timeout, a partition falls behind. Counted by arrival time, those events land in the wrong minute, so the dashboard shows a dip while they are missing and a spike when they all arrive, and alerts fire on both. Count by event time instead, the timestamp the event carries, and keep each minute's window open for a while after it ends so stragglers can join. A watermark tracks how far event time has progressed. The trade is delay: a minute's result is published only when its window closes. Events later than that go to a corrections output, not into silence.

What this simulation simplifies

  • A minute is one second here, and late events arrive about two minutes late; real lateness varies from milliseconds to days.
  • The processor here knows exactly which events are late; a real one only knows timestamps and must estimate progress with a watermark.

How this comes up in interviews

"You count events per minute in a stream processor. How do you handle events that arrive late?"

Then they ask

  1. What is a watermark, and how would you set its margin?
  2. What happens to events later than that?
  3. What does waiting for late events cost?

A strong answer covers

  • Count by event time, the timestamp the event carries, not by arrival time.
  • Keep each window open for an allowed lateness; a watermark tracks event-time progress.
  • Publish early estimates and update them, or publish once the window closes.
  • Send events later than the allowed lateness to a corrections output instead of dropping them.
  • The trade is latency: results come later, so set the margin from measured lateness.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last updated: Official documentation

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.