Part 5 · 2 chapters · ~12 min

Sagas

Why distributed transactions do not fit long business processes, sagas as sequences of local transactions, compensating actions as new entries, ordering steps by reversibility (pivot transactions), orchestration versus choreography, saga state machines, and failures inside compensations.

7

Steps and compensations

Order steps so that everything before the pivot can be compensated and everything after it cannot fail permanently (or is retried until it succeeds). For the transfer: debit and FX can be reversed; the external credit is the pivot; after it, only retryable notifications remain.

A SAGA WITH COMPENSATIONS
a cross-border transfer as steps that can each be undone
1. debit NGN walletcompensate: refund2. book FXcompensate: reverse FX3. credit via M-Pesano compensation: finalcompletedstep 3 failscompensate 2, then 1
swipe the figure sideways, or tap expand for full screen
1/5
no distributed transaction
A transfer spanning two countries' ledgers and an external rail cannot be one ACID transaction (Distributed Systems P8). A saga splits it into local steps, each committed on its own.
a sequence of local transactionsno two-phase commit
8

When compensation fails

problemapproach
a compensation fails (refund rail down)retry compensations until they succeed; they must be idempotent
the outcome of a step is unknownstatus enquiry before deciding to compensate (Ledgers part 3)
a customer sees intermediate statesdesign them: "in progress", "refunding", "refunded" (Trust course)
concurrent sagas on one accountholds and reservations instead of locks across steps