Course contentsShow
← Databases & SQL
Lesson 1688 of 1,95316. Distributed Data ArchitecturesFree lesson

Three-Phase Commit (3PC) Protocol

Evaluate non-blocking protocols designed to improve the availability of distributed transactions.

In this lesson, you will learn how the Three-Phase Commit (3PC) protocol introduces a "prepared-to-commit" state to eliminate the blocking risks inherent in traditional two-phase commit mechanisms.

The Problem with 2PC

As we discussed in the Two-Phase Commit (2PC) protocol, the system is blocking. If the coordinator fails after a participant has voted "yes" but before the commit/abort decision is broadcast, that participant remains locked, holding resources indefinitely. This violates our availability requirements during distributed transactions.

The 3PC Solution: Non-Blocking via State Separation

3PC addresses this by splitting the commit phase into two distinct sub-phases, adding an intermediate "Pre-Commit" state. The protocol follows these steps:

  1. CanCommit: The coordinator asks participants if they can commit. If all reply "Yes," we move forward.
  2. PreCommit: The coordinator sends a PreCommit message. Participants acknowledge this, acknowledging that they are prepared to commit. Crucially, if a participant is in this state, it knows that all other participants have voted "Yes."
  3. DoCommit: The final command to execute the transaction.

Why 3PC is Non-Blocking

By introducing the PreCommit stage, 3PC ensures that if the coordinator fails, the participants have enough information to reach a consensus without waiting for the coordinator to recover.

If a participant is in the PreCommit state and the coordinator becomes unreachable, the participants can safely assume that everyone else also reached the PreCommit state. They can elect a new coordinator and proceed to commit the transaction. Conversely, if no participant has received a PreCommit message, they can safely abort.

-- Conceptual 3PC Logic
IF participant_state == PRE_COMMIT THEN
    -- Even if coordinator is dead, we know others are ready
    EXECUTE COMMIT;
ELSE IF participant_state == CAN_COMMIT THEN
    -- Safe to abort, as no one else has moved to PreCommit
    EXECUTE ABORT;
END IF;

While 3PC offers better availability than 2PC, it remains sensitive to network partitions. If a partition occurs, the protocol may still struggle to maintain consistency without manual intervention or advanced failure detectors.

Key Takeaway: The Three-Phase Commit protocol improves distributed system availability by adding a "PreCommit" buffer, allowing participants to reach a consensus on whether to commit or abort even if the original coordinator fails.