Algorithm Search⌘K

Modeladvanced

Sagas

Also called compensating transactions, saga pattern.

A long-lived transaction written as a sequence of local transactions, each with a compensating transaction that undoes it on failure.

A saga (Garcia-Molina and Salem, 1987) is a long-lived transaction written as a sequence T1,T2,…,TnT_1, T_2, \ldots, T_n of ordinary transactions, each with a compensating transaction CiC_i that undoes the effect of TiT_i in the terms of the application. Each TiT_i commits on its own and releases its locks, so a long process does not hold locks for its whole length, and no two-phase commit across the steps is needed. The system guarantees that either the whole sequence runs, or a prefix runs followed by the compensations of that prefix in reverse order. The cost is isolation: other transactions see the results of the steps as they commit, so a saga is atomic only in a semantic sense, and the transaction anomalies between sagas are the application's problem. Sagas can be seen as open nested transactions with one level of children, and a relative of transaction chopping, which asks when such pieces need no compensation and keep serializability.

The machine

A saga. A saga is a sequence of transactions T1,…,TnT_1, \ldots, T_n and compensating transactions C1,…,Cn−1C_1, \ldots, C_{n-1}. Each TiT_i and each CiC_i is an ACID transaction of the underlying database. CiC_i undoes TiT_i semantically: a cancellation for a booking, a refund for a charge. It need not restore the old state byte for byte, since other transactions may have run in between. The last step TnT_n needs no compensation, because after it the saga is complete.

The guarantee. The system runs the saga so that the committed sequence is one of

T1 T2⋯TnorT1⋯Tj Cj Cj−1⋯C1for some 0≤j<n.T_1\, T_2 \cdots T_n \qquad \text{or} \qquad T_1 \cdots T_j\, C_j\, C_{j-1} \cdots C_1 \quad \text{for some } 0 \le j < n.

Transactions of other sagas and of other programs may run between any two of these steps.

Execution. A saga execution component runs the steps in order and writes a saga log: the start of the saga, the start and end of each step and each compensation. The log is durable, like the log of write-ahead logging. After a crash, the component reads the log and continues: it finishes or reruns the step in progress and then goes on forward or backward.

Backward recovery. If Tj+1T_{j+1} aborts, the component runs Cj,…,C1C_j, \ldots, C_1. Each compensation is retried until it commits; the model assumes that a compensation can always eventually succeed.

Forward recovery. If the saga takes save points, the component can instead roll back to the last save point, by compensations, and run the steps from there again. With a save point after every step and steps that can always be retried until they commit, the saga never compensates and always completes; this is pure forward recovery.

Example. A trip: T1T_1 books a flight, T2T_2 books a hotel, T3T_3 charges the card. C1C_1 cancels the flight and C2C_2 cancels the hotel. If the charge T3T_3 is declined, the saga runs T1 T2 C2 C1T_1\, T_2\, C_2\, C_1. Between T1T_1 and C1C_1 the seat is taken and other customers see one seat fewer; that is the missing isolation.

What it counts

The model replaces the ACID guarantees of one long transaction with weaker ones, and the measure is which of them survive.

property one long transaction saga
atomicity all or nothing all, or a prefix and its compensations
consistency each transaction keeps the constraints each step keeps the constraints
isolation serializable with other transactions none between steps
durability yes yes, for each step and for the saga log

The resource measures are those of locking. Locks are held only for the length of one step, not of the saga, so the time others wait falls from the length of the saga to the length of a step. The price is the work of compensation on failure, and the design effort to write each CiC_i.

Relation to other models

  • Flat transactions and two-phase commit. One flat transaction, or a distributed one decided by two-phase commit, gives full isolation and holds its locks to the end. A saga gives up isolation to release them early. Sagas suit processes that run for minutes or days, or that cross services that will not take part in an atomic commit protocol.
  • Nested transactions. In closed nested transactions, the effect of a child is visible only to its parent until the top level commits. In open nested transactions, a child commits at once and its effect is undone by an inverse operation, as in a saga; the multilevel theory gives conditions under which the result is serializable at the higher level, which plain sagas do not ask for.
  • Transaction chopping. Transaction chopping also splits a transaction into pieces that commit one by one, but it requires that only the first piece can roll back, so no compensation is needed, and it gives a test under which every execution stays serializable. A saga whose steps pass that test, and whose later steps never fail, is serializable.
  • Escrow. A step that reserves an amount by the escrow method has a compensation, the release, that cannot fail and commutes with other reservations.
  • Weak isolation. The isolation between steps of different sagas is close to that of read uncommitted at the level of the saga: another saga may see a booking that a later compensation cancels. Highly available transactions study what can be guaranteed without coordination in similar settings.

Known results in the model

The guarantee needs only the log and retries. If every step and every compensation is an ACID transaction, the saga log is durable, and compensations always commit in the end, then after any crash the component can bring the saga to one of the two forms of the guarantee: it reads the log, finishes or reruns the step in progress, and continues forward or backward. No locks are held across steps.

Compensation is not inversion. The state after T1⋯Tj Cj⋯C1T_1 \cdots T_j\, C_j \cdots C_1 with other transactions in between need not equal the state without the saga. If a compensation restores a saved value instead of applying an inverse change, it can erase a later update of another saga, a lost update. Compensations should be written as inverse operations, such as "add back 2 seats", that commute with other updates of the same kind.

Sagas show the anomalies of no isolation. A step of one saga can read a value that a step of another saga wrote and that a compensation later undoes; this is the dirty read of the transaction anomalies, at the level of the saga. Two sagas can read and act on the same state and both proceed, as in write skew. Applications counter this with application-level locks or states ("pending" bookings), with steps that commute, or by placing the step that can fail first.

Order of steps. Steps that can fail should come early, and steps that cannot be compensated, such as sending a message to a person, should come last. A step after which the saga never fails is called a pivot in the practice of microservices; with all such irreversible steps after the last step that can fail, the saga never needs a compensation it cannot run.

Parallel steps. Garcia-Molina and Salem also discuss sagas whose steps run in parallel. The guarantee then concerns a partial order, and recovery compensates every committed step in an order that respects it.

References

  1. Hector Garcia-Molina and Kenneth Salem, Sagas, Proceedings of ACM SIGMOD 1987, pages 249 to 259, the definition of a saga and its compensating transactions, the guarantee on the executed sequence, backward and forward recovery with save points, and the implementation on top of a database system; chapter level only
  2. Jim Gray and Andreas Reuter, Transaction Processing, Concepts and Techniques, Morgan Kaufmann, 1993, Chapter 4, Transaction Models, the sections on chained transactions, sagas and long-lived transactions, chapter level only
  3. Gerhard Weikum and Gottfried Vossen, Transactional Information Systems, Morgan Kaufmann, 2001, the chapters on multilevel and open nested transactions and on compensation, chapter level only
  4. Dennis Shasha, François Llirbat, Eric Simon and Patrick Valduriez, Transaction chopping, algorithms and performance studies, ACM Transactions on Database Systems 20(3), 1995, pages 325 to 363, the condition under which pieces of a transaction can commit one by one and keep serializability, chapter level only

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/ND2BYK/text.yaml