Skip to content
SEE Exam Prep

Question 6 of 11 · CIE-2 QB · Set 1 · 6 marks

Open in ChatGPT

Explain 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)

  1. The coordinator writes a prepare record to its log, then sends PREPARE to every participant.
  2. Each participant checks that it can commit (locks held, constraints satisfied, disk available).
  3. It writes an undo/redo entry and its vote to stable storage before replying.
  4. It replies YES (can commit, but not yet committed) or NO.

Phase 2: commit or abort

  1. If every vote is YES, the coordinator writes commit to its log and sends COMMIT to all.
  2. If any vote is NO, or a participant times out, it writes abort and sends ABORT to all.
  3. Each participant applies or undoes, writes the decision, releases locks, and acknowledges.
  4. The coordinator writes complete and forgets the transaction.

Diagram

Phase 1: the coordinator sends Prepare to every participant and each replies ready or not ready

Phase 2: the coordinator sends commit or abort to every participant

Why 2PC blocks

The protocol is blocking because a participant that voted YES holds its locks and cannot decide alone:

  1. 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.
  2. Participant fails after voting YES. The coordinator waits for it, blocking everyone else's locks until the participant recovers.
  3. Message lost in phase 2. A participant that does not receive COMMIT waits indefinitely; it cannot abort unilaterally because another may have committed.
  4. 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.