Part 5 of 7 · 3 min read
Sagas
Break the work into local transactions and undo with compensating steps.
Think of booking a holiday: flight, then hotel. If the hotel is full, you do not roll back time. You phone the airline and cancel the flight. The cancellation is a new action that cancels the effect of the old one.
The UDS book notes that 2PC is synchronous and blocking, assuming short transactions and available participants. Some transactions take hours, and some do not need isolation. Then a non-blocking way to keep atomicity helps. The saga is that way.
Saga
A saga is a distributed transaction made of local transactions T1, T2, ... Tn, each with a compensating transaction Ci that undoes its effect. Either all Ti succeed, or the compensations undo the ones that did.
Example: T1 books a flight, T2 books a hotel. If T2 fails, run C1, which cancels the flight.
The book's trip example has an orchestrator (the coordinator) in the travel service:
- Ask the flight service to book (T1). If it fails, nothing happened. Mark the transaction aborted.
- If it succeeds, ask the hotel service (T2). If that succeeds, the saga is done.
- If the hotel fails, send a cancel (C1) to the flight service.
The coordinator can talk to participants through message channels so that a temporarily down service does not block it. Because the coordinator can crash, it records the saga's state in a database at every step, as a state machine, so a restart (or another elected coordinator) resumes where it left off.
Retries mean duplicates. If the coordinator crashes after sending a request but before saving, it will send it again. A timeout causes a resend too. So participants must remove duplicates, making each step idempotent (module 1). Managed workflow services such as AWS Step Functions and Azure Durable Functions provide this machinery.
What a saga gives up: isolation. Between T1 and C1 other users can see the flight booking. The UDS book suggests semantic locks: mark the data a saga changes with a dirty flag, cleared at the end. Others who meet the flag either fail and roll back or wait, and waiting can cause deadlocks that need a plan.
The Building Microservices book describes the same idea and its pains. A failure may be handled by retrying later, which is a form of eventual consistency, or by aborting the whole operation with compensating transactions. If the compensation itself fails, retry it or let a background process or an admin screen clean up. With many steps, handling every failure mode becomes hard to understand, so the book points to distributed transactions as an alternative, and also warns that those are hard to get right and advises against writing your own.
A worked example. An order saga for an online shop has steps T1 reserve stock, T2 charge card, T3 create shipment. Compensations: C1 release stock, C2 refund card.
| Case | Sequence |
|---|---|
| All succeed | T1, T2, T3 |
| Card fails | T1, T2 fails, C1 |
| Shipment fails | T1, T2, T3 fails, C2, C1 (reverse order) |
Notice that between T2 and C2 the customer was really charged. That temporary visible state is the missing isolation. The design needs the user interface to say "payment pending, refund on the way" and not "no charge".
Check yourself:
- Why must saga steps be idempotent?
- What does a saga give up compared with 2PC?