Distributed commit: prepare, decide, recover

Trace two-phase commit across participants, then reason about durable decisions, uncertainty, and blocking after failure. Distributed commit and workflows · Databases.

A transaction that touches one database has one local commit boundary. A transaction that changes inventory and a ledger has two local owners, yet the application still needs one outcome: both participants commit, or both abort. Visibility and isolation still depend on each local engine's transaction boundary. Distributed atomic commit coordinates that all-or-abort result across those owners. It does not make the network reliable, and it does not replace each database's own recovery log.

One transaction, several participants

Use one order, order-42, with a checkout coordinator, an inventory database, and a ledger database. A coordinator asks the participants—the resource managers that own the local work—to prepare. Each participant checks its constraints and local transaction state. A yes vote means “I can commit this prepared work later.” It does not mean “the global transaction has committed.”

One order, two resource managers

Read the steps as text

  1. For order-42, inventory durably prepares and votes yes. Ledger's account constraint fails, so ledger votes no without preparing. The coordinator has not decided yet.
  2. The coordinator evaluates yes plus no. A yes is a promise to finish, not a global decision; a no rules out commit for this attempt.
  3. The coordinator records abort durably and sends it. Inventory discards its prepared reservation; ledger has no committed entry. A separate order-43 attempt receives two yes votes.
  4. For order-43, the coordinator link fails before the decision reaches either participant. Each knows its own durable promise, but not whether a decision was recorded. A timeout cannot identify the decision.
  5. Both remain prepared and uncertain while recovery seeks the decision. Their reserved resources may block other work; neither participant can safely guess commit or abort.
  6. In this example recovery finds a durable abort decision and delivers it. Both participants apply abort and release their prepared work. Another run could instead recover a commit decision.

Two-phase commit, or 2PC, separates that readiness from the final decision. In phase one, each participant records enough durable state to survive a restart, then votes yes or no. If every vote is yes, the coordinator may record a commit decision durably and send it; the decision and each participant applying it are separate events. If any participant votes no, or fails before durably recording prepared, the coordinator records abort and sends it. The all-yes condition is the safety hinge: a participant must never commit a distributed transaction when another participant voted no.

A participant that has prepared is no longer free to forget the work. It holds whatever locks or resources its local engine requires and waits for a final decision. This is why “prepared” is a recovery state, not a synonym for committed. If a prepared participant receives COMMIT, it makes the local commit durable and acknowledges. If it receives ABORT, it rolls back the prepared local work.

When the answer is missing

Now change the dataset. Inventory and ledger both prepare, the coordinator writes no visible final decision, and the network link fails. Each participant is prepared but uncertain: it knows it must not unilaterally commit or abort, because the missing decision might be the other one. It can ask the coordinator or another valid recovery source for a durable decision when communication returns. A timeout alone proves neither commit nor abort. Until then, locks may remain held and other work can wait.

Place the failure around the decision

Read the steps as text

  1. For transfer-77 the prepare request is in flight. Neither participant has a durable prepared record, and the coordinator has no final decision.
  2. The coordinator records abort for this attempt. A separately identified retry can start later; committing only one participant would break atomicity.
  3. In another attempt both participants prepared and voted yes. The coordinator durably recorded commit, but balance lost its link before receiving it.
  4. Catalog has applied commit and acknowledged. Balance reconnects still prepared and asks for the decision; the durable coordinator record remains commit.
  5. Recovery resends the recorded commit. Balance applies it locally and acknowledges. If a decision is unavailable to a prepared participant, it may block while seeking it.
  6. Consensus can replicate the decision service. The 2PC protocol still coordinates the resource managers' prepare and completion; these are different agreement boundaries.
  7. In this recovery example both resource managers have applied the recorded commit. The decision was durable before the last participant received and applied it.

The same boundary explains several failures. If a participant fails before it prepares, the coordinator can safely abort or retry because that participant has not promised a later commit. If the coordinator learns a no vote and records abort durably, a participant that was prepared can learn abort after it returns. If the coordinator has already recorded commit, recovery can resend commit even when one participant was temporarily offline. The crucial question is not “which machine failed?” but “was the global decision durably recorded, and did this participant learn it?”

A coordinator failure before its decision can leave prepared participants blocked. This is a deliberate trade-off: 2PC protects atomicity by refusing a local guess, while availability suffers during uncertainty. WAL and recovery are therefore hard prerequisites. The participant's prepare record and the coordinator's decision record must survive restart; otherwise a restart can forget the promise or invent a conflicting outcome. This lesson describes a basic 2PC profile with durable participant promises and coordinator decisions; real engines and transaction managers differ in logging, recovery, timeout and presumed-abort optimizations. PostgreSQL, for example, exposes prepared transactions to an external transaction manager and retains their locks; it is not the coordinator by itself. This lesson leaves page-level mechanics to the recovery lesson.

A useful state table keeps the phases separate. working means a participant is still deciding whether it can prepare. prepared means its promise is durable and its local resources may remain reserved. committed and aborted are final local outcomes for this distributed transaction. A message arriving late must be interpreted against the durable state: a participant that has already committed must not reverse itself because an old abort message appears, and a participant that has prepared must not treat a missing message as permission to choose. The coordinator also needs a durable state transition, because a memory-only decision disappears in the same crash that makes participants ask for it.

Recap

  • Phase one establishes durable readiness; phase two distributes one commit or abort decision.
  • A prepared participant may block while uncertain because choosing locally could violate all-or-abort atomicity.
  • Recovery records preserve the decision boundary; 2PC's safety can reduce availability during coordinator or network failure.
Where consensus and 3PC fit

Consensus chooses an agreed value or log entry among replicas. A consensus-backed coordinator can make the decision service more fault tolerant, but that does not turn every consensus round into a distributed transaction, and 2PC still coordinates resource managers. Dale Skeen's primary nonblocking-commit work motivates a three-phase shape with an intermediate pre-commit phase; it is often described as reducing blocking under stronger timing and failure assumptions. Those assumptions matter: it is not a universal cure for partitions or arbitrary asynchronous failure, so this track treats 3PC as a qualified extension.

Next: Sagas: compensate a long-running workflow.

For practice, connect this lesson to recovery and transaction isolation before you reason about replicas or consensus. Practise this in DSA with Graph Fundamentals: model participants and failure edges as a state graph.