A chain of local transactions, with a 'compensating' step to undo each one if something fails.
A saga is a way to do one big job that spans many services, without a single big database transaction. It is a chain of small local transactions. If one step fails, the saga runs undo steps for the ones that already finished.
In a single database, a transaction is all-or-nothing. If anything fails, the database rolls back and it is as if nothing happened. With microservices, each service owns its own database. One order may touch the Order, Inventory, Payment and Shipping services, so there is no single transaction that covers all four.
A distributed lock-and-commit protocol such as two-phase commit can fix this, but it blocks, is slow, and many modern databases and message brokers do not support it. The saga pattern takes another road. It gives up instant all-or-nothing and accepts a short period where things are only partly done.
A saga is a sequence of local transactions: T1, T2, T3 and so on. Each one commits for real. For every step Ti there is a compensating transaction Ci that semantically undoes it. If T3 fails, the saga runs C2, then C1, in reverse order.
| Choreography | Orchestration | |
|---|---|---|
| Who decides the next step? | Each service, by reacting to events | A central orchestrator, by sending commands |
| Coupling | Loose, but services must know which events to listen to | Services only know the orchestrator |
| Seeing the whole flow | Hard: it is spread over many services | Easy: it is written in one place |
| Risk | A hidden tangle of events and cycles as it grows | The orchestrator can become a busy, central piece |
| Best for | Short sagas with 2 to 4 steps | Longer flows, branches and clear error handling |
| Step type | Meaning | Example |
|---|---|---|
| Compensable | Can be undone with a compensating action | Reserve stock, release stock |
| Pivot | The point of no return. After it succeeds, the saga goes forward only | Charge card and ship the parcel |
| Retryable | After the pivot. Must be retried until it works | Send the confirmation email |
A simplified orchestrated saga. Real sagas also store their progress, so a crashed coordinator can carry on where it stopped.
| Term | Meaning in easy words |
|---|---|
| Saga | A chain of local transactions with undo steps, used instead of one distributed transaction |
| Local transaction | A normal ACID transaction inside one service and its own database |
| Compensating transaction | A new action that cancels the effect of an earlier committed step |
| Choreography | A saga style where services react to each other's events, with no central boss |
| Orchestration | A saga style where a central orchestrator tells each service what to do next |
| Orchestrator | The component that holds the saga flow and its current state |
| Pivot transaction | The step after which the saga can only move forward |
| Eventual consistency | The data may disagree for a while but ends up correct |
| Idempotent | Doing it twice has the same effect as doing it once |
| Semantic lock | A status flag (such as PENDING) that warns others the record is mid-saga |
| Saga log | Stored record of the saga's progress, used to recover after a crash |
| Choice | We gain | We lose |
|---|---|---|
| Saga instead of 2PC | No blocking, better scale and availability | Strict all-or-nothing and isolation |
| Choreography | Simple start and loose coupling | A hard-to-follow flow as it grows |
| Orchestration | Clear flow and central error handling | A central component to build and run |
| Undo fast with compensations | Quick recovery | Customers may briefly see the in-between state |
| Retry forward | No need to write an undo | Only works when the failure is temporary |