Question 4 of 15 · CIE-2 QB · Set 1 · 6 marks
Open in ChatGPTExplain the working of the two-phase commit protocol and analyze the reasons for its blocking behavior.
Introduction
A transaction that updates data on several nodes must end the same way on all of them. Two-phase commit (2PC) is the protocol that makes that decision atomic, with one node acting as coordinator and the rest as participants.
Phase 1: prepare (voting)
- The coordinator writes a
preparerecord to its log, then sends PREPARE to every participant. - Each participant checks that it can commit (locks held, constraints satisfied, disk available).
- It writes an
undo/redoentry and its vote to stable storage before replying. - It replies YES (can commit, but not yet committed) or NO.
Phase 2: commit or abort
- If every vote is YES, the coordinator writes
committo its log and sends COMMIT to all. - If any vote is NO, or a participant times out, it writes
abortand sends ABORT to all. - Each participant applies or undoes, writes the decision, releases locks, and acknowledges.
- The coordinator writes
completeand forgets the transaction.
Diagram


Why 2PC blocks
The protocol is blocking because a participant that voted YES holds its locks and cannot decide alone:
- Coordinator fails after collecting votes. Every participant is in the prepared state, holds locks, and does not know the decision. It must wait for the coordinator to recover, or ask another participant: and if all voted YES and none has the decision, no participant can decide.
- Participant fails after voting YES. The coordinator waits for it, blocking everyone else's locks until the participant recovers.
- Message lost in phase 2. A participant that does not receive COMMIT waits indefinitely; it cannot abort unilaterally because another may have committed.
- No timeout helps. A participant cannot time out and abort, because committing elsewhere would then be inconsistent.
The window is the gap between a YES vote and the decision. Three-phase commit removes the blocking by adding a pre-commit phase so that any participant can decide from its own state, at the cost of an extra round.
Conclusion
2PC guarantees atomicity at the cost of availability during coordinator failure: the price of never being inconsistent is being stuck.