Consistent Hashing Explained
How consistent hashing works, why it beats modulo hashing when servers change, how virtual nodes balance load, and where it’s used in caches and databases.
The saga pattern explained: local transactions and compensating actions, choreography vs orchestration, the outbox pattern and isolation pitfalls.

The saga pattern manages a business transaction that spans several services, each with its own database, without a distributed lock or two-phase commit. The process is split into a sequence of local transactions. Each step commits in its own service and triggers the next. If a step fails, the saga runs compensating actions that undo the earlier steps in business terms, such as refunding a payment or releasing reserved stock. The result is eventual consistency instead of one atomic commit.
Inside one database, an ACID transaction makes a multi-step change atomic; our guide to transaction isolation levels explains what it guarantees. Across services, a single transaction would need two-phase commit (2PC), which holds locks while waiting on every participant, blocks if the coordinator fails and is often unsupported by message brokers and modern databases. Sagas, first described by Garcia-Molina and Salem in 1987 for long-lived database transactions, avoid holding locks across the whole process.
PENDING.CONFIRMED.If inventory can’t reserve the items at step 3, the saga compensates: refund the payment (undoing step 2) and mark the order CANCELLED (undoing step 1).
| Step | Action | Compensation |
|---|---|---|
| 1 | Create order (pending) | Cancel order |
| 2 | Charge payment | Refund payment |
| 3 | Reserve inventory | Release reservation |
| 4 | Schedule shipping | Cancel shipment |
Compensations are business operations, not database rollbacks: a refund is a new transaction that leaves an audit trail, which is exactly what finance teams want.
Choreography: each service listens for events and reacts. The order service emits OrderCreated; payment hears it, charges the card and emits PaymentCompleted; inventory hears that, and so on.
Orchestration: a saga orchestrator tells each service what to do and tracks the state, stepping forward or compensating backward.
| Choreography | Orchestration | |
|---|---|---|
| Control | Distributed, via events | Central coordinator |
| Visibility | Low | High |
| Coupling | Services know each other’s events | Services know the orchestrator’s commands |
| Best for | Short sagas, 2–4 steps | Longer or frequently changing flows |
Workflow engines such as Temporal, AWS Step Functions and Camunda provide durable orchestration so you don’t have to build state machines by hand.
A step usually has to update its database and publish an event. Doing those separately risks one succeeding without the other. The transactional outbox pattern writes the event to an outbox table in the same local transaction as the business change; a relay then publishes outbox rows to the broker. Our comparison of Kafka, RabbitMQ and SQS covers the broker side. Because relays and brokers deliver at least once, every handler must be idempotent; see idempotency keys.
Sagas give up isolation: other requests can see intermediate states, such as an order that is charged but not yet confirmed. Common countermeasures:
Test the unhappy paths deliberately: make each step fail in turn and check that compensations leave every service consistent. In production, track how many sagas are in progress, how long each step takes and how often compensations run. A sudden rise in compensations is often the first sign that a dependency is failing.
Use them when a business process genuinely spans services that own their own data. If every step touches the same database, a local transaction is simpler and safer. Sagas pair naturally with event sourcing and CQRS, and the need for them is one of the real costs weighed in our microservices vs monolith framework.
No. Two-phase commit makes all participants commit atomically while holding locks. A saga commits each step immediately and uses compensations to undo them if something fails later.
Retry it, since compensations should be idempotent. If it keeps failing, alert a human and park the saga in a visible failed state; never drop it silently.
Choreography suits short, stable flows. Orchestration is easier to understand and change once a saga has more than a few steps.
Every article is edited by a human and checked against our editorial policy. Spotted a mistake? Tell us.
How consistent hashing works, why it beats modulo hashing when servers change, how virtual nodes balance load, and where it’s used in caches and databases.
The CAP theorem and its extension PACELC explained: consistency, availability and partition tolerance, what the trade-offs mean in practice and common myths.
How CDNs work: edge locations, request routing, cache keys, Cache-Control headers, invalidation, origin shielding, security features and edge compute.