advanced26 min studyLesson 261 in Engineering

Atomic commit and two-phase commit

Separate agreement on a cross-shard outcome from isolation, replication, and the availability of each participant.

On this page

What you’ll understand

  • Trace prepare, durable decision, and participant completion.
  • Explain why prepared participants cannot freely time out and abort.
  • Distinguish atomic commit from serializable isolation and replicated availability.

A transfer moves quota from one training project to another on separate database shards. Local atomic writes do not establish one outcome for the combined transfer: one debit and no credit would lose quota. Atomic commit coordinates the participants so that their final decisions agree. Understanding that requirement starts with durable promises and failure states, rather than assuming that two successful network calls amount to one transaction.

Prepare is a durable promise

In classic two-phase commit, a coordinator asks every required participant to prepare. A participant that votes yes must first ensure it can later complete the transaction after a crash, including the necessary recovery state. It has not committed yet. Its vote is a promise to honor a later valid global decision, so it cannot simply discard that promise when a timer expires. A no vote prevents a commit decision. The coordinator must know the complete participant set; silently forgetting one changes the transaction being coordinated.

After receiving all required yes votes, the coordinator durably records commit before telling participants to commit. If a participant votes no, the coordinator can decide abort according to the protocol. Participants record and apply the outcome, release retained resources when safe, and acknowledge as needed for cleanup. Logging details vary with the implementation and optimizations, but the safety argument always identifies which recoverable state authorizes each message. An acknowledgment that was sent before the corresponding persistence boundary cannot serve as durable evidence after a crash.

decideCommit⁡(T)⟹⋀i∈P(T)preparedYes⁡i(T)\operatorname{decideCommit}(T)\Longrightarrow\bigwedge_{i\in P(T)}\operatorname{preparedYes}_i(T)
P(T) is the complete finite participant set. Each Boolean preparedYes denotes the recoverable promise required by the protocol, not merely that a packet was received. This necessary condition does not by itself prove a complete implementation correct.↔ Scroll wide equations horizontally.

An unknown outcome can block

A participant that has prepared and lost contact with the coordinator may be unable to distinguish a transaction committed elsewhere from one not yet decided. Preserving atomicity can require waiting for authoritative recovery information. This is the blocking behavior of ordinary two-phase commit under relevant failures. A timeout detects missing timely communication, not the truth of the global decision. Retrying messages and querying recovered state are useful; independently inventing an outcome is unsafe when another participant could already have committed.

Replication can improve access to the coordinator’s decision record, but it introduces its own consensus and durability assumptions. A reachable majority of database replicas is not a majority vote among transaction participants. If the transaction requires three shards, two prepared shards do not authorize omitting the third. Also distinguish atomic commit from isolation: all participants can agree to commit transactions whose interleaving still violates an application invariant under weak isolation. Each property needs its own mechanism and tests.

di(T),dj(T)∈{commit,abort}⟹di(T)=dj(T)d_i(T),d_j(T)\in\{\mathrm{commit},\mathrm{abort}\}\Longrightarrow d_i(T)=d_j(T)
For required participants i and j with final decisions, atomic agreement forbids opposite outcomes. An undecided participant is outside this final-value implication; agreement alone does not imply eventual termination.↔ Scroll wide equations horizontally.

Budget the slowest required participant

Prepare work may include local validation, log persistence, and waiting for conflicting activity. A fast average shard does not remove the slowest required shard from the path. Record stage durations, the age and count of prepared transactions, and which owner will resolve each one after coordinator restart. Bound how much unrelated application work a transaction performs while retaining locks. A production runbook should identify the decision authority, not instruct an operator to resolve an old prepared transaction solely from its age.

The reliability lab illustrates a deliberately different rule. Press Reset, set replica count to 5 and unavailable replicas to 2, predict whether three available replicas meet the majority, then press Step. For a fresh comparison, Reset, explicitly set replica count to 5 and unavailable replicas to 3, predict, then Step. The diagram counts available replicas only; it has no prepared states or decision log. Explain why its majority cannot replace the requirement for every transaction participant to prepare.

Follow a three-shard prepare barrier

Three required shards receive prepare simultaneously at time zero. Their durable yes votes arrive at the coordinator at 5, 8, and 13 milliseconds. Recording the global commit decision then takes 2 milliseconds, and decision delivery takes another 3 milliseconds. Ignore other work.

  1. The coordinator has all required yes votes only at 13 milliseconds. Votes from the first two shards at 8 milliseconds are insufficient for this participant set.

  2. The durable commit record finishes at 13 + 2 = 15 milliseconds. In this stated protocol, decision messages are sent after that boundary.

  3. All participants receive the decision at 15 + 3 = 18 milliseconds. Applying their outcomes or acknowledging cleanup can take additional time not included in this trace.

The result

The earliest stated decision delivery is 18 milliseconds. If the third participant never votes, these observations cannot justify commit.

Explore the model

Interactive lab

Interactive lab · reliability

Replica majority is not participant unanimity

Press Reset, set replica count n = 5 and unavailable count k = 2, predict majority availability, then Step. Compare a fresh Reset with both n = 5 and k = 3, predicting before Step. This counts replicas, not two-phase-commit votes.

Replica majority is not participant unanimityA replica count and a majority threshold, without a consensus protocol. Playback highlights fixed replica states. Having a majority alone does not establish linearizability, durability or failure independence. Current readings: Available replicas: 4 of 5; Majority needed: 3; Majority available?: Yes. All parameters are adjustable with the labeled controls below.Majority available · need 3 of 5Playback highlights fixed statesR1 · upR2 · upR3 · upR4 · upR5 · downCyan mark: majority threshold
AvailableUnavailableMajority threshold
Available replicas
4 of 5
Majority needed
3
Majority available?
Yes
t = 0 s

A replica count and a majority threshold, without a consensus protocol. Playback highlights fixed replica states. Having a majority alone does not establish linearizability, durability or failure independence. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA replica majority can support a replicated service under further assumptions; atomic commit still follows its required participant promises.

Check your understanding

Three required transaction participants exist. Two have durably prepared yes, and the third is unreachable. What do those facts authorize in classic two-phase commit?

Try it yourself

Four required yes votes arrive at 4, 7, 9, and 12 milliseconds. The coordinator’s durable decision takes 3 milliseconds and dissemination takes 2 more. Compute decision delivery under the same simplified sequence, and identify the safety risk if commit messages were sent before the durable decision.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Find the last required vote before adding the two sequential stages.

Compare with the solution

The barrier finishes at 12 milliseconds, persistence at 15, and delivery at 17. Sending commit before its decision is recoverable could let one participant commit while a coordinator crash erases the only record authorizing that outcome. The recovery protocol must preserve agreement rather than guess from missing volatile state.

Take it with you

  • A yes vote preserves a recoverable promise until a valid decision resolves it.
  • Classic two-phase commit can block when the decision cannot be learned.
  • Atomic commit, isolation, and replica-quorum availability are distinct properties.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.