Video in progress
How do you choose between orchestration and choreography for a saga, and which classic patterns do they resemble?
Orchestration: a central coordinator commands each step (command + state machine). Choreography: services react to each other's events (observer). With many steps and an order that matters, I choose orchestration; with 2-3 independent reactions, choreography.
📖 Answer
In choreography, OrderCreated is published, the warehouse hears it and reserves stock and publishes StockReserved, payment hears that and charges the card, and so on. Nobody knows the whole process. This is an observer at the system level.
Coupling is low, and adding a new reaction is easy. The problems appear as the steps multiply:
- you can't see from one place where the process got stuck;
- the compensation logic is spread across services;
- a loop of events can appear and nobody notices it.
In orchestration, OrderSaga has a state machine. It sends a ReserveStock command, waits for the reply, then sends ChargePayment. On failure, it sends compensation commands in reverse order. This is a combination of the command and state patterns, and the process reads in a single piece of code.
The criteria I look at:
| Choreography | Orchestration | |
|---|---|---|
| Number of steps | 2-4 | more than 4 |
| Compensation | little or none | complex, ordered |
| Process owner | none, each team owns its part | one team |
| Observability | relies on distributed tracing | saga state in a table |
In both cases the must-haves are the same: every step idempotent, messages through an outbox, and every compensation safe to run again. And put the steps that "can't be compensated" (an SMS was sent, money went to an external bank) as late as you can.
Sign up to mark studied questions and save your progress.