Two-Phase Commit and Distributed Transactions
How a coordinator gets several databases to commit or abort one transaction together, why the protocol can block, and when a saga is the better choice.
Overview
A distributed transaction changes data in more than one place, for example two databases, or a database and a message broker, and must take effect everywhere or nowhere. Each database can make its own part atomic, but none of them can see the others. Something has to make sure they reach the same decision.
Two-phase commit (2PC) is the classic protocol for this. One node, the coordinator, asks every participant whether it can commit. If all say yes, it tells them all to commit; if any says no or does not answer, it tells them all to abort. The protocol is simple and correct, and it is still in use: the XA standard, PostgreSQL's PREPARE TRANSACTION and Google Spanner's cross-shard commits are all built on it.
Its weakness is just as well known. A participant that has voted yes has given up the right to decide alone. If the coordinator then fails, that participant waits, holding its locks, until the coordinator returns. This is the blocking problem, and it is the reason 2PC is used carefully inside one system and rarely between services owned by different teams.
What you will be able to do
- Walk through both phases, including what each side writes to disk and when.
- Explain every failure case and how recovery resolves it.
- Explain why 2PC blocks, and how three-phase commit, Paxos Commit and Spanner deal with that.
- Choose between 2PC, a saga and a redesign that avoids the cross-database write.
Key concepts
- Atomic commitment
- Coordinator and participants
- Prepare and vote
- Forced log writes
- In-doubt participants
- The blocking problem
- Three-phase commit
- Paxos Commit
- XA and PREPARE TRANSACTION
Interview questions
- Walk me through two-phase commit.
- Why is 2PC called a blocking protocol?
- What happens if a participant crashes after voting yes?
- Why do microservice designs usually avoid 2PC?
- How does Spanner do cross-shard transactions if 2PC blocks?
- What is the difference between 2PC and Paxos or Raft?
- A write must update a database and publish an event. Is 2PC the answer?
- How would you find and fix transactions stuck in the prepared state?
Sign up free to read the full topic
More System Design topics
Start free on Cariara · Coding problems · System design studio