# The saga pattern: local transactions, compensation and state

> A saga replaces one big distributed transaction with a chain of local ones and a compensating action per step. It needs a persisted state, idempotent steps and a sweep for stuck sagas.

Published 2026-11-25 · Language: en · Tags: distributed-systems, saga, payments

When a business operation touches several services, such as creating an order, charging a wallet and reserving stock, you cannot wrap it in one database transaction. A **saga** breaks it into a sequence of local transactions. Each one commits in its own service, and if a later step fails, earlier steps are undone by running **compensating transactions**.

 

## A saga in motion

 

Take a purchase: T1 creates the order as pending, T2 debits the wallet, T3 reserves the stock. If T3 fails because the item is out of stock, the saga runs the compensations in reverse order: C2 refunds the wallet, then C1 cancels the order. A compensation is a new business action, not a database rollback. The original debit did commit and was visible, and the refund is a second, separate entry in the ledger.

 

## Orchestration or choreography

 

- With **orchestration**, a central orchestrator sends a command to each service in turn, tracks the outcome and issues compensations. The whole flow is readable in one place and easy to monitor and change. The orchestrator becomes a component you must make reliable and highly available.
- With **choreography**, there is no coordinator. Each service listens for events and reacts, publishing its own event when done. It has fewer moving parts and no central point of failure, but the flow is spread across services, harder to follow and easy to turn into circular dependencies.

 

Choreography suits short, simple flows with few participants. For payments and other flows with many steps and rollbacks, orchestration is usually easier to reason about.

 

## What makes a saga reliable

 

1. **Persist the state.** Model the saga as a state machine stored in a table, such as `STARTED, WALLET_DEBITED, STOCK_RESERVED, COMPLETED, COMPENSATING, FAILED`. After a crash, the orchestrator reads the state and continues.
2. **Make every step and compensation idempotent.** Messages are delivered at least once and retried, so running the refund twice must not refund twice. Use an idempotency key per saga step.
3. **Retry compensations until they succeed.** A compensation that cannot fail permanently is the safety net. Use backoff, and escalate to a manual queue with an alert if it keeps failing.
4. **Order the steps by risk.** Some actions cannot be undone, such as sending an email or shipping a parcel. Put steps that are hard or impossible to compensate last, after everything that can fail has succeeded.
5. **Expect no isolation.** Other users can see the intermediate state. Mark records as pending so other flows can treat them accordingly, a technique called a semantic lock, and show users an honest in-progress status.
6. **Detect stuck sagas.** A periodic job looks for sagas that sat in an intermediate state longer than expected and either pushes them forward or compensates them.

 

```mysql
CREATE TABLE sagas (
  id          CHAR(36)    PRIMARY KEY,
  type        VARCHAR(40) NOT NULL,
  state       VARCHAR(30) NOT NULL,
  payload     JSON        NOT NULL,
  updated_at  DATETIME(3) NOT NULL,
  KEY idx_state_updated (state, updated_at)    -- supports the stuck-saga sweep
) ENGINE=InnoDB;
```

 

## A warning sign

 

Calling three services one after another over HTTP and describing the result as atomic, with no stored state, no compensation and no sweep for failures, is not a saga. When the second call times out you do not know whether it happened, and nothing will repair the result.

 

## Takeaway

 

A saga trades atomicity for availability: every step commits locally, and failures are handled by compensating actions. Persist its state machine, make steps and compensations idempotent, put irreversible steps last, show pending states honestly and reconcile anything that gets stuck.

Source: https://tdphat.io.vn/blog/saga-pattern-compensation-and-state
