Transactional Outbox
Save a change and the event announcing it in the same database transaction, then publish the event separately, so you never lose or invent events.
Description
A service that writes to its database and then publishes an event has a gap: if it crashes between the two, the order exists but nobody hears about it; publish first and crash, and everyone hears about an order that doesn't exist. You can't wrap a database and a message broker in one transaction.
The outbox closes the gap. The service writes the business change and a row in an outbox table in one local transaction: both happen or neither does. A separate relay reads new outbox rows and publishes them to the broker, marking them sent. If the relay crashes, it simply publishes again, so consumers must handle duplicates.
System design
Transactional Outbox system design diagram
- Order service (service).
- Orders DB (database): orders + outbox tables.
- Outbox relay (function): polling or CDC.
- Message broker (topic).
- Shipping service (service).
- Step 1: Order service → Orders DB: one transaction.
- Step 2: Orders DB → Outbox relay: new outbox rows.
- Step 3: Outbox relay → Message broker: publish.
- Step 4: Outbox relay → Orders DB: mark as sent.
- Step 5: Message broker → Shipping service: deliver (at least once).
- The service inserts the order and an OrderPlaced row in the outbox table in a single local transaction. Either both are saved or neither is.
- The relay picks up outbox rows that haven't been sent, by polling or by reading the database's change log.
- It publishes each event to the message broker.
- It marks the rows as sent. If it crashes before this, it publishes them again later, a duplicate rather than a loss.
- Consumers such as shipping receive the event and ignore duplicates they've already processed.
- AWS
- DynamoDB Streams or Aurora with Debezium/DMS feeding EventBridge, SNS or Kinesis; Lambda as the relay.
- Azure
- Cosmos DB change feed or SQL with a Functions relay publishing to Service Bus.
- Google Cloud
- Spanner or Cloud SQL change streams with Datastream or Dataflow into Pub/Sub.
When to use it
Use it when
- A service must update its own database and notify others, and losing or faking an event would be a real bug.
- You're building event-driven microservices or sagas that depend on reliable events.
- You can't use distributed transactions (and you usually can't, or shouldn't).
Avoid it when
- An occasional lost notification is acceptable, such as for analytics. Publish directly and keep it simple.
- Your database or platform already turns writes into events (change data capture streams). Use that, which is an outbox without the table.
Pitfalls
- Consumers that aren't idempotent. The relay guarantees at-least-once delivery, not exactly-once.
- Outbox tables that grow forever. Delete or archive sent rows.
- Polling too often (load on the database) or too rarely (slow events). Change data capture avoids polling.
- Losing ordering when several relays publish in parallel. Partition by aggregate ID if order matters.
Alternatives
Change data capture
Choose it when your database can stream its changes (Debezium, DynamoDB Streams); the log is your outbox.
Event Sourcing
Choose it when you store events as the source of truth anyway, so there's no separate state to keep in sync.
Saga
Choose it when you need a multi-step business process across services; it relies on reliable events like these.