Coordinate a multi-service transaction
An order touches payment, inventory and shipping services; no single transaction spans them.
Say it like this
Payment, inventory and shipping each own their data, so instead of a distributed transaction I run a saga: local transactions in sequence, each with a compensating step, so a failure anywhere unwinds what already happened.
Basic solution
Run a saga: a sequence of local transactions, each with a compensating action to undo it on failure.
Orchestrated saga with compensation
The orchestrator runs each local transaction in order and logs progress. When the card authorisation fails, it runs the compensation for the completed step (release the driver) and marks the saga failed.
How it works
- A saga is a defined sequence of steps, each one a local transaction inside a single service: reserve inventory in the inventory service, charge the card in the payment service, create the shipment in the shipping service. No transaction ever spans two services.
- Each step has a compensating action that undoes its business effect: release the reservation, refund the charge, cancel the shipment. Compensation is a new action, not a rollback; the original step stays in the history.
- An orchestrator service (or, in the choreography style, the services themselves reacting to each other's events) runs the steps in order and records which ones have completed in a durable saga log, one row per saga and one per step.
- If a step fails after its retries, the orchestrator reads the log, runs the compensating actions for the completed steps in reverse order, and then marks the saga failed. Nothing that happened is left dangling.
- Every step and every compensation is idempotent and keyed by the saga id, because the orchestrator may crash and replay from its log, calling a step whose first attempt already succeeded.
What a saga gives up
- Atomicity: between steps, other readers can see an order that is paid but not yet shipped. Isolation becomes the application's job, usually by giving the aggregate a pending status that hides the intermediate state from ordinary reads.
Worked example
Ride booking on the ride-matching platform
- Steps: 1) lock the driver, 2) authorise the fare on the rider's card, 3) create the trip, 4) notify both parties.
- 50K trips/hour at peak ≈ 14 sagas/s. Each step is a 5–20 ms local write; the saga completes in ~100 ms, mostly network.
- Failure rates: card authorisation fails ~2% of the time. That is ~1,000 compensations/hour: release the driver lock (a Redis DEL) and nothing else, because the trip was never created.
- Rare path: the trip is created but the notification service is down. Notification is not compensated; it is retried from the saga log, because it has no business effect to undo. Only steps with side effects need compensation.
- Saga log: one row per saga plus one per step, ~5 rows × 14/s × 200 B ≈ 14 KB/s; kept for 30 days for audit, about 36 GB.
Variants and when each wins
Orchestration
- One coordinator calls each service in turn and records progress. It wins because the whole flow, its timeouts and its compensations live in one readable place; the coordinator must be durable (Temporal, Step Functions, or a state machine table in a database).
Choreography
- Each service listens for the previous step's event and emits its own when done. It wins for keeping services decoupled with no central component; the flow is spread across services and hard to see, so cycles and missing compensations hide until production.
Forward-only with retries
- Every step is retried until it eventually succeeds and there is no compensation at all. It wins only when no step can be rejected on business grounds, such as a pipeline where every step is idempotent and cannot fail permanently.
- A real distributed transaction with a prepare phase and a commit phase across all participants. It wins on atomicity; it blocks every participant while the coordinator is down, and most managed services do not support it.
Use orchestration with a workflow engine for anything involving money; choreography is fine for two or three services that already talk by events.
Failure modes
Compensation that cannot undo
- An email has been sent, or a third party has charged a non-refundable fee, and the saga then fails. It happens because a step with an irreversible side effect was placed before a step that can fail. Order the steps so the ones most likely to fail run first and the irreversible ones run last.
Orchestrator crash mid-saga
- A saga sits half-done, with the driver locked forever. It happens because the orchestrator kept its progress in memory, so when it restarted it had no idea the saga existed. Persist the saga state after every step, resume from the log on restart, and put a timeout on every saga.
Non-idempotent step on replay
- The card is charged twice. It happens because the orchestrator retried a step whose first attempt had actually succeeded, and the payment service treated the retry as a new charge. Send the saga id as the idempotency key with every step, so the service returns the first result on a repeat.
Intermediate state leaks
- The rider sees a trip appear, then vanish. It happens because the trip row was visible to reads before the saga had finished, and the saga then compensated. Write each step's data in a pending status and flip it to active only when the saga completes, or hide pending rows in reads.
Compensation itself fails
- The refund API is down and the saga is stuck in the compensating state. It happens because compensations were treated as best-effort calls. Retry compensations with backoff indefinitely, alert after N attempts, and keep a manual queue for the rare ones that need a human; a compensation must be designed to always eventually succeed.
Interviewer follow-ups
Answer out loud before you open each one.
How is a saga different from a distributed transaction?
A distributed transaction holds locks across all participants until a coordinator commits, giving atomicity and isolation at the cost of availability. A saga commits each step locally and relies on compensation for failure, so intermediate states are visible but nothing blocks.
What makes a good compensating action?
It must always be possible, idempotent, and semantically undo the step: release a reservation, refund a charge, cancel a shipment. If no such action exists, the step is irreversible and must be the last step, or the whole flow needs redesigning.
Orchestration or choreography, and why?
Orchestration for anything with more than a couple of steps or involving money, because the flow, timeouts and compensations are visible in one place. Choreography keeps services decoupled but hides the flow, which is fine only while it is tiny.
How do you handle a user who reads the order while the saga is in flight?
Give the aggregate a pending status that the saga flips at the end, and have reads either hide pending rows or show them as in progress. Isolation is not free with sagas; the application has to provide it.
Connections
- In study paths
- Decoupling with events
- Built on
- DAG of tasks
- Pairs with
- Make retries safe · Publish events reliably with the write · Move slow work off the request · Make sure only one worker does it · Park poison messages
- Pulls against
- Commit across two stores
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| AWS Step Functions | Saga orchestrator | Workflows | Logic Apps, Durable Functions | Temporal, Apache Airflow |
| Amazon EC2 Auto Scaling | Driver lock service, Trip service | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |
| Amazon DynamoDB | Saga log | Firestore, Bigtable | Cosmos DB | ScyllaDB, Apache Cassandra |