The compensation that undid somebody else's work
Failure story 20, in concurrency and ownership.
The challenge
Section titled “The challenge”A saga reserved two units, then failed at payment, and its compensation ran correctly. Why did another customer’s three reserved units disappear at the same moment?
The setup
Section titled “The setup”| Assumption | Value |
|---|---|
| Inventory | Product P-3 has 10 units available |
| Saga | Reserve stock, take payment, confirm. If a later step fails, earlier steps are compensated |
| Reserve step | Reads the available count, records it as the before value, and writes available − quantity |
| Compensation | Writes the before value back: “put it back the way it was” |
What happened
Section titled “What happened”| Step | Saga W1 (2 units) | Saga W2 (3 units) | Available |
|---|---|---|---|
| 1 | Reads 10; records before = 10; writes 8 | 8 | |
| 2 | Reads 8; records before = 8; writes 5 | 5 | |
| 3 | Payment declined: compensation starts | 5 | |
| 4 | Writes its before value: 10 | 10 | |
| 5 | Payment succeeds; W2 confirms 3 units | 10 | |
| 6 | The system shows 10 units available. Three of them are promised to W2’s customer, and will be sold again. | 10 |
If the compensation is retried, say because its acknowledgement was lost, it writes 10 again: still wrong, and still silent.
Why it went wrong
Section titled “Why it went wrong”The compensation undid a state instead of undoing an action. “Restore what I saw” is correct only if nobody else changed anything in between, and in a system where sagas run concurrently, somebody always might. W1’s job was to give back its own two units. Writing a remembered total gave back everyone’s changes since W1 looked.
Two designs
Section titled “Two designs”| Aspect | Design A: restore a snapshot | Design B: release by reservation ID |
|---|---|---|
| What reserving creates | A lower number | A reservation record: R-1, P-3, 2 units, held |
| Available is | A stored number | Stock minus the active reservations |
| Compensation | Writes the old number back | Marks R-1 as released, if it’s still held |
| Runs twice | Writes the old number again | The second run finds R-1 already released and does nothing |
| W2’s reservation | Erased | Untouched |
-- Compensation for saga W1: affects only its own reservation, and is safe to repeatUPDATE reservations SET status = 'released', released_at = now() WHERE reservation_id = 'R-1' AND status = 'held';The general rule: a compensation should be the semantic inverse of the action, scoped to that action’s own effects, and idempotent.
The check
Section titled “The check”Interleave the two sagas as in the timeline.
- Expected: after W1’s compensation, W2 still holds all three units and available is 7.
- Run W1’s compensation twice. Expected: the same result: 7 available, W2’s three units held.
- Run 100 sagas concurrently with random failures. Expected: available always equals stock minus the units of reservations still held.
This check is in the checks catalog as test templates in Java and Python.
When the simpler design is enough
Section titled “When the simpler design is enough”- Only one saga can touch a given item at a time, because it locks the item for the saga’s whole duration. That’s safe, and usually too slow for anything popular.
- The step’s effect is genuinely a value only that saga owns, such as a draft document it created, so restoring the previous state can’t affect anyone else.
For educators
Section titled “For educators”Discussion question. “Compensation is rollback for distributed systems.” What’s misleading about that sentence?
Exercise. W1’s reserve request timed out, so the saga doesn’t know whether the reservation was made, and it starts compensating with release R-1. The reservation service has no R-1 yet, because the reserve request is still in a queue. What happens under Design B, and what do you add?
Worked answer
The release finds nothing to update and does nothing. Then the delayed reserve arrives and creates R-1, holding two units that nobody will ever confirm or release. It’s the same problem as the cancellation that arrived before the booking. Add a record of the release: if a release arrives for an unknown reservation ID, store R-1: released, so a later reserve with that ID is refused. Add an expiry too: a reservation held for longer than the saga’s maximum duration is released automatically, which cleans up anything the first fix misses.
Further reading
Section titled “Further reading”- Microsoft’s architecture guidance on the saga pattern (opens in a new tab) and the compensating transaction pattern (opens in a new tab).
Related pages
Section titled “Related pages”Glossary: saga
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.