← Lessons

quiz vs the machine

Gold1380

Databases

Distributed Transactions Two Phase Commit

How a coordinator drives prepare and commit across many shards.

5 min read · core · beat Gold to climb

Atomic Across Shards

When a transaction touches rows on several shards, all of them must commit or none of them. Two phase commit (2PC) is the classic protocol that achieves this atomicity across nodes.

The Two Phases

  • Prepare phase: the coordinator asks every participant if it can commit. Each writes its changes durably and votes yes or no.
  • Commit phase: if all voted yes, the coordinator records a commit decision and tells everyone to finalize. If any voted no, it tells everyone to abort.

Once a participant votes yes it is bound: it must be able to commit even after a crash, so it logs the prepared state.

The Blocking Weakness

If the coordinator crashes after participants vote yes but before they learn the decision, those participants are stuck holding locks until it recovers. This blocking behavior is why distributed SQL systems replicate the coordinator state with Raft so the decision survives failures.

Key idea

Two phase commit guarantees atomicity across shards by voting then committing, but its blocking on coordinator failure pushes modern systems to replicate the coordinator.

Check yourself

Answer to earn rating on the learn ladder.

1. What happens in the prepare phase of 2PC?

2. Why is plain 2PC called a blocking protocol?

3. How do modern distributed SQL systems harden 2PC?