Topic 118 / Advanced
Transactional outbox
Chapter 7.4 · Data Consistency Across Systems
What and why
A service often must both change its own database state and tell the rest of the system about it — but a database commit and a broker publish are two separate operations against two separate systems, and nothing ordinary makes them atomic: one can succeed while the other fails, leaving the event and the state permanently disagreeing (the dual-write problem). The transactional outbox avoids this by writing the business change and a record of the event to publish in the same local database transaction (an "outbox" table), then having a separate relay read the outbox and publish to the broker, marking rows published once confirmed. Builds on: → 70 (transactions and isolation levels — the outbox's atomicity depends entirely on the local transaction boundary).
Diagram
Text version of this diagram
Service transaction: Relay (separate process):
BEGIN
UPDATE orders SET status=... poll/stream outbox WHERE published = false
INSERT INTO outbox (event) │
COMMIT ── both rows durable ▼
together, or neither is publish to broker → mark row published
How it works
- The business-logic transaction writes its normal state change and inserts a row describing the event into an outbox table, inside the same database transaction.
- The transaction commits atomically — either both the state change and the outbox row exist, or neither does; there is no window with only one.
- A relay process (a poller, or a CDC-based reader → 119) reads unpublished outbox rows and publishes them to the broker.
- After a confirmed publish, the relay marks the row published (or deletes it); if it crashes after publishing but before marking, it republishes on restart.
- Because of step 4, the outbox is at-least-once by design — consumers of these events must be idempotent (→ 116); the pattern guarantees the event is never lost relative to the state change, not that it arrives exactly once.
Practical example
(illustrative). An order service updates orders.status = 'placed'
and inserts an OrderPlaced row into outbox in one transaction. If the process crashes
immediately after commit but before the relay runs, the outbox row is still there on
restart, and the relay publishes it once the process comes back. Publishing first, then
updating the database — or updating then publishing with no outbox — both have a window
where a crash between the two steps loses one side permanently.
When to use it
- Use when a service must atomically pair a database write with a published event, and you control that service's writes.
- Avoid when the service doesn't own the database (then CDC, → 119, derives events from the log instead), or when best-effort, non-atomic consistency between the write and the notification is already acceptable.
- Failure scenarios: reading the outbox through a mechanism that isn't actually consistent with the write (e.g. a relay reading a lagging replica) republishes stale rows or misses recent ones; deleting outbox rows immediately on publish, without confirming the publish succeeded, can lose an event.
- Simpler alternative: if dual-write inconsistency is rare and tolerable (a non-critical analytics event), skip the outbox and accept best-effort publish-after-commit — simpler, but now some failures genuinely lose the event.
Practice
(pattern — implement minimal, spot the misuse; 20–30 min, lab Postgres + Kafka). Implement an order update plus outbox insert in one Postgres transaction, and a relay that polls the outbox and publishes unpublished rows to a Kafka topic, marking them published after a successful send. Kill the process between the commit and the relay's first poll, restart, and confirm the event is still published. Then, given a version that publishes to Kafka and updates the database as two independent calls with no outbox, reproduce the inconsistency by failing the second call, and show the outbox version doesn't have that failure mode. Done when the crash-and-restart test still publishes the expected event, and the non-outbox version is shown to lose or duplicate it under the same fault.
Shared lab setupKey takeaway
The outbox pattern makes "write the state and record the event" atomic by keeping both inside one database transaction, pushing the publish step to an at-least-once relay. Done when you can: explain why publishing right after commit, without an outbox, has a permanent failure window, and why outbox consumers must be idempotent. Used next in → 119 (CDC is an alternative way to produce an event stream from writes), → 139 (Saga steps often publish via outbox to stay consistent with their own state).
References
Designing Data-Intensive Applications — see references.md.
Source directory