Skip to content
System Design Explorer
concepts

Two-Phase Commit

By Ankur NayakReviewed

Two-phase commit (2PC) makes a transaction that touches several databases or partitions commit everywhere or nowhere. A coordinator first asks every participant to prepare, then tells all of them to commit only if every one said yes. Its weakness is blocking when the coordinator fails, which systems like Spanner avoid by making each participant a replicated Paxos group.

Two-Phase Commit diagramComponents: Client, Coordinator, Participant A, Participant B. Connections: Client to Coordinator (commit); Coordinator to Participant A (prepare / commit); Coordinator to Participant B (prepare / commit).ClientClientCoordinatorCoordinationParticipant ASQL databaseParticipant BSQL database
Commit across two partitions

How it works

Phase 1 (prepare): the coordinator asks each participant whether it can commit. A participant that votes yes durably records the transaction's changes and locks, and promises to commit if asked. Phase 2 (commit or abort): if every participant voted yes, the coordinator records the decision and tells everyone to commit; otherwise it tells everyone to abort.

The blocking problem: a participant that has voted yes must wait for the decision. If the coordinator crashes at that moment, the participant can neither commit nor abort on its own and keeps its locks until the coordinator recovers. Gray and Lamport describe two-phase commit as the trivial version of Paxos Commit that tolerates zero faults; Paxos Commit runs a consensus instance per participant so the decision survives failures.

Spanner uses 2PC across Paxos groups: when a transaction involves more than one group, one group's leader acts as coordinator and the others as participants. Because each participant is itself a replicated Paxos group, a single machine failure doesn't leave the transaction stuck.

Key points

  • Prepare: every participant votes and makes its vote durable
  • Commit only if all vote yes; otherwise abort everywhere
  • A yes-voter holds locks until it learns the outcome
  • Coordinator failure blocks participants (plain 2PC)
  • Replicate the coordinator and participants (Paxos) to avoid blocking

When to use it, and pitfalls

Use it for

  • A transaction must update several partitions or databases atomically
  • Inside a database that controls all participants (Spanner, distributed SQL)

Watch out for

  • Across independent services: long-held locks and tight coupling; prefer sagas there
  • A single, unreplicated coordinator is a blocking point
  • Latency: at least two round trips plus durable writes on every participant

Trade-offs

Two-phase commit vs. sagas

Use 2PC inside one database system; use sagas with compensations across services.

Pros: 2PC: true atomicity and isolation; Sagas: no cross-service locks, services stay independent

Cons: 2PC: blocking and latency; Sagas: intermediate states are visible; compensations must be written

Used in

Frequently asked questions

Why is two-phase commit called blocking?

After a participant votes yes it must keep its locks until it learns the decision. If the coordinator crashes before telling it, the participant can't safely commit or abort on its own, so it waits, holding locks, until the coordinator comes back.

How does Spanner make two-phase commit practical?

Each participant in Spanner's two-phase commit is a Paxos group rather than a single machine, and one group's leader acts as the coordinator. Since every group is replicated, a single server failure doesn't block the transaction.

Sources