Two Phase Commit and Sagas

Topics Covered

The Atomic Commit Problem

Atomicity across machines

How atomic commit differs from consensus

The properties we want

Where atomic commit appears

The two families of solutions

A running example

What can go wrong

Why not just retry until everyone commits?

Atomicity is not isolation

Who is the coordinator?

A brief history

What this lesson builds toward

The cost of getting it wrong

A note on terminology

A transaction that touches data on several machines must either commit everywhere or abort everywhere. If a money transfer debits an account on one shard and credits an account on another, it must never happen that only the debit sticks. This is the atomic commit problem, and it is different from consensus in an important way: every participant has a veto. This section defines the problem, explains why it is harder than it first looks and sets up the two main families of solutions covered in this lesson, two phase commit and sagas.

Atomicity across machines

On a single database, atomicity comes from the write ahead log: the transaction writes its changes and then a commit record, and recovery replays or discards changes depending on whether the commit record made it to disk. Across machines, there is no single log. Each participant has its own, and the participants must agree on whether the commit record should be written. Each participant can also fail independently, and the network can lose messages, so a participant may not know whether others committed.

How atomic commit differs from consensus

In consensus, any proposed value may be chosen, and a majority suffices. In atomic commit, the outcome must be commit only if every participant voted yes, and abort if any voted no or crashed before voting. A single participant can force an abort, for example because a constraint would be violated or a lock could not be acquired. This makes atomic commit stricter in one way, since all participants must agree to commit, and the classic protocol for it, two phase commit, is not fault tolerant in the same sense as Paxos or Raft. Combining the two, as Spanner does, produces a fault tolerant atomic commit.

Atomic commit needs every participant to agree. A single no vote, or a silent participant, forces the whole transaction to abort.

The properties we want

The atomic commit problem asks for several properties. Agreement: all participants that decide reach the same decision. Validity: commit is decided only if all participants voted yes, and if all voted yes and there were no failures, the decision is commit. Termination: every participant that does not crash eventually decides. The first two are safety properties and are achievable. The third is a liveness property, and as with consensus, it cannot be guaranteed in an asynchronous system with failures; the question is how badly the protocol behaves when failures occur.

Where atomic commit appears

Distributed databases use atomic commit for transactions that span shards: CockroachDB, YugabyteDB, Spanner, TiDB and Vitess all run some form of two phase commit across their partitions. Message brokers use it to make producing messages and committing consumer offsets atomic, as in Kafka transactions. The XA standard lets an application server coordinate transactions across a database and a message queue from different vendors. Microservices face the same problem when a business operation spans services with separate databases, though they usually choose sagas instead, for reasons this lesson explains.

The two families of solutions

Two phase commit provides true atomicity: either all participants commit or none do, and intermediate states are hidden behind locks. It requires participants to hold locks during the protocol and to trust a coordinator. Sagas give up isolation and instant atomicity in exchange for availability and autonomy: each step commits locally and immediately, and if a later step fails, earlier steps are undone by compensating actions. Most real systems use two phase commit inside a database and sagas across services, and understanding both lets you choose deliberately.

A running example

Throughout this lesson, consider an online order that must reserve inventory in an inventory service, charge a card in a payments service and create an order record in an orders service. With two phase commit, the three services would prepare and then commit together, so a customer is never charged without a reservation. With a saga, the order service would reserve inventory, then charge the card, then confirm, and if the charge fails, it would release the reservation. The second approach is how most e commerce systems actually work.

What can go wrong

Failures can happen at every step: a participant crashes after voting yes, the coordinator crashes after collecting votes but before announcing the decision, messages are lost or delayed, and a network partition separates the coordinator from some participants. Each failure must be handled without violating agreement. The rest of this lesson shows how two phase commit handles them, where it gets stuck, how fault tolerant variants avoid getting stuck and how sagas sidestep the problem by changing what atomic means.

Why not just retry until everyone commits?

A tempting shortcut is to have each participant commit locally and, if one fails, keep retrying it until it succeeds. This works only when every failure is transient. A participant that refuses because a constraint would be violated, such as an account balance going negative, will refuse forever, and the other participants have already committed. Retrying forward is the right tool for steps that are guaranteed to succeed eventually, which is exactly the idea behind retriable steps in sagas, but it cannot replace a vote when any participant may legitimately say no.

Atomicity is not isolation

Atomic commit guarantees that a transaction takes effect everywhere or nowhere. It says nothing on its own about what concurrent transactions see while the protocol runs. Two phase commit is usually paired with locking, so prepared data stays locked and invisible until the decision, which provides isolation as well. Sagas keep atomicity in the eventual sense, since the business outcome is all or nothing after compensations, but they give up isolation entirely. Keeping the two properties separate in your head makes the trade offs in this lesson much easier to reason about.

Who is the coordinator?

The coordinator can be a dedicated transaction manager, the client library that started the transaction or one of the participants. Distributed databases usually pick one participant per transaction, often the shard holding the first key written, because it saves a network hop and a separate service to operate. Application servers using XA rely on a transaction manager inside the application server. In sagas, the orchestrator plays a similar role, but it coordinates a sequence rather than a vote. Wherever it lives, the coordinator is the component whose failure determines how the protocol behaves under faults.

A brief history

Two phase commit was described in the late 1970s, with Jim Gray notes on database operating systems as the classic reference, and it became part of commercial databases and transaction monitors in the 1980s. Lampson and Sturgis studied crash recovery for it, Skeen proposed three phase commit in 1981, and the presumed abort and presumed commit optimizations came from Mohan and colleagues at IBM. Sagas were proposed in 1987 for long lived transactions in a single database and were rediscovered two decades later by the microservices community. The underlying ideas are old; the deployment contexts have changed.

What this lesson builds toward

The sections that follow move from the classic protocol to its fault tolerant forms and then to sagas, finishing with how to choose between them and run them in production. By the end, you should be able to look at any operation that spans machines and say which mechanism fits, what it costs and how it fails.

The cost of getting it wrong

Atomicity failures are among the most expensive bugs in software. A transfer that debits without crediting loses customer money, an order charged but never reserved creates refunds and support tickets, and inventory reserved for failed orders blocks real sales. These bugs are also hard to detect, because each individual system looks consistent; only comparing them reveals the problem. That is why the protocols in this lesson, and the reconciliation jobs that back them up, deserve careful design.

A note on terminology

Database texts say atomic commitment, distributed commit or global commit for the same problem, and they call participants resource managers or cohorts. The ideas are identical whatever the vocabulary.

Atomic commit gives every participant a veto

Unlike consensus, where a majority picks any proposed value, atomic commit commits only if all participants vote yes. That unanimity is why the classic protocol can block, and why many systems combine it with consensus or replace it with sagas.