The Lost Event: Dual Writes and the Transactional Outbox
An Order Service saves an order in its database and publishes an OrderPlaced event so that the Shipping Service can ship it. Two systems, one business fact - and no transaction that spans both. That gap is called the dual-write problem, and it is one of the most common hidden bugs in event-driven systems and microservices.
Your job in this simulation is to break one simple rule: every committed order gets exactly one shipment, and nothing ships that was never ordered. Then switch strategy and try to break it again.
How It Works
- Pick a strategy - write to the database first, publish first, or use a transactional outbox
- Place orders and watch the data flow through the system
- Inject failures - crash the service between its two writes, take the broker down, or crash the outbox relay
- Check consistency - the panel tells you whether orders were lost, shipped twice, or shipped without existing
- Complete the challenges to see every failure mode - and the patterns that fix them
Live simulation
Actions
Inject failures
Consumer
Consistency check
Challenges 0/5
Event log
What You Just Saw
| Strategy | Service crashes between the writes | Broker is down | Duplicates |
|---|---|---|---|
| Dual write, DB first | Order saved, event lost - nothing ships | Order saved, event lost | Rare |
| Dual write, publish first | Event out, order rolled back - a ghost shipment | Order rejected - the customer sees an error | Rare |
| Transactional outbox | Nothing lost - the event is in the outbox table | Orders keep flowing; events are delivered later | Possible - consumers must be idempotent |
Why dual writes fail
Reordering the two writes only changes which inconsistency you get. Database first risks lost events; publish first risks events about things that never happened - and it couples your ability to take orders to the broker's availability. Retries do not help either: a process that crashes cannot retry, and a retry after a timeout cannot know whether the first attempt succeeded.
How the outbox fixes it
The service writes the order and the event into its own database in one local transaction. Either both exist or neither does. A separate relay reads unsent rows from the outbox table, publishes them, and marks them as sent. The relay can be a simple poller or change data capture on the database log (for example, Debezium's outbox event router).
The price: at-least-once delivery
If the relay crashes after publishing but before marking the row as sent, it publishes the same event again after restarting. The outbox guarantees that no event is lost - not that each event arrives exactly once. That is why the outbox and the idempotent consumer are two halves of one pattern: the consumer records which event IDs it has processed, ideally in the same transaction as its own state change, and ignores repeats.
Key Takeaways
- Writing to a database and a message broker in one operation is a distributed transaction in disguise
- Changing the order of the writes moves the failure; it does not remove it
- The transactional outbox turns two writes into one local transaction plus reliable, asynchronous delivery
- Outbox delivery is at-least-once - design every consumer to be idempotent
- The outbox also decouples availability: you can accept orders while the broker is down
- Costs to plan for: relay latency, outbox table cleanup, monitoring of the backlog, and event ordering per aggregate
Further Reading
- Chris Richardson - Pattern: Transactional outbox and Pattern: Idempotent consumer
- Pat Helland - "Life beyond Distributed Transactions" (2007, revisited 2016)
- Debezium documentation - the outbox event router for change data capture
Decision Angle
"Use a transactional outbox for publishing domain events" is a textbook Architecture Decision Record. The context is the dual-write risk; the consequences include at-least-once delivery, the obligation for idempotent consumers, a relay to operate, and an outbox table to clean up. Writing that down once saves every future team member from rediscovering it in production.
See also: Event-Driven Architecture · Microservices