Distributed Systems Topic
Two-Phase Commit (2PC)
Learn the Two-Phase Commit protocol for achieving atomicity in distributed transactions.
Two-Phase Commit (2PC) is a distributed consensus protocol that ensures all participants in a transaction either commit or abort together, maintaining atomicity.
Overview
2PC ensures atomicity: either all nodes commit the transaction, or all abort. It's a blocking protocol with a coordinator and participants.
Protocol Phases
Phase 1: Prepare (Voting)
- Coordinator sends "prepare" message to all participants
- Each participant:
- Writes transaction to log (prepare record)
- Votes "yes" (ready to commit) or "no" (must abort)
- Sends vote to coordinator
- Coordinator collects votes
Phase 2: Commit/Abort (Decision)
-
If all vote "yes":
- Coordinator writes "commit" to log
- Sends "commit" to all participants
- Participants commit and send acknowledgment
-
If any votes "no":
- Coordinator writes "abort" to log
- Sends "abort" to all participants
- Participants abort and send acknowledgment
Implementation
1
Examples
Database Replication with 2PC
1
Common Pitfalls
- Coordinator failure: Participants block indefinitely. Fix: Use timeouts, elect new coordinator, or use 3PC
- Network partition: Cannot proceed if participants unreachable. Fix: Use majority-based commit or eventual consistency
- Not logging state: Cannot recover from failures. Fix: Write all state to durable log
- Blocking behavior: Participants wait for coordinator. Fix: Use timeouts, consider alternative patterns
- Single point of failure: Coordinator is critical. Fix: Use coordinator replication or alternative protocols
Interview Questions
Beginner
Q: What is Two-Phase Commit and how does it work?
A: Two-Phase Commit (2PC) is a protocol that ensures all participants in a distributed transaction either all commit or all abort.
How it works:
- Phase 1 (Prepare): Coordinator asks all participants if they can commit. Participants vote yes/no.
- Phase 2 (Commit/Abort):
- If all vote yes: Coordinator tells everyone to commit
- If any votes no: Coordinator tells everyone to abort
Goal: Atomicity - all nodes agree on the outcome.
Intermediate
Q: What are the limitations of 2PC? How would you address them?
A:
Limitations:
- Blocking: If coordinator fails, participants block waiting for decision
- Single point of failure: Coordinator is critical
- Not partition-tolerant: Requires all nodes to be reachable
- High latency: Multiple round trips (prepare + commit)
- Synchronous: All participants must respond
Solutions:
- Timeouts: Participants timeout and abort if coordinator doesn't respond
- 3PC: Three-Phase Commit reduces blocking (adds pre-commit phase)
- Saga pattern: Use compensating transactions for eventual consistency
- Paxos/Raft: Use consensus algorithms for better fault tolerance
- Majority commit: Commit if majority agrees (sacrifice some consistency)
When to use: Short transactions, strong consistency required, all nodes must agree.
Senior
Q: Design a fault-tolerant 2PC system. How do you handle coordinator failures, participant failures, and network partitions? How do you ensure no data loss?
A:
Fault-Tolerant 2PC Design:
1
Handling Failures:
-
Coordinator failure:
- Participants timeout and can abort
- Or elect new coordinator to recover
- New coordinator reads log, queries participants, makes decision
-
Participant failure:
- Coordinator continues with remaining participants
- Failed participant recovers from log on restart
- Can query coordinator or other participants for decision
-
Network partition:
- Majority partition can proceed (if configured)
- Minority partition blocks or aborts
- Resolve conflicts when partition heals
Data Loss Prevention:
- Durable logging: Write all state to persistent log before responding
- Write-ahead log (WAL): Log before applying changes
- Replication: Replicate coordinator log for high availability
- Quorum: Require majority for commit decisions
Key Takeaways
2PC ensures atomicity: All participants commit or all abort
Two phases: Prepare (voting) and Commit/Abort (decision)
Blocking protocol: Participants wait for coordinator
Coordinator is critical: Single point of failure
Not partition-tolerant: Requires all nodes reachable
Use for: Short transactions, strong consistency, all-or-nothing requirements
Alternatives: 3PC (less blocking), Saga (eventual consistency), Paxos/Raft (better fault tolerance)
Related Topics
What's next?
Keep exploring
Partial failure and consistency show up together in real systems. Continue with the next hub topic that stresses the same idea.