advanced
Outbox pattern
Persist state changes and outgoing messages atomically in one service, then relay messages safely with retry and deduplication.
The outbox pattern writes domain changes and outgoing messages in one local database transaction, then a relay process publishes to the broker. This avoids dual-write races where the DB commits but the message never sends, or vice versa.
Trade-off: added table, relay lag, and deduplication at consumers — but strong reliability within the service boundary.
On interviews: contrast outbox with naive "save then publish"; explain relay failure handling and ordering guarantees.
Common pitfalls: publishing before commit; relay without backoff; consumers that are not idempotent.
Checklist:
- Store outbox rows in the same transaction as domain writes.
- Run a reliable relay with retries and metrics.
- Mark published rows and prune safely.
- Pair with idempotent consumers or inbox.