Idempotency and Sagas
Overview
When one action touches several services, there is no single database transaction to undo everything if a step fails. Idempotency makes retries safe, and sagas undo earlier steps with compensating actions.
Detailed Notes
What are idempotency and sagas
An operation is idempotent if doing it many times has the same effect as doing it once. A saga is a sequence of small local transactions, one per service, where a failure triggers compensating actions that undo the earlier steps.
The problem they solve
A traveller books a trip: reserve a flight, reserve a hotel, charge a card. Each is a different service with its own database. The flight and hotel succeed but the card is declined. No single database can roll it all back, so the traveller holds reservations they never paid for. Separately, networks lose replies, so clients retry, and a retried "charge $50" must not charge twice.
How it works
Idempotency
- Naturally idempotent: "set status to shipped" (HTTP PUT and DELETE).
- Not naturally idempotent: "add 10 to the balance", "create an order".
- Idempotency key: the client sends a unique key (such as a UUID) with the request. The server stores the key with its result; a retry with the same key returns the stored result instead of repeating the work. Payment APIs work this way.
- Message consumers deduplicate by message id, because queues usually deliver at least once (see the Messaging Systems chapter).
Sagas
Instead of a global lock-and-commit (two-phase commit: a coordinator asks every service to prepare, then tells all to commit; it blocks if the coordinator fails and hurts availability), a saga runs steps one by one. If a step fails, compensating transactions run for the finished steps: a business-level undo such as "cancel reservation" or "refund".
| Style | How it coordinates | Trade-off |
|---|---|---|
| Choreography | Services react to each other's events | Loose coupling, but the flow becomes hard to follow |
| Orchestration | A central orchestrator tells each service what to do | Clear flow and monitoring, but one more component |
Rules: every step and compensation must be idempotent (they may be retried), and sagas give eventual consistency: other users may briefly see in-between states.
A simple example
Ordering online: Create Order, Reserve Inventory, Charge Payment, Confirm Order. If Charge Payment fails, the saga runs Release Inventory, then Cancel Order. The customer clicked "pay" twice by accident? The idempotency key means only one charge happens.
System design example
An e-commerce checkout uses an orchestrator calling the inventory, payment and shipping services through queues. Each request carries an idempotency key, each service deduplicates messages, and failures trigger compensations. To avoid "database updated but event never published", write the event into the same local transaction as the data change and let a relay publish it later (the transactional outbox).
Summary
- Idempotent operations are safe to retry; use an idempotency key.
- A saga is a chain of local transactions with compensating undo steps.
- Choreography is decentralised; orchestration uses a central coordinator.
- Expect eventual consistency, not instant rollback.
Architecture Diagram
A failed payment triggers undo steps.
- 1The client sends the request with a unique key.
- 2The orchestrator calls each service in order.
- 3If payment fails, it tells inventory to release its reservation.
- 4A retry with the same key returns the stored result.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

