What is the two-phase commit (2PC) protocol?
Why Interviewers Ask This
This question targets practical, hands-on experience with System Design. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.
Answer
The Two-Phase Commit (2PC) protocol ensures atomic commit across multiple distributed nodes — all commit or all rollback. Used when a transaction spans multiple databases or services. Participants: coordinator (transaction manager) and participants (resource managers — databases). Phase 1 — Prepare: coordinator sends "Prepare" to all participants; each participant executes the transaction locally but doesn't commit — writes to WAL, acquires locks; each participant responds "Yes, I can commit" or "No, I cannot commit." Phase 2 — Commit/Abort: if all participants voted Yes → coordinator sends "Commit" → all participants commit and release locks; if any participant voted No → coordinator sends "Abort" → all participants rollback. Problems with 2PC: (1) Blocking: if the coordinator crashes after Phase 1 but before Phase 2, participants are stuck waiting with locks held — the system is blocked until the coordinator recovers; (2) Latency: two network round trips per transaction — slow; (3) Single point of failure: coordinator failure blocks the system. Alternatives: Saga pattern (eventual consistency, no distributed locks), 3PC (three-phase commit — adds pre-commit phase to avoid blocking, but not widely used), consensus-based protocols (Paxos, Raft — for replicated state machines). When 2PC is used: relational databases (XA transactions), some distributed databases, financial systems where ACID across services is required. The CAP theorem explains why 2PC is unavailable during network partitions.
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex System Design answers easy to follow.
Previous
How would you design a search engine like Elasticsearch?
Next
What is the Raft consensus algorithm?