The dual write problem
When a service updates the ledger and then publishes an event to a message bus, a crash between the two leaves them inconsistent: the money moved but no one was told, or an event fired for a change that rolled back.
The outbox pattern
Write the event into an outbox table in the same database transaction as the ledger change. A separate relay reads new outbox rows and publishes them to the bus, marking each as sent.
- The database commit makes the state change and the event atomic.
- The relay provides at least once delivery, so consumers must be idempotent.
Operational notes
- Poll or use change data capture to drive the relay.
- Keep an index on unsent rows so the relay scans efficiently.
- Retry publishing on failure; duplicates are fine because consumers dedupe.
Key idea
The outbox pattern records payment events in the same transaction as the ledger change, then relays them, eliminating the dual write gap between database and message bus.