GO TO A SECTION

↑ ↓ to navigate · Enter to openExplore all sections →
FDEInterviews logo
SIGMOD 1987

Sagas

Split a long transaction into steps that each commit on their own, each with a compensating step that undoes it in business terms. The 1987 pattern behind multi-step agent actions.

Breakdown by FDE Interviews · 11 min read · Updated

About the original · 1987
AUTHORS
Garcia-Molina, Salem
FROM
Princeton University
PUBLISHED
May 1987 · SIGMOD 1987
IN THIS BREAKDOWN · 10 SECTIONS

You can read 23% of this breakdown.

01

The paper in one minute

A long-lived transaction (LLT) takes hours or days: producing monthly bank statements, processing an insurance claim, booking several flights while the customer decides between each one. As an ordinary atomic transaction, it holds locks on everything it touched until it commits. Short transactions queue behind it. Deadlocks become far more likely: in the analysis the authors cite, deadlock frequency grows with the fourth power of the number of objects a transaction touches. And a transaction that runs for hours is more likely to be hit by a crash and aborted.

Hector Garcia-Molina and Kenneth Salem at Princeton observed that many LLTs do not need full atomicity. A booking transaction can let others reserve seats on a flight right after it reserves its own. What it needs is that the whole booking either completes, or any reservations it made are cancelled.

They named the structure a saga: a sequence of ordinary transactions T1 … Tn that may interleave with other transactions. Each Ti except the last has a compensating transaction Ci that undoes it semantically. To undo a seat reservation, Ci subtracts one from the reservation count; it does not write back the count Ti saw, because other transactions may have changed it since. The database guarantees that either T1, …, Tn all execute, or T1, …, Tj execute followed by Cj, …, C1.

There is no isolation at the saga level: other transactions can see a saga's partial results, and when a step is compensated, nobody tells them. A saga is like a nested transaction with only two levels and no atomicity at the top.

The paper then works through the machinery: commands for programs to declare sagas and their compensations, storing saga code reliably, backward recovery (compensate) and forward recovery (resume from a save-point), what to do when a compensation itself fails, how to run sagas on an existing database with tables and a background daemon, parallel sagas, and how to design databases and transactions so that long work can be written as sagas.

THE IDEA, VISUALLY
A SAGA: COMMIT EACH STEP, KEEP A WAY BACK T1 commits C1 T2 commits C2 T3 commits C3 Tn commits no Cn needed either T1 T2 … Tn or T1 … Tj then Cj … C1 (j < n) COMPENSATION IS SEMANTIC (THE AIRLINE EXAMPLE) C: subtract one seat A books (11), B books (12), A cancels → 11. B kept. restore old count A saw 10 … writes back 10. B's seat silently lost. No isolation: other transactions may read a saga's partial results, and nobody tells them when a step is compensated. A saga is a nested transaction with two levels and no atomicity at the top. Commands: begin-saga, begin/end-transaction (naming its C), abort-transaction, abort-saga, save-point, end-saga. WHY LONG TRANSACTIONS HURT They hold locks for hours or days, block short transactions, and abort more often: deadlock frequency grows with the fourth power of transaction size (Gray's analysis). 2× the objects → 16× the deadlocks RECOVERY Backward abort current step, run Cj … C1 Backward/forward compensate to a save-point, restart Pure forward no compensations: every step retried until it succeeds Stuck compensation alternate block, or a human fixes and reruns BUILDING AND DESIGNING SAGAS Inside the DBMS: a saga execution component sharing the log. On an existing DBMS: subroutines write saga tables; a saga daemon scans them after a crash and finishes the work. Split along real-world steps (graduation: library, bills, address). Keep in-between state in the database: funds in transit. Hard to undo: send a second letter, stop a check. A missile: no.
A saga commits each step and releases its locks, keeping a compensating step for each. The system guarantees that all steps finish or that finished ones are compensated in reverse. Compensation undoes meaning, not bytes, and nothing hides a saga's partial results from others.
Research notes: the problem, approach and results
size⁴

growth of deadlock frequency with transaction size in the analysis the paper cites, so long transactions are disproportionately costly

Cj … C1

compensations run in reverse order of the completed steps on abort

0

isolation between a saga's steps: others can see partial results and are not told when they are compensated

THE PROBLEM

Long-lived transactions (monthly statements, insurance claims, multi-stop bookings that wait for the customer) hold locks for hours or days. Other transactions wait, deadlocks and aborts multiply, and a long transaction is more likely to be hit by a crash itself.

THE IDEA

If the long transaction can be split into a sequence of ordinary transactions that may interleave with others, run it as a saga: each step commits on its own, and each step has a compensating transaction that semantically undoes it. The system guarantees either all steps run, or the completed ones are compensated in reverse order.

THE RESULT

A simple mechanism, buildable inside a database or on top of an existing one, that trades isolation for availability: locks are released after each step. The paper covers backward and forward recovery, save-points, failing compensations, parallel sagas and how to design data and transactions so that sagas are possible.

02

Why it matters to an FDE

An agent that books travel, provisions accounts, places orders or updates records across several systems is running a long-lived transaction across services that do not share a transaction manager. There is no global rollback. Sagas are the pattern that makes such work safe.

Every state-changing tool needs a compensation. For each action the agent can take (create a ticket, reserve a room, charge a card), define the action that undoes it in business terms (close the ticket, cancel the reservation, issue a refund), and when it may run.

Compensate, do not restore. Writing back the value you saw before is wrong whenever anyone else touched the record since. Undo the meaning of your step.

Expect others to see half-done work. Between steps, other users and agents can read your partial results. Design the in-between states so that they are valid, as the paper's "funds in transit" advice does.

Some actions cannot be undone. The paper's example is firing a missile; for agents it is sending an email or a payment. Put irreversible steps last, or behind human approval.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 99 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦1 results table
  • ?5 interview questions with answers
  1. 03Key takeaways

    The guarantee, the commands, recovery modes, failure handling and the misreadings to avoid

    Free account
  2. 04How it works

    The saga log, backward and forward recovery, running on an existing database, parallel sagas

    Premium
  3. 05The numbers that matter

    The few quantitative claims, and the design guidance that replaces measurement

    Premium
  4. 06Try it yourself

    Compare compensation styles, crash-test a saga executor, and measure lock waits

    Premium
  5. 07Where it breaks

    Isolation anomalies, irreversible actions, compensation bugs and the jump to many services

    Premium
  6. 08Using it on a deployment

    Designing an agent's multi-step actions as sagas

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →