Q9 · What’s the role of sagas in long-running workflows?
Back to 24 Architecture Questions.
The question is not “how do you sequence steps”. The axis it turns on: who compensates when step four fails, and whether compensation is a real business action. A saga is a sequence of local transactions, each committing on its own, with no global rollback. When step four fails, consistency is restored by running compensations backward: the design, not the database, owns the undo.
What the answer should contain
The saga is the distributed answer to “no global transaction”. Each step commits locally. When a later step fails, the earlier ones are undone by compensations, in reverse order. There is no rollback; there is only a plan for going backward.
Every step declares its compensation before it runs. Reserve declares release, charge declares refund, ship declares cancel. Some steps cannot be compensated at all (the email sent, the goods delivered): place them last, or design a pivot action (accept the state and finish differently) instead of pretending an undo exists.
Compensations are real business actions. A refund moves money through a payment provider;
it is not a DELETE. If the business would not actually take the compensating action in
practice, the saga is fiction, and finding that out during an incident is the expensive way.
Two coordination styles, both legitimate. Orchestration: a coordinator owns the plan, the step states, and the compensation sequence; the plan is visible and testable, the coordinator must persist its state. Choreography: services react to each other’s events; no central point, and the plan is invisible in any single place. Pick orchestration when the workflow has money in it, choreography when it is a few loosely coupled reactions.
Choreography still needs three designed things. A correlation ID on every message. A failure event each participant subscribes to, which is what triggers its own compensation. And a per-step deadline, so a step whose expected next event never arrives compensates itself. Each service persists only its own step state. For one view of a whole saga, add a read-only tracker fed by the same events; if one owner must decide when a saga is dead, use an orchestrator.
Compensations assume a repeat. They run under at-least-once delivery, so they must be idempotent, and the saga state must persist so a crashed coordinator resumes rather than restarts. A compensation that fails is parked for a human, with the full saga state attached.
Sagas expose intermediate states. Between charge and ship, the order is “placed, not yet shipped”. Readers must tolerate that window: isolation is weaker than a transaction, and the design must say so instead of hiding it.
Ideas worth mentioning out loud
- “A saga is a plan with a named undo for every step. If a step has no undo, it goes last.”
- The state machine is the design: written down, it is reviewable; implied, it is a set of incident postmortems waiting to be written.
- Ask the business the axis question directly: “when step four fails, who refunds, and is that refund something you actually do today?”
Follow-ups to expect
- What if a compensation fails? Retry with cap, then park to a human with the saga state. This is the DLQ of the workflow, and it must be staffed.
- Why not a distributed transaction? Locks held across a network, a coordinator that becomes a second failure domain, and a throughput ceiling, usually for no business-visible benefit.
- How do you test it? Fail each step deliberately, in every position, and assert the compensations ran in reverse order.
Red flags in an answer
- Compensation described as “delete the row”.
- An uncompensatable step sitting in the middle of the chain.
- No persisted saga state: failure handling is a hope, not a mechanism.
- Nobody asked the business whether the refund is a real action it will take.
Sources
- Sagas, Hector Garcia-Molina and Kenneth Salem, SIGMOD 1987: the original paper. A saga is a long transaction broken into steps, each with a declared compensation, and the paper’s motivation, locks held for hours, is still the argument against distributed transactions today.
- Pattern: Saga, Chris Richardson, microservices.io: orchestration versus choreography, and the coordination failure handling that makes the pattern operational.