How it works · 21 of 49 Advanced
What is the transactional outbox pattern?
Save and announce, both or neither
Outbox: A service that saves a change and then sends a message about it is doing two writes to two systems; a crash between them leaves the database saying yes while nobody else ever hears (or, sending first, others act on a change that was never saved). The outbox writes the message into an outbox table in the *same* database transaction as the change, so both are kept or neither is; a separate relay reads the table and publishes. The price: a message can be sent twice (the relay crashes after sending, before marking it sent), so every consumer must ignore repeats by message id.
Loading the animation…
Say it like this The outbox writes the message into the same database transaction as the change and lets a relay publish it later, so a change is announced if and only if it is saved; the price is that messages can arrive twice, so consumers must ignore repeats.
Under the hood — for builders
- The sources, in their words
- Chris Richardson ("Pattern: Transactional outbox", microservices.io): "How to atomically update the database and send messages to a message broker?"; "Messages are guaranteed to be sent if and only if the database transaction commits." Microsoft ("Transactional Outbox pattern", Azure Architecture Center, 2026): "When you save the business object and its events within the same database transaction, the system guarantees no data loss."
- Where the gap is
- Transaction: a group of database writes kept all together or not at all. Relay: a separate worker that reads the outbox table and publishes each row. Microsoft lists why the send fails after the save: network error, message service outage, host failure.
- Run here: lost notes and repeats
- Idempotent consumer: one that gives the same result if a message arrives twice, here by keeping the ids of notes already handled (Richardson's suggestion). 10,000 orders, a 1% chance of a crash at each gap:
- Running the relay
- Microsoft's Cosmos DB version replaces polling with the change feed, and lets Service Bus drop repeats by message id.
- Order matters
- Microsoft: "You must preserve event order so that the system publishes an OrderCreated event before an OrderUpdated event." One relay per order stream, sessions by key.
- Measure it
- Rows waiting in the outbox · age of the oldest unsent row · repeats dropped by consumers · relay restarts.
- Watch out
- Richardson: "the developer might forget to publish the message/event after updating the database." Clean old rows out; Microsoft keeps them about 10 days.
- Sources
- Chris Richardson, "Pattern: Transactional outbox", microservices.io · Microsoft, "Implement the Transactional Outbox pattern by using Azure Cosmos DB", Azure Architecture Center, 2026 · both read in session · SQLite crash run, Python, 19 Nov 2026.
What happens in the outbox animation, step by step?
- The shop saves the order, then sends a note to the warehouse: two separate steps.
- Order #4471 is saved. The power cuts before the note leaves, and nothing remembers it was due.
- Your turn: three days later, what does the customer see? Tap an answer.
- Confirmed and paid, and no parcel, ever. Sending first is no better: the warehouse packs an order that was never saved.
- The fix: write the note into the same drawer, in the same save. Both go in, or neither, so a power cut loses nothing.
- A postman empties the drawer. He may deliver twice, so the warehouse ignores a note it has seen. Next: data owned by the teams that make it.
What everyday example explains outbox?
The letter filed in the same drawer as the record. If the record is in the drawer, so is the letter; the postman empties the drawer, and the warehouse throws away a letter it has already read.
Why should a team care about outbox?
Every service that "saves and then publishes" has this gap, and it fails silently: no error, just a customer who paid for nothing. Any state change another service must hear about needs an outbox (or a change feed doing the same job) and consumers that ignore repeats.