Sagas and Compensating Transactions

The Worked Example

A checkout flow, five steps, five services, five databases, used consistently through this part:

  1. Reserve inventory — Inventory service decrements available stock for this SKU.
  2. Authorize card — Payments service places a hold with the issuer (see Card network). No money moves yet.
  3. Create shipment — Fulfillment service books a carrier label and a warehouse pick.
  4. Capture — Payments service converts the hold into an actual charge (see Authorize vs capture). This is the pivot.
  5. Send receipt — Notification service emails the customer.
The saga, forward path — five local transactions, five databases, one pivot
The saga, forward path — five local transactions, five databases, one pivot

Why 2PC Is Not on the Table

Most saga-vs-2PC writeups lead with blocking or availability math. For a payment flow, the disqualifying reason is simpler and comes first:

The card network is not a 2PC participant. It will not vote, it will not PREPARE, and it will not hold a lock open for you while your other services decide whether to commit. That alone ends the discussion — everything else below is why 2PC would be a bad idea even for the participants you do control.

Blocking. In 2PC, once a participant votes "yes" on PREPARE, it has made a promise it can't take back unilaterally — it must hold its lock and wait for the coordinator's COMMIT or ABORT. If the coordinator dies in that window, every participant that voted yes is stuck: it cannot commit (it doesn't know if anyone voted no), and it cannot abort (it doesn't know if everyone else already committed). It can only hold the lock and wait for the coordinator to come back. That's the blocking problem, and it's structural, not a bug you can engineer around.

Availability(2PC) = A(coordinator) × A(participant₁) × A(participant₂) × ... × A(participantₙ)
— strictly lower than the availability of the least-reliable single participant

Latency(2PC) ≈ 2 × max(RTTᵢ)
— a full prepare round trip, then a full commit round trip, gated on the slowest participant each time

Lock duration. Holding a row lock across a network call that might take seconds — exactly what an issuer round trip can take under load — is a throughput ceiling, not just a latency cost:

throughput_cap(row) = 1 / lock_hold_time

A 2-second lock held on one inventory row while 2PC waits on the issuer
→ that row can service at most 0.5 transactions/second, no matter how much
  hardware sits behind it — every other buyer of that SKU queues behind
  the lock, not behind real contention for stock

None of this means 2PC is a bad protocol everywhere. It's the right tool for a single-organization transaction across two databases you both control and can reach in single-digit milliseconds — classic XA across a primary and a replica, or two tables in the same trust boundary. It falls apart specifically when a participant is slow, unreliable, or (as with a card network) simply refuses to play.

2PCSaga
CoordinationCoordinator holds a global lock across all participants until every vote is inEach step commits locally and immediately; no global lock ever exists
Requires participant cooperationYes — every participant must implement PREPARE/COMMIT/ABORTNo — a saga step can be any local commit, including a call to a party that has no idea it's in a saga
Failure recoveryCan block indefinitely if the coordinator dies mid-protocolNever blocks — a failed step triggers compensations for what already committed
Good fitSingle org, low latency, small fixed participant set (e.g. two databases you own)Cross-org, unreliable/slow participants, a participant you don't control at all

What a Saga Actually Is

A saga is a sequence of local transactions, each committing independently and permanently the moment it runs, paired with a compensating transaction defined for every step that needs one. There is no global lock and no coordinator holding cross-service state open — only a record of which steps have committed so far, so that if a later step fails, the compensations for the already-committed steps can be triggered in reverse.

A Compensation Is Not a Rollback

A rollback erases history — after it, the database looks as if the transaction never happened. A compensation is a new forward transaction that semantically undoes the previous one, and it is externally visible. Nothing is erased; a fact is added that cancels out an earlier fact.

The refund is the canonical case: refunding a charge doesn't delete the charge. It creates a second, opposing ledger entry. The customer's statement shows both the original charge and the refund — two lines, not zero. Most real compensations are lossier than that:

RefundLeaves both statement entries; network/processing fees on the original charge are frequently non-refundable, so "compensated" still costs money
Un-reserve inventoryIf another saga took the last unit while this one was reserved, "releasing" the reservation doesn't restore stock that no longer exists
Un-send an emailImpossible outright — the only available "compensation" is a second email, which is a mitigation, not an undo

Orchestration vs Choreography

Two ways to wire the steps together, and they fail differently:

OrchestrationChoreography
Who holds the state machineA central orchestrator (service or durable workflow) that explicitly calls each stepNo one — it's implicit in the union of every service's event handlers
How you debug itRead one orchestrator instance's execution historyReconstruct the sequence from N services' logs via a correlation ID, hoping all N logged it
How failure is detectedOrchestrator gets an explicit failure response or times out waiting on a stepA service is simply waiting on an event that never arrives; nothing declares the saga failed
Failure mode at scaleSingle point of logic — a bug in the orchestrator affects every saga instance"Nobody knows the overall state" — each service knows its own slice, no one can answer "where is order 4471?"

For money flows, orchestrate. The auditability argument settles it: a regulator, a support engineer, or your own on-call self at 2am needs one place to ask "where is order 4471 right now, and what's left to do" — and get an authoritative answer, not a reconstruction project across five services' logs.

The Pivot Transaction

The pivot is the step after which the saga can only go forward, never backward. In this flow, capture is the pivot (see Authorize vs capture) — once the hold becomes an actual charge, there's no "un-charge," only a refund, which is itself a new forward transaction, not a rollback.

Naming the pivot splits every saga step into exactly three kinds, and the split is what makes the whole thing tractable:

CompensatableBefore the pivot. Has a defined undo (release inventory, void an authorization).
PivotExactly one step. Succeeds or the saga compensates backward; once it succeeds, no more compensation.
RetriableAfter the pivot. No undo needed or available — only retried until it succeeds.

This is why you want the pivot as late as possible in the sequence: every step before it stays cheap to reverse, and every step after it only has to solve "keep retrying," never "figure out how to undo." Put the pivot early and you've turned steps that used to be free to compensate into steps you're stuck honoring.

The same saga, states named by which side of the pivot they're on
The same saga, states named by which side of the pivot they're on

Semantic Locks: Sagas Have No Isolation

A saga commits each step immediately, which means another transaction can read the order mid-saga — after inventory is reserved but before payment is captured, the order row exists and looks real to anyone querying it. There is no snapshot isolating "the saga" as a unit. The standard countermeasures:

Semantic lockA status field (pending_payment) that other code must check before acting, turning an invisible intermediate state into a checkable one.
Commutative updatesreserved_qty -= 1 instead of set balance = X, so two sagas touching the same row don't clobber each other regardless of execution order.
Re-read and verifyBefore capturing, re-fetch the PaymentIntent and confirm it's still requires_capture — don't trust a status read minutes earlier that the auth window may have expired since (see Card network).
By-value schedulingRoute every saga touching a given SKU through one per-SKU queue, so overlapping sagas can't interleave on that resource at all — pessimism instead of runtime isolation.

Durability of the Saga Itself

An orchestrator that keeps saga state in a process's memory loses that state the moment the process dies mid-saga — inventory reserved, card authorized, and nothing left that knows to capture or compensate. This is exactly the transactional-outbox / durable-execution problem from the Idempotency: the machinery part: the orchestrator's own progress has to survive a crash, which means it has to be persisted transactionally with each step, not held in RAM between steps.

Compensation path when step 3 fails — and where it can get stuck
Compensation path when step 3 fails — and where it can get stuck

The Invariants

  • Guarantees: Eventual consistency across every participating service, given that compensations are retried until they succeed: every step in the saga ends up either fully completed or fully compensated, never left in an ambiguous middle state indefinitely.
  • Does not guarantee: Isolation — other transactions can observe the saga mid-flight, which is why semantic locks exist. Atomicity from an outside observer's point of view — there is no instant at which "the whole order" becomes true all at once. The ability to undo an externally visible effect — a compensation adds a new fact; it never erases the one before it, and some effects (a consumed digital good, a sent email) have no compensation at all.