What is the saga pattern?

The saga pattern manages a business transaction that spans several services as a sequence of local transactions. If a step fails, compensating transactions undo the steps that already completed.

Also called saga, saga transaction, compensating transactionUpdated

In a single database, "reserve stock, charge the card, create the shipment" is one transaction. Either it all happens or none of it does. Once those steps live in separate microservices with separate databases, that guarantee is gone. Two-phase commit across services is slow, fragile and unsupported by most cloud databases and brokers. A saga is the usual replacement.

"Saga" is not an acronym, so there is no full form. Hector Garcia-Molina and Kenneth Salem introduced the term in a 1987 SIGMOD paper about long-lived database transactions. Their idea was to split one long transaction into a sequence of shorter ones, each paired with a compensating transaction that semantically undoes it.

How it works

  • Each step is a local transaction in one service, committed straight away.
  • Each step that may need undoing has a compensation: ReleaseStock for ReserveStock, RefundPayment for CapturePayment.
  • If step n fails, the saga runs the compensations for steps n−1 down to 1, in reverse order.
  • Compensation isn't rollback. Other users may have seen the intermediate state, and some actions, such as a sent email, can only be followed up, not undone.

Choreography vs orchestration

These are the two types of saga.

  • Choreography. No coordinator. Each service listens for events and publishes its own: OrderPlaced → inventory reserves → StockReserved → payments charges. It is simple for three or four steps, but the flow exists only across several codebases and gets hard to follow.
  • Orchestration. A coordinator (the orchestrator) tells each service what to do and tracks the saga's state. The flow is in one place, easier to test and easier to monitor. Workflow engines such as Temporal, AWS Step Functions and Camunda are often used, as are a hand-written state machine in Spring Boot or .NET.

Most teams start with choreography and move to orchestration once a saga passes four or five steps or needs timeouts.

Example: a holiday booking

A travel platform sells a flight, hotel and payment as one package. An orchestrator runs:

Step Action Compensation
1 Hold flight seat (expires in 15 minutes) Release seat
2 Book hotel room Cancel room
3 Capture ₹48,500 payment Refund payment
4 Issue ticket and confirm None (the point of no return)

The hotel booking succeeds but payment is declined. The orchestrator records PaymentFailed, cancels the room, releases the seat and marks the booking FAILED. The customer sees "payment declined, nothing was booked". If the hotel API times out on cancel, the orchestrator retries with backoff. After the retry limit it raises an alert for manual handling.

Ordering matters. Put steps that are easy to undo first, and irreversible steps such as ticketing last.

What makes sagas reliable

  • Idempotent steps. The orchestrator retries, so each action and compensation must be safe to repeat (see idempotency).
  • Reliable messaging. Commit the state change and the outgoing message together with a transactional outbox.
  • Visible intermediate states such as PENDING_PAYMENT, so other features don't treat a half-finished order as done.
  • Timeouts for steps that never answer.
  • Reconciliation to catch the rare cases that end up stuck.

When to use it

Use a saga when a business action must change data owned by more than one service. If all the data can sit in one database, for example inside a modular monolith, a plain transaction is simpler and stronger.

Our monolith vs microservices guide covers when that trade is worth making.

Any idea in mind? Let's make it real.

Tell us what you're building. You'll hear back from an engineer, not a sales team, usually within one working day.

Book a 30-min call