← Lessons

quiz vs the machine

Gold1460

System Design

The Outbox for Payment Events

Writing events to a table in the same transaction so the ledger and the message bus never diverge.

5 min read · core · beat Gold to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. What problem does the outbox pattern solve?

2. Why must consumers of outbox events be idempotent?