Intermediate LevelSystem Design & Architecture
What is the Two-Phase Commit (2PC) protocol and why does it suffer from blocking risks?
β Interview Prompt: Explain Two-Phase Commit (2PC), Phase 1 (Prepare) vs Phase 2 (Commit/Abort), Coordinator node SPOF, and blocking lock risks.
The Two-Phase Commit (2PC) protocol coordinates atomic transactions across multiple distributed database nodes:
2 Phases:
1
Phase 1 (Prepare): Coordinator asks all participant nodes if they can commit. Participants acquire locks and vote 'YES' or 'NO'.
2
Phase 2 (Commit/Abort): If all vote 'YES', Coordinator commands all nodes to 'COMMIT'. If any vote 'NO', Coordinator commands 'ABORT'.
β’
β οΈ Blocking Risk: If the Coordinator crashes during Phase 2, participant nodes remain indefinitely blocked holding database locks!
Code Implementation Example
markdown
Two-Phase Commit (2PC) Protocol Flow:
[Coordinator] ββ(Phase 1: Prepare?)ββ> [Node A] [Node B]
[Coordinator] <-- (Vote: YES/YES) βββ [Node A] [Node B]
[Coordinator] ββ(Phase 2: Commit!)ββ> [Node A] [Node B]π‘Two-Phase Commit (2PC) Prepare and Commit phases.
π― Interview Key Takeaway
Avoid 2PC for high-throughput microservices due to coordinator blocking risks; prefer eventual consistency Saga patterns.
#System Design#2PC#Two-Phase Commit#Distributed Transactions#Intermediate
Back to all System Design & Architecture Questionsβ