The dual write problem
A service often must update its database and publish an event. Doing both as two separate calls is a dual write. If the database commits but the publish fails, consumers never learn about the change. The outbox pattern removes the gap.
How the outbox works
- In the same database transaction that changes business data, insert the event into an outbox table.
- Because both writes share one transaction, they commit or fail together.
- A separate relay reads new outbox rows and publishes them to the broker, then marks them sent.
Why it is reliable
- The atomic local write guarantees that if data changed, the event exists.
- The relay retries publishing, giving at least once delivery.
- Because of retries, consumers may see duplicates, so they must be idempotent.
The relay can poll the table or tail the database change log for lower latency.
Key idea
The outbox writes the event in the same transaction as the data, then a relay publishes it, turning an unsafe dual write into one reliable atomic step.