Notestransactions🌱 seedling
Saga Pattern
Also: Saga · Sagas · Compensating Transaction · Orchestration Saga · Choreography Saga
Replace one long distributed transaction with a sequence of local transactions, each committed immediately, plus a compensating transaction for each step. On failure, run the compensations in reverse. Atomic eventually; never isolated.
Why it exists
Long-lived, cross-service work can't hold locks across services for its whole duration, and many participants (payment providers, warehouses) can't take part in Two-Phase Commit anyway.
How it works
- Steps T1…Tn each commit locally; for each undoable step there's a Ci that semantically reverses it (refund, release, cancel).
- Outcome: all of T1…Tn, or T1…Tk followed by Ck…C1.
- Orchestration: a coordinator tells each service what to do and tracks state. Choreography: services react to each other's events.
- Put irreversible steps (send email, ship) last, after the last step that can fail.
- Every step and compensation must be idempotent and retryable; each step's "update DB + emit event" needs a Transactional Outbox.
Trade-offs
| Gains | Costs |
|---|---|
| no cross-service locks or blocking | no isolation: others see intermediate states |
| works with external services | compensations to design, test and keep correct |
| failures stay local | some actions can't be compensated |
In practice
- Isolation countermeasures: semantic locks (a
PENDINGstate others treat as tentative), commutative updates, re-reading values before acting. - Durable workflow engines such as Temporal are a common way to run orchestrated sagas so they survive crashes (Durable Execution).
Pitfalls
- Calling a saga a transaction: a reader can see "charged" before "refunded".
- A compensation that can fail permanently leaves the saga stuck halfway.
Connections
- Part of
- Transactions
- Contrasts with
- Two-Phase Commit
- Related
- Distributed TransactionDurable ExecutionIdempotencyIntegrity vs TimelinessTransactional OutboxWorkflow Orchestration
- In real systems
- Temporal
Recall
These cards are also in your review deck, scheduled with spaced repetition.