Theory · Page 26 of 34 · Pipeline patterns

26. Event-at-a-time streaming: state, event time and backpressure

An event-at-a-time stream job handles each event the moment it arrives, incrementally, and keeps whatever it must remember between events as state. It gives the lowest latency, and asks three things of its designer: manage the state, group by event time rather than arrival, and push back when events come faster than the job can take them.

Ten claims, one by one
Playback speed

The clip reads each of the ten claim events as it reaches the log and handles it in thirty seconds. At 09:15 three claims arrive within fifteen seconds: the job takes one, holds one in its buffer, and the third waits in the log until there is room, which is backpressure; nothing is lost. The job keeps a running total for each ten-minute window of event time; when the watermark passes 09:10 the first window is emitted and its state freed. A claim reported at 09:12 and delivered at 09:21 still counts in its own window. Grouped by arrival instead, the same claims give different totals.

The intuition: a cashier, not a nightly count

Harbour Mutual's operations dashboard shows the value of claims reported in each ten minutes, and the fraud team wants each claim looked at while the customer is still on the portal. Waiting for a batch is not an option: each claim must be handled as it comes, like a cashier serving one customer after another rather than counting the till at night.

A cashier has three problems a night count does not. She must remember things between customers (the running total), she must decide what time a purchase belongs to when the receipt is handed in late, and when a coach party arrives at once she needs the queue to wait rather than push past. Those are the three design notes of streaming: state, event time and backpressure.

Remember Process each event as it arrives; keep the state checkpointed, group by event time, and let a full consumer leave events in the log.

How it works, step by step

The job reads a durable, ordered log (page 22) from its last offset. For each event it runs its logic once, incrementally: it updates what it already knows instead of recomputing from everything. Here each event takes the job 30 seconds, because it calls the policy system.

What the job remembers between events is its state: here, a running total for every ten-minute window that is still open. State is what makes a stream job more than a filter, and it must survive a crash, so the job writes it to a checkpoint together with the offset it has read up to. After a restart it reloads both and carries on, so no event is counted twice or missed.

Windows are by event time, when the claim was reported, not processing time, when it reached the job (page 23). A watermark, the latest event time seen less an allowed lateness (here five minutes), decides when a window is complete: once the watermark passes the window's end, the window's total is emitted and its state freed. An event for a window already closed is late, and dropped or corrected.

When events arrive faster than the job handles them, a queue forms. Backpressure is how the system copes without losing anything: the job holds only what fits in its buffer (here 1 event) and stops reading; the rest waits in the log, which is durable, until the job has room. The alternative, an unbounded queue in memory, ends with the job running out of memory and losing everything it held.

Event time and processing time Event time is when it happened in the world; processing time is when the system saw it. Windows by processing time describe the network's delays; windows by event time describe the claims. Every stream job chooses one, explicitly or by accident.

By hand

Run the ten events through the job by hand: thirty seconds each, a buffer of one, ten-minute windows by event time and a watermark five minutes behind the latest event time.

The ten events, in the order they reach the log
EventClaimPolicyReportedReached the logAmountNote
E01C-201P-100109:01:4009:02:10€1,200–
E02C-202P-100209:0409:04:50€2,400–
E03C-203P-100309:06:3009:07:45£800health details
E04C-204P-999909:0909:09:40€650–
E06C-206P-100609:14:2009:15:05€950–
E07C-207P-100709:14:5009:15:12€4,200–
E08C-208P-100809:1509:15:20€700–
E09C-209P-100909:18:3009:19:10€1,500–
E05C-205P-100509:12:1009:21:30€3,100–
E10C-210P-101009:2409:25:40€2,600–
  1. E07 arrives at 09:15:12 while E06 is still being handled. When does it start, and when is it done?
    Predict the result, then check it It waits in the buffer until 09:15:35, and is done at 09:16:05: 53 seconds after it arrived.
  2. E08 arrives at 09:15:20: the job is busy and its buffer is full. Where does it wait, and when is it done?
    Predict the result, then check it In the log, until 09:15:35; it is done at 09:16:35, 75 seconds after it arrived.
  3. After E08, the latest event time seen is 09:15. Where is the watermark, and which window closes?
    Predict the result, then check it At 09:10: the window 09:00–09:10 is emitted, with €4,520.
  4. E05 was reported at 09:12:10 and arrives at 09:21:30. The watermark is 09:13:30. Does it count, and in which window?
    Predict the result, then check it Yes: the watermark has not passed 09:20, so it counts in the window 09:10–09:20.
  5. What are the three windows' totals by event time, and by processing time?
    Predict the result, then check it By event time €4,520, €10,450 and €2,600; by arrival €4,520, €7,350 and €5,700.

What you found: Each claim is ready about half a minute after it arrives (36.8 seconds on average, 75 at most in the burst); the state holds the open windows, and only event time gives totals that describe the claims.

Why this step
Read from a durable log with offsets
Without it: A crashed job cannot know where it was, and events are lost or counted twice.
Checkpoint the state with the offset
Without it: After a restart the totals start from zero, or count again what was already counted.
Window by event time with a watermark
Without it: Totals move with the network's delays, and a window never knows when it is complete.
Bound the buffer and leave the rest in the log
Without it: A burst fills memory until the job falls over and loses what it held.
People often think…
Streaming means there is no state.
In fact: Every aggregate is state. At the end of the morning the job still holds 2 open windows.
Backpressure loses data.
In fact: It delays it. In the burst, 1 event waited in the log and was still handled, 75 seconds after it arrived.
Late events are always dropped.
In fact: Only when their window has closed. E05 arrived nine minutes after it happened and still counted.

When it fits, and when it does not

  • Fits: decisions needed in seconds (fraud scoring while the customer waits, alerts), and running aggregates that must always be current.
  • Does not fit: figures read once a day, sinks that throttle single writes (group events first, page 27), and teams that cannot run a job that never stops.

Design notes

Partition the log by a key the state is kept by (the customer for fraud features), so one customer's events reach one instance in order. Size the allowed lateness by measuring how late events really are. Watch the job's lag, how far its offset is behind the end of the log: it is the first sign that it cannot keep up.

Why it matters

Streaming is chosen for its latency and lived with for its state. A team that knows where its state lives, how its watermark is set and what happens in a burst runs a stream job calmly; a team that does not finds out on the busiest day of the year.

On Azure (checked against Microsoft Learn on 24 October 2026)

  • Azure Event Hubs is the log: events are kept up to 7 days on the Standard tier and 90 on Premium and Dedicated, read by partition from an offset.
  • Azure Databricks runs Structured Streaming with a checkpoint; for sub-second latency it offers a real-time mode, and it does not support Spark's experimental continuous processing.
  • Azure Functions with an Event Hubs trigger handles events as they arrive and saves a checkpoint per partition; it guarantees at-least-once delivery, so the code must cope with an event handled twice.

Where it goes wrong

  • State in memory only. A restart forgets every open window.
  • Windows by arrival. The dashboard's totals change when the network is slow.
  • An unbounded queue. A burst becomes an outage.
  • Lag nobody watches. The job falls an hour behind and the "real-time" dashboard is an hour old.
You can now…
  • Explain what state a stream job keeps and why it is checkpointed with the offset.
  • Place a late event in its window by the watermark, by hand.
  • Explain backpressure and why it delays events rather than losing them.
  • Say when event-at-a-time processing is worth its cost.

Check yourself

Choose an answer to each question, then press Check.

1. In the burst at 09:15, how long after arriving is the third claim done?
2. What does the job write to its checkpoint?
3. What happens to an event the job has no room for?
Answer every question first.

Sources

The works this page draws on, summarised in its own words rather than quoted. Read them for the full argument.

  • T. Akidau, S. Chernyak and R. Lax, Streaming Systems (O'Reilly, 2018)Event time, watermarks, state and consistent checkpoints.
  • M. Kleppmann, Designing Data-Intensive Applications (O'Reilly, 2017), chapter 11Stream processing, logs, state and fault tolerance.
  • Microsoft Learn: Azure Functions reliable event processingCheckpoints per partition and at-least-once processing with the Event Hubs trigger.

Practise it

The chapters that put this theory to work on Harbour Mutual and Kestrel:

The tools to try it in:

  • Pipeline patterns lab · Run the same claim events through eight pipeline patterns and see latency, overhead, a consumer failing, a delivery repeated and a logic change replayed.
  • Stream lab · Run one claims feed as batch, Lambda and Kappa side by side, inject a late correction and replay the log.

Terms taught here: BackpressureCheckpoint

An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Cloud Data Architecture Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.