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.
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.
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.
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.
| Event | Claim | Policy | Reported | Reached the log | Amount | Note |
|---|---|---|---|---|---|---|
| E01 | C-201 | P-1001 | 09:01:40 | 09:02:10 | €1,200 | – |
| E02 | C-202 | P-1002 | 09:04 | 09:04:50 | €2,400 | – |
| E03 | C-203 | P-1003 | 09:06:30 | 09:07:45 | £800 | health details |
| E04 | C-204 | P-9999 | 09:09 | 09:09:40 | €650 | – |
| E06 | C-206 | P-1006 | 09:14:20 | 09:15:05 | €950 | – |
| E07 | C-207 | P-1007 | 09:14:50 | 09:15:12 | €4,200 | – |
| E08 | C-208 | P-1008 | 09:15 | 09:15:20 | €700 | – |
| E09 | C-209 | P-1009 | 09:18:30 | 09:19:10 | €1,500 | – |
| E05 | C-205 | P-1005 | 09:12:10 | 09:21:30 | €3,100 | – |
| E10 | C-210 | P-1010 | 09:24 | 09:25:40 | €2,600 | – |
- 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. - 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. - 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. - 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. - 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.
- 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.
- 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.
- 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.
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:
- Chapter 30: Lambda and Kappa: architectures for streaming
- Chapter 31: Pipeline patterns: choosing and combining
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