31. Event-driven pipelines: handlers that are safe to retry
In an event-driven design, work starts when a business event happens: the event is routed to the handlers that care, and each does its part. Delivery can happen more than once, so every handler must be safe to run again on the same event: idempotent, usually by remembering a deduplication key.
The clip routes each ClaimReported event to two handlers: open the claim file and send the acknowledgement letter. Delivery is at least once: two acknowledgements to the broker were lost, so E02 and E06 are delivered again, twelve deliveries for ten events. A handler that remembers nothing sends twelve letters; one that remembers the delivery's id still sends twelve, because a redelivery has a new id; one that remembers the event's own id sends ten and skips the two repeats.
The intuition: a doorbell, and a note on the door
When a customer reports a claim, Harbour Mutual must open a claim file and post an acknowledgement letter. Nobody should have to check every hour whether there is new work: the event itself is the doorbell, and the right people answer it.
Doorbells sometimes ring twice. The broker that delivers the events was not told the first delivery arrived, so it delivers again, as it should. A clerk who posts a letter every time the bell rings will post two letters to the same customer. A careful clerk keeps a note of the claims already answered, and checks it before posting.
How it works, step by step
A business event records something that happened ("claim C-202 reported") in the language of the business, with its own id, type and time. It is published to a broker, which routes it by type to the handlers subscribed to that type. Here ClaimReported goes to two handlers.
Brokers deliver at least once: a handler acknowledges each delivery, and when the acknowledgement is lost (a timeout, a restart) the broker delivers again. Exactly-once delivery across a network is not on offer; what can be made exactly once is the effect (page 23).
A handler is idempotent when handling the same event twice has the same effect as handling it once. The usual way is a deduplication key: the handler records the key of every event it has handled, in the same transaction as its effect, and skips any delivery whose key it has seen.
The key must identify the event, not the delivery. A redelivery carries a new delivery id, so a handler keyed on the delivery id treats it as new. The event's own id (or a business key such as the claim and event type together) is the same in every delivery.
| Key | Letters | Repeats skipped | Duplicate letters |
|---|---|---|---|
| No key | 12 | 0 | 2 |
| The delivery's id | 12 | 0 | 2 |
| The event's id | 10 | 2 | 0 |
By hand
Route the ten events to the letter handler. The acknowledgements of E02 and E06 were lost, so the broker delivers them again.
| 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 | – |
- How many deliveries reach the handler?
Predict the result, then check it
12: ten first deliveries and 2 repeats. - The handler remembers nothing. How many letters are posted?
Predict the result, then check it
12: two customers get two letters. - It remembers each delivery's id. How many letters now?
Predict the result, then check it
Still 12: the repeats came with new delivery ids. - It remembers each event's id. How many letters, and how many deliveries skipped?
Predict the result, then check it
10 letters; 2 skipped.
What you found: Only a key that belongs to the event makes the handler idempotent: 10 letters for 10 claims.
- Give every event its own id
- Without it: No handler can tell a repeat from a new event.
- Record the key in the same transaction as the effect
- Without it: A crash between the two leaves the letter sent and the key unrecorded, and the next delivery sends it again.
- Route by event type
- Without it: Every handler receives every event and must filter, and a new event type breaks them.
- Retry, then dead-letter
- Without it: A handler that cannot process one event either loses it or stops for good.
- Our broker guarantees exactly-once delivery, so duplicates cannot happen.
- In fact: Delivery can repeat whenever an acknowledgement is lost; here 2 of 12 deliveries were repeats.
- Any id will do as a deduplication key.
- In fact: The delivery's id changes on every redelivery: keyed on it, the handler still posts 12 letters.
- Idempotent means the handler runs only once.
- In fact: It may run many times; the effect happens once.
When it fits, and when it does not
- Fits: work that should start when something happens (a letter, a file, a payment instruction), with handlers owned by different teams.
- Does not fit: aggregates over many events (a stream or batch job does those), or work that must never be repeated and cannot be made idempotent; then a human or a transactional system must guard it.
Design notes
Never replay a log into handlers with side effects in the world: a replay would post every letter again. Keep replays for derived data (pages 26 and 29), and keep handlers idempotent even so. Version event types under a contract, and route unknown types to a dead-letter queue rather than dropping them.
Why it matters
Event-driven designs let teams add a new reaction to a claim without changing the claims system. They also turn every handler into code that must be safe to run twice, which is the one property reviewers most often forget to check.
On Azure (checked against Microsoft Learn on 24 October 2026)
- Azure Event Grid delivers each event at least once to every matching subscription, does not guarantee order, retries with exponential back-off for up to 24 hours by default, and can dead-letter to a storage account.
- Azure Service Bus can drop a resent message with a
MessageIdit has seen within a duplicate-detection window (10 minutes by default, up to 7 days) on the Standard and Premium tiers; it protects the queue, not the handler's own effects. - Azure Functions handles the events; Microsoft's guidance is to design functions to be idempotent, because triggers deliver at least once.
Where it goes wrong
- Duplicate letters. A handler with no deduplication key acts on every repeat.
- The wrong key. The delivery id or the arrival time changes with every redelivery.
- Key and effect apart. The key is recorded after the effect, and a crash in between repeats it.
- A replay into the world. Replaying the log re-sends every letter.
- Explain why delivery can repeat and why the effect must not.
- Choose a deduplication key that belongs to the event, not the delivery.
- Count a handler's effects with and without idempotency, by hand.
- Say why side-effecting handlers are never fed a replay.
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.
- G. Hohpe and B. Woolf, Enterprise Integration Patterns (Addison-Wesley, 2003)Event messages, routers and the idempotent receiver.
- Microsoft Learn: Azure Event Grid delivery and retryAt-least-once delivery, the retry schedule and dead-lettering.
- Microsoft Learn: Azure Service Bus duplicate message detectionMessageId, the detection window and the tiers that support it.
Practise it
The chapters that put this theory to work on Harbour Mutual and Kestrel:
- Chapter 29: Integration patterns: batch, streaming, APIs and sharing
- 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.
Terms taught here: Event-drivenIdempotentDeduplication keyAt-least-once delivery