Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 37 ยท Data Consistency

Sagas: Distributed Transactions Without 2PC

Keep data consistent across services without distributed locks: learn orchestrated and choreographed sagas, compensating actions and failure handling with runnable code.

In a monolith with one database, placing an order is one transaction: reduce stock, record the payment and save the order, and if anything fails, the database undoes everything. Once each service owns its own database (Database per Service and Data Ownership), that single transaction is gone. Stock lives in one database, payments in another, orders in a third, and no COMMIT can span all three.

The classic answer, two-phase commit (2PC), makes all participants lock their data until a coordinator decides. It is rarely used between microservices: it is slow, locks resources across the network, and one stalled participant blocks everybody. The practical answer is the saga: a sequence of local transactions, one per service, where each failure is repaired by running compensating actions for the steps already done.

The saga idea

Local steps Each service commits its own transaction.
Compensation A failed step triggers undo steps.
No global lock Nothing waits across the network.
1 / 2

An order saga

Consider an order that needs three steps, each owned by a different service:

  1. Orders creates the order in state pending.
  2. Stock reserves the items.
  3. Payments charges the customer.
  4. Orders marks the order confirmed.

Each step is a separate local transaction that commits immediately. If step 3 (payment) fails after step 2 (reserve), the stock is already reserved and committed. The saga now needs a compensating action for each completed step: release the stock, and cancel the order.

flowchart LR
    A["Create order<br/>(pending)"] --> B["Reserve stock"]
    B --> C["Charge payment"]
    C --> D["Confirm order"]
    C -.->|"payment fails"| X["Release stock"]
    X -.-> Y["Cancel order"]

Solid arrows are the happy path. Dotted arrows are the compensation run when the payment step fails.

Compensating actions are not rollbacks

A database rollback makes it as if the transaction never happened. A compensation does not: it is a new business action that semantically undoes an earlier one. The original change was visible to other services for a while, and some effects cannot be erased at all.

  • A reserved stock item can be released, but another customer may have seen "sold out" in the meantime.
  • A charged card is refunded, which creates a second ledger entry and may email the customer.
  • A sent email cannot be unsent. You send a correction instead.

So design each step with its compensation in mind, and put steps that are hard to undo last. Charging the card before reserving stock would mean refunding customers for every out-of-stock item; reserving first, then charging, means an unlucky payment failure costs only a reservation that is easy to release. Steps after which no failure can happen (the final confirmation) are the pivot that decides whether the saga can still go back.

Compensations must also be idempotent, because they can be retried, and they must keep working when the original service is slow to answer (Synchronous Communication with HTTP APIs).

Orchestration

In an orchestrated saga, a single component, the orchestrator, holds the workflow. It sends commands to each service in order, records the state, and on failure runs the compensations in reverse order. The flow is written in one place, which makes it easy to read, test and monitor.

The disadvantage is that the orchestrator is another piece of logic to build, and it can attract too much business knowledge. It must also persist its state, so that if it crashes in the middle it can continue where it stopped.

Node.js An orchestrated order saga with failures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

When payment fails, the stock reservation and the order are undone in reverse order. When stock fails, only the order exists to cancel, so the compensation list is shorter. The failed step itself is not compensated, because it did not complete.

Choreography

In a choreographed saga there is no coordinator. Each service listens for events and publishes its own: orders publishes OrderCreated, stock hears it, reserves items and publishes StockReserved (or StockRejected), payments hears that and so on. Compensation works the same way: PaymentFailed makes stock release the reservation and orders cancel the order.

This has no central component and suits small sagas, but the flow is spread across services, and nobody can see the whole picture in one file. With more steps, it gets hard to follow who reacts to what, and cyclic dependencies between services sneak in. A common rule of thumb: choreography for two or three steps, orchestration beyond that.

The stand-in below uses a tiny synchronous event bus. In reality events travel through a broker (Asynchronous Messaging and Events) with at-least-once delivery, so every handler must tolerate duplicates.

Node.js A choreographed saga on an event bus

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Nobody coordinated these flows: each service reacted to the event it cared about. Notice that order 2 went through a visible intermediate state (stock reserved, payment pending) before it failed. That leads to the most important property of sagas.

No isolation: the price of sagas

A database transaction gives you isolation: nobody sees half-finished work. Sagas do not. Between steps, other requests can see the intermediate state: a reserved item that will be released, a pending order, a charged card that will be refunded. Designers call these anomalies, and the common defenses are:

  • Semantic locks. Mark records with a state such as pending so others know a saga is in progress and can wait or refuse to touch them.
  • Commutative updates. Prefer operations whose order does not matter, such as "add 5" or "subtract 3", over "set to 17".
  • Reread before acting. Check the value again before the step that depends on it.
  • Show the truth in the UI. "Order received, payment processing" is honest and avoids surprises.

Operating sagas safely

  • Persist saga state so a crash does not lose track. An orchestrator that dies halfway must resume or compensate on restart.
  • Make every step and compensation idempotent, because retries and duplicates are normal (The Transactional Outbox and Idempotent Consumers).
  • Retry steps that can succeed later; compensate only for business failures. A timeout is not a "no": retry it first, then decide (Timeouts, Retries and Exponential Backoff).
  • Compensations must not fail permanently. If a refund fails, retry until it works or alert a human.
  • Publish events reliably. Updating your database and publishing an event are two writes; the outbox pattern makes them atomic enough.
  • Observe them. Use a correlation id across all steps so one saga can be followed end to end (Distributed Tracing).

If two services always change together in one transaction, that is often a sign they belong in one service. A saga is the tool for when the boundary is right but a business process crosses it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is two-phase commit rarely used between microservices?

    2PC couples availability and latency of all participants. Sagas avoid global locks by using local transactions plus compensations.
  2. 2. What is a compensating action?

    The original step already committed and may have been seen by others, so the undo is another action, such as a refund or a stock release.
  3. 3. Why put hard-to-undo steps late in a saga?

    Do the easily reversible steps first so that a failure costs little. Charging a card is harder to reverse than reserving stock.
  4. 4. Which is a main drawback of choreography compared with orchestration?

    With no central workflow, you must read several services to understand the whole saga.
  5. 5. What do sagas NOT provide that database transactions do?

    Between steps, others may see half-finished state. Use semantic locks, pending states and commutative updates to cope.

Summary

  • Without a shared database, a business process that crosses services becomes a saga: local transactions with compensating actions for failure.
  • Compensations are new business actions, not rollbacks, and some effects (like a sent email) can only be corrected, not erased.
  • Orchestration keeps the flow in one coordinator; choreography lets services react to events. Choreography suits short sagas.
  • Sagas give up isolation, so design for visible intermediate states with pending statuses and semantic locks.
  • Persist saga state, make steps and compensations idempotent, and retry before compensating.

Next: The Transactional Outbox and Idempotent Consumers.