Skip to content
System Design Explorer
reliability

Saga & Transactional Outbox

By Ankur NayakReviewed

Two patterns for keeping data consistent across services without distributed transactions. A transactional outbox saves the outgoing message in the same database transaction as the change, so a message is sent if and only if the change commits. A saga chains local transactions and undoes completed steps with compensating transactions when a later step fails.

Saga & Transactional Outbox diagramComponents: Order Service, Orders DB + Outbox, Message Relay, Broker, Payment Service. Connections: Order Service to Orders DB + Outbox (1 transaction: row + outbox); Orders DB + Outbox to Message Relay (poll / CDC); Message Relay to Broker (publish); Broker to Payment Service.Order ServiceServiceOrders DB + OutboxSQL databaseMessage RelayWorkerBrokerQueuePayment ServiceService
Outbox and relay

How it works

The dual-write problem: a service updates its database and then publishes an event. If it crashes in between, or the broker is down, the two disagree. Two-phase commit across the database and the broker would fix it but is usually unavailable or too costly.

Transactional outbox: write the message into an outbox table in the same transaction as the business change. A separate message relay publishes outbox rows to the broker, either by polling the table (polling publisher) or by reading the database's transaction log (log tailing / change data capture). Messages are sent if and only if the transaction commits, in the order the sender wrote them.

The relay may publish a message more than once (for example after crashing between sending and marking it sent), so consumers must be idempotent, typically by recording the IDs of messages they have processed.

Saga: a business operation that spans services becomes a sequence of local transactions. If one fails, the steps already completed are undone by compensating transactions. In choreography each step publishes an event that triggers the next; in orchestration a coordinator tells each participant what to do. Sagas give up isolation: other operations can see intermediate states, so designs use countermeasures such as marking records PENDING.

Key points

  • Outbox: message + change in one local transaction
  • Relay publishes by polling or by tailing the log (CDC)
  • At-least-once: consumers deduplicate by message ID
  • Saga: local transactions + compensations, no global lock
  • Choreography (events) vs orchestration (a coordinator)

When to use it, and pitfalls

Use it for

  • A service must change its data and notify others reliably
  • A business process spans services that each own their database (order → payment → shipping)
  • Feeding event streams or search indexes from a primary database

Watch out for

  • Non-idempotent consumers processing a duplicate message
  • Forgetting to write to the outbox on some code path
  • Compensations that can fail too, or can't truly undo an external effect (an email already sent)
  • Reading another saga's half-finished state; mark records PENDING until the saga completes

Trade-offs

Polling publisher vs. transaction log tailing

Start with polling for simplicity; move to log tailing (CDC) at high volume.

Pros: Polling works with any SQL database; Log tailing adds no load from queries and keeps order naturally

Cons: Polling adds query load and makes strict ordering harder; Log tailing depends on database-specific tooling

Choreography vs. orchestration

Choreography for short flows; an orchestrator once there are more than a few steps.

Pros: Choreography: loose coupling, no central component; Orchestration: the whole flow is visible in one place

Cons: Choreography: hard to follow and to change; Orchestration: a coordinator to build and run

Used in

Frequently asked questions

What problem does the transactional outbox solve?

The dual write: updating a database and publishing a message are two separate writes, and a failure between them leaves the systems inconsistent. Writing the message to an outbox table in the same transaction makes the two atomic; a relay then publishes it.

How is a saga different from a distributed transaction?

A distributed transaction (two-phase commit) locks resources in every participant until all agree to commit. A saga runs a series of independent local transactions and, if one fails, runs compensating transactions to undo the earlier ones. It avoids global locks but gives up isolation between concurrent sagas.

Does the outbox pattern guarantee exactly-once delivery?

No. It guarantees a message is published if and only if the transaction commits, but the relay may publish it more than once. Consumers must be idempotent, usually by tracking the IDs of messages they have already processed.

Sources