The checkout saga reserves stock, charges the card and books a courier. One afternoon the courier step fails for a remote address, and the compensations run as designed: refund the card, release the stock. The team calls it a rollback. A week later, the warehouse finds its stock counts drifting. The release step had written back the quantity it read before the reservation, erasing a delivery that was booked in between. And a customer who tried to buy the last unit during those seconds was told it was sold out.
The common misconception
The belief: a saga is the microservices version of a transaction. If any step fails, the compensations roll everything back, as if the process had never started.
The vocabulary invites it. Microsoft's page is titled "Saga distributed transactions pattern", and the diagrams draw each compensation as a mirror image of its step. Chris Richardson's description sounds like atomicity too: the saga finishes when "either all steps complete successfully or, if a local transaction fails because it violates a business rule, the changes made by the completed steps are undone by compensating transactions." That part is true. What a doesn't give you is everything else a database transaction does: isolation, and an undo that restores what was there.
What actually happens
Every step commits for real
Each step is a local transaction in one service, and it commits before the next step starts. Microsoft lists the consequence plainly: "Data can't be rolled back because saga participants commit changes to their respective databases." Between the first step and the last, the rest of the system sees a half-finished process.
That was the design from the start. Hector Garcia-Molina and Kenneth Salem introduced sagas in 1987 as long-lived transactions that could be written "as a sequence of transactions that can be interleaved with other transactions", noting that "other transactions might see the effects of a partial saga execution." Interleaving was the point: no step holds locks while waiting for the others.
No isolation means anomalies
Richardson lists the "Lack of isolation (the “I” in ACID)" as a drawback. Microsoft names the typical anomalies:
Lost updates: one saga overwrites changes made by another, as the release step did to the delivery.
Dirty reads: a saga or user reads data another saga has changed but not finished with, like the "sold out" answer based on a reservation that was later released.
Fuzzy reads: different steps of one saga read different values because something changed between the reads.
None of these is a bug in your saga framework. They follow from committing each step separately.
A compensation is a new action, not an undo
The 1987 paper defines it carefully: a "undoes, from a semantic point of view, any of the actions performed" by its step, "but does not necessarily return the database to the state that existed" before it. Its example is a seat reservation: the compensation cancels the reservation, but it can't store the old seat count, because other transactions may have changed the count in between. Microsoft's Compensating Transaction pattern says the same: restoring the original state "can overwrite changes from other concurrent application instances."
Compensations also carry business rules. Microsoft's example: "canceling a flight reservation might not entitle the customer to a complete refund." And some steps can't be compensated at all. The paper's extreme case: "if a transaction fires a missile, it may not be possible to undo this action." An email, a payout or a parcel handed to a courier are everyday versions.
Compensations fail too
A compensation is just another step in another service, with the same failure modes. Microsoft: "Compensating transactions are eventually consistent operations and can fail." Each step should be "an idempotent command" that can be retried, and "Sometimes manual intervention is the only way to recover from a failed step."
The fix: compensate with business operations
C# · ReserveStock.cs
using Microsoft.EntityFrameworkCore;public sealed class ReserveStock(InventoryDb db){ public async Task CompensateAsync(SagaStep step, CancellationToken ct) { var item = await db.Items.SingleAsync(i => i.Sku == step.Sku, ct); // "Roll back" by restoring the value read before the reservation. // A delivery committed in between is silently erased item.Available = step.AvailableBefore; await db.SaveChangesAsync(ct); }}
C# · ReserveStock.cs
using Microsoft.EntityFrameworkCore;public sealed class ReserveStock(InventoryDb db){ public async Task CompensateAsync(Guid reservationId, CancellationToken ct) { var r = await db.Reservations.FindAsync([reservationId], ct); if (r is null || r.Released) return; // already compensated: no-op var item = await db.Items.SingleAsync(i => i.Sku == r.Sku, ct); item.Available += r.Quantity; // reverse only our own effect r.Released = true; // Available is a concurrency token: a clash throws, and the step retries await db.SaveChangesAsync(ct); }}
The fixed compensation releases one specific reservation instead of restoring a snapshot, so a delivery booked in between survives. It's idempotent: run it twice and the second run finds the reservation already released. And the item's Available column is an optimistic concurrency token, so a concurrent change makes the save fail and retry instead of overwriting it. Reservations also act as what Microsoft calls a semantic lock: other processes can see that stock is held by a saga that hasn't finished, rather than mistaking it for sold. Each step still has to update its database and publish its next message atomically, which is the outbox article's job, and receive messages idempotently, as the article on idempotent consumers shows.
Database transaction
Saga
All or nothing
The database guarantees it
All steps, or compensations you write
Others see work in progress
No, until commit
Yes, after every step
Undo
Rollback restores the old state
A new business action; old state isn't restored
A failing undo
Handled by the database
Retry it idempotently, or escalate to a person
Duration
Short, holding locks
Seconds to days, holding no locks
How to apply it in your project
Write the saga down as a tableFor every step, list its compensation and whether it's compensable, the pivot, or retryable. A step with no compensation must come after the pivot.
Make compensations business operationsCancel, release or refund the specific thing a step created, by its id. Never restore a value read earlier.
Choose countermeasures for isolationUse pending states, updates that commute such as increments, and rereads before acting, wherever a lost update or a dirty read would hurt.
Plan for failing compensationsMake every step idempotent, record saga progress, and alert a person when a compensation keeps failing.
Key takeaways
A saga is a sequence of local transactions that each commit; it isn't a distributed transaction.
Without isolation, other processes see intermediate states, so lost updates and dirty reads are possible by design.
A compensation is a new business action that reverses a step's effect; it never restores old values.
Compensations can fail, so make them idempotent, retry them and escalate when they keep failing.
Order steps around the pivot: compensable first, irreversible steps only after everything can succeed.
التعليقات (0)