Saga: Orchestration, Choreography and Compensation
Objectives
By the end of the lesson, you can explain what the saga solves, how orchestration and choreography differ, and why compensation is not a distributed rollback.
Why is it needed?
If a business operation touches several data owners, there is no shared transaction that either commits everything or nothing (5.1). The saga addresses how a coherent end result can still be reached when the steps happen in separate local transactions.
What is a saga?
According to the definition of microservices.io, a saga is a sequence of local transactions. Each step updates the database of its own service and publishes a message or event that triggers the next step. If a step violates a business rule, the saga runs compensating transactions that offset the earlier changes in business terms.
flowchart TD
A["Step 1:<br/>local transaction"] --> B["Step 2:<br/>local transaction"]
B --> C{"Succeeded?"}
C -->|"Yes"| D["Step 3"]
C -->|"No"| K["Compensation:<br/>offsetting step 1"]
Two modes of coordination
| Mode |
How does it work? |
Advantage |
Cost |
| Choreography |
Each local transaction publishes an event that triggers a local transaction in another service; there is no central controller |
Little central dependence with few steps |
Hard to follow with many branches and cycles; a new consumer may create new feedback |
| Orchestration |
An orchestrator tells the participants which local transaction to execute |
A transparent flow, the rule described in one place |
The orchestrator's contract and availability; its durable state must be resumable after a restart |
The orchestrator should not take over the services' own internal rules.
Compensation is a business operation
Compensation is not time travel. Refunding a charge is a new financial operation, not making the original record disappear. After a reservation is released, someone else may take the resource. A compensating step can also fail and be repeated, so it needs its own idempotency, retry and final reconciliation path (6.5). If automatic recovery is not possible, the system must provide a recognisable, traceable state.
What does a saga not give?
microservices.io mentions three drawbacks:
- No automatic rollback: the compensating transactions must be designed by the developer.
- No isolation: concurrent sagas and transactions may see anomalies, so countermeasures are needed (5.4).
- The outcome must be reported: the result of an asynchronous saga must be learned by the client through a separate channel (delayed response, polling, notification).
In addition, a service cannot use a distributed transaction between its database and the message broker, so a separate pattern (outbox or event sourcing) is needed to link data saving and message sending atomically (5.3).
By aspect
| Aspect |
Saga |
| Coupling |
The contracts of the steps and the meaning of the events are a shared dependency |
| Consistency |
Intermediate states are visible; eventual agreement |
| Failure handling |
Compensation, retry, handling an unknown outcome |
| Cost |
Keeping track of the process state and observing it |
Common misconceptions
| Statement |
Clarification |
| "A saga is the same as a distributed transaction." |
There is no isolation and no automatic undo. |
| "Compensation restores the previous state." |
It performs a new business operation that offsets the earlier one. |
| "Choreography is simpler because there is no orchestrator." |
The process still exists, just harder to follow. |
| "A saga guarantees that it always succeeds." |
It guarantees that a failure is handled and ends up in a traceable state. |
In short
- A saga is a sequence of local transactions with compensation.
- Two coordination modes: choreography and orchestration.
- Compensation is a new business operation, not an undo.
- There is no isolation and no automatic rollback; the outcome must be reported separately.
Review questions
- Why is compensation not a rollback?
- When is it better to use an orchestrator instead of choreography?
- What happens if the compensating operation fails too?
Learning resources
Saga: Orchestration, Choreography and Compensation
Objectives
By the end of the lesson, you can explain what the saga solves, how orchestration and choreography differ, and why compensation is not a distributed rollback.
Why is it needed?
If a business operation touches several data owners, there is no shared transaction that either commits everything or nothing (5.1). The saga addresses how a coherent end result can still be reached when the steps happen in separate local transactions.
What is a saga?
According to the definition of microservices.io, a saga is a sequence of local transactions. Each step updates the database of its own service and publishes a message or event that triggers the next step. If a step violates a business rule, the saga runs compensating transactions that offset the earlier changes in business terms.
flowchart TD A["Step 1:<br/>local transaction"] --> B["Step 2:<br/>local transaction"] B --> C{"Succeeded?"} C -->|"Yes"| D["Step 3"] C -->|"No"| K["Compensation:<br/>offsetting step 1"]Two modes of coordination
The orchestrator should not take over the services' own internal rules.
Compensation is a business operation
Compensation is not time travel. Refunding a charge is a new financial operation, not making the original record disappear. After a reservation is released, someone else may take the resource. A compensating step can also fail and be repeated, so it needs its own idempotency, retry and final reconciliation path (6.5). If automatic recovery is not possible, the system must provide a recognisable, traceable state.
What does a saga not give?
microservices.io mentions three drawbacks:
In addition, a service cannot use a distributed transaction between its database and the message broker, so a separate pattern (outbox or event sourcing) is needed to link data saving and message sending atomically (5.3).
By aspect
Common misconceptions
In short
Review questions
Learning resources