Back to Docs
Recipe

Saga Distributed Transaction Pattern

Coordinate long-running transactions across microservices without distributed locks or two-phase commit.

Problem

A single business operation spans multiple services — order creation, payment capture, inventory reservation, shipment scheduling. Each step may fail independently. Traditional 2PC blocks resources and degrades under contention.

Solution

Decompose the transaction into a sequence of local transactions, each paired with a compensating action. If any step fails, execute compensations in reverse order to restore consistency.

Orchestration Flow

  1. 1Create Order — insert pending order row. Compensate: mark cancelled.
  2. 2Reserve Inventory — decrement available count. Compensate: release hold.
  3. 3Capture Payment — authorize and settle. Compensate: void or refund.
  4. 4Schedule Shipment — enqueue fulfillment job. Compensate: cancel shipment.

Choreography vs Orchestration

Choreography — each service listens for events and triggers the next step. Decoupled but harder to reason about.
Orchestration — a central saga coordinator invokes each step sequentially. Easier to observe, retry, and compensate.

Key Considerations

  • • Compensations must be idempotent — retry-safe.
  • • Use outbox pattern for reliable step dispatch.
  • • Track saga state in a durable store for crash recovery.
  • • Timeouts and dead-letter queues prevent stuck sagas.