Skip to main content

Short note: Two-phase commit

Short answer

Two-phase commit (2PC) ensures all participants of a distributed transaction either commit or abort. The coordinator sends canCommit? requests; participants reply Yes if prepared, No otherwise. If all votes are Yes, the coordinator sends doCommit, else doAbort. Participants then execute the decision and acknowledge, ensuring atomicity despite failures, though coordinator failure may block.

Write a short note on two phase commit protocol.

The two-phase commit protocol (2PC) makes sure that all servers (participants) of a distributed transaction either all commit or all abort. One server acts as the coordinator (usually the one that the client contacted); others are participants.

Phase 1: voting

  1. The coordinator sends canCommit? to every participant.
  2. Each participant replies Yes (it has saved the transaction's changes to permanent storage and is prepared; it can no longer unilaterally abort) or No (it aborts at once).

Phase 2: completion 3. If all votes (including the coordinator's own) are Yes, the coordinator records the decision and sends doCommit; if any vote is No or a timeout occurs, it sends doAbort. 4. Participants carry out the decision and send haveCommitted to the coordinator.

Coordinator            Participants
   |--- canCommit? ------->|
   |<------ Yes / No ------|
   | (decide, log)         |
   |--- doCommit/doAbort ->|
   |<---- haveCommitted ---|

Failures

  • If a participant crashes before voting, the coordinator times out and aborts.
  • A participant that has voted Yes is in the uncertain state; if the coordinator fails it must wait (it blocks) or ask other participants for the decision.
  • On recovery, servers use their logs to complete the protocol.

Drawbacks: blocking if the coordinator fails, and extra messages and delays (about 3N messages for N participants).

✓ Answer checked by Claude Opus 5.5 (high effort)

All Distributed System chapter 8 questions →