Two-Phase Commit (2PC)
TechniqueDistributed SystemsMidSeniorUpdated
Coordinator asks all participants to PREPARE; if all vote yes it sends COMMIT, otherwise ABORT. Blocks if the coordinator crashes between phases.
When to Use Two-Phase Commit (2PC)
Cross-shard or cross-database atomicity where you control all participants and tolerate the blocking window. Common inside one datacenter.
In Practice
MySQL XA transactions, PostgreSQL prepared transactions, Spanner's participant leaders run 2PC on top of Paxos groups, MongoDB cross-shard transactions.
How Two-Phase Commit (2PC) Comes Up in Interviews
Scenario
A payment transfers $50 across two shards in the same datacenter and must be all-or-nothing. You control both participant databases.
2PC gives atomic commit across cooperating participants. The blocking-on-coordinator-failure window is acceptable inside one DC. Saga would also work but trades atomicity for eventual consistency with compensations.
Spanner runs 2PC over Paxos groups so each participant is itself fault-tolerant, sidestepping the classic coordinator-stuck failure mode.