Saga
Run a business transaction across several services as a series of local steps, each with a compensating action that undoes it if a later step fails.
Description
In a microservice system, one business action, such as placing an order, touches several services, each with its own database. There's no transaction spanning them all. A saga splits the action into local transactions: charge the card, reserve stock, book the courier. Each step commits on its own.
If a step fails, the saga runs compensating actions for the steps that already succeeded, in reverse order: release the stock, refund the payment. The result is eventual consistency without distributed locks. Sagas are either orchestrated by a central coordinator (shown here) or choreographed through events between services.
System design
Saga system design diagram
- Checkout (client).
- Order saga (service): orchestrator.
- Payments (service).
- Inventory (service).
- Shipping (service).
- Step 1: Checkout → Order saga: place order.
- Step 2: Order saga → Payments: charge card.
- Step 3: Order saga → Inventory: reserve stock.
- Step 4: Order saga → Shipping: book courier.
- Step 5: Shipping → Order saga: no courier available (failure path).
- Step 6: Order saga → Inventory: release stock (failure path).
- Step 7: Order saga → Payments: refund (failure path).
- Checkout asks the orchestrator to place the order.
- Step 1: Payments charges the card in its own local transaction.
- Step 2: Inventory reserves the stock.
- Step 3: Shipping is asked to book a courier.
- Shipping fails: no courier is available for the address.
- The orchestrator compensates in reverse order, starting by releasing the reserved stock.
- Then it refunds the payment. The order ends as "cancelled" and every service is consistent again.
- AWS
- AWS Step Functions (with compensation via Catch states); EventBridge for choreographed sagas.
- Azure
- Durable Functions orchestrations; Logic Apps; Service Bus for choreography.
- Google Cloud
- Workflows; Pub/Sub for choreography. Temporal works on any cloud.
When to use it
Use it when
- A business process spans several services or databases and must end in a consistent state.
- Each step can be undone by a business action (refund, cancel, release).
- The process can take seconds to days, and holding locks across it is impossible.
Avoid it when
- Everything lives in one database. Use a normal transaction.
- Steps can't be compensated, such as an email sent or goods shipped. Reorder steps so the irreversible one comes last, or redesign.
- Users must never see intermediate states. Sagas are eventually consistent by nature.
Pitfalls
- Compensations that can fail too. Make them retryable and idempotent, and alert when they give up.
- Intermediate states leaking to users ("paid" orders that are later refunded). Use clear pending states.
- Choreographed sagas where nobody can see the whole flow. Prefer orchestration once there are more than three or four steps.
- Messages lost between steps. Combine with a transactional outbox.
Alternatives
Distributed transaction (2PC)
Choose it when rarely: all participants support it and you accept its locking and availability costs.
Workflow engine
Choose it when you want sagas with durable state, timers and retries handled for you (Step Functions, Temporal, Durable Functions).
Transactional Outbox
Choose it when you need each saga step to publish its event reliably. Usually used together.
Merge the services
Choose it when the steps always change together; one service with one transaction may be the honest design.