Distributed patterns / Intermediate / 5 min / Updated
Transactional outbox: no lost events, no dual writes
Kill the order service between the DB write and the Kafka publish. Dual writes lose events; the outbox and CDC never do, but send some twice.
Start the simulation → Change the load, break a part, watch what happens. Free, no sign-up.
In short
What is outbox pattern?
The outbox writes an event row in the same DB transaction as the business change; a relay publishes it.
Why does it matter?
Services must update their DB and tell others, without losing or inventing events on crashes.
What is the trade-off?
No lost events, but at-least-once delivery means duplicates, so consumers must be idempotent.
How do you explain it in an interview?
"Orders write to an outbox table; Debezium streams it to Kafka; consumers dedupe by event id."
Check yourself
What goes wrong with a dual write (save to DB, then publish to Kafka)?
Answer: A crash between the two leaves an order with no event or an event with no order. The two writes are not in one transaction, so they can disagree.
How does the outbox pattern fix it?
Answer: The event row is written in the same DB transaction as the order; a relay publishes it later. Both commit or neither does, so the relay always has the event to publish.
Why must outbox consumers be idempotent?
Answer: The relay can publish an event and crash before marking it sent, so it is sent again. The outbox gives at-least-once delivery; consumers dedupe by event id.