Course contentsShow
← Computer Science
Lesson 2653 of 2,87259. Consensus and ReplicationFree lesson

Two-Phase Commit Protocol

Study the basic two-phase commit (2PC) protocol for atomic commitment and its coordinator failure vulnerability.

Two-Phase Commit Protocol

You'll learn: How the two-phase commit (2PC) protocol achieves atomic commitment across distributed nodes and why coordinator failure remains its Achilles' heel.

The Core Problem

When a distributed transaction spans multiple databases or services, you need all participants to either commit or abort together—no partial success allowed. The two-phase commit protocol solves this atomic commitment problem, ensuring "all or nothing" semantics.

How 2PC Works

The protocol uses a coordinator and multiple participants:

Phase 1 – Prepare (Voting Phase):

  1. Coordinator sends a PREPARE request to all participants
  2. Each participant prepares the transaction (locks resources, writes to log)
  3. Participants reply with VOTE-COMMIT (ready to commit) or VOTE-ABORT

Phase 2 – Commit (Decision Phase):

  1. If all votes are commit, coordinator sends COMMIT to everyone
  2. If any vote is abort, coordinator sends ABORT to everyone
  3. Participants execute the decision and acknowledge
  4. Coordinator completes the transaction

Think of it like a wedding ceremony: the officiant (coordinator) asks "Do you...?" to both parties (participants). Only if both say "I do" does the officiant pronounce them married. One "no" cancels everything.

The Coordinator Failure Vulnerability

Here's the critical weakness: if the coordinator crashes after sending PREPARE but before broadcasting the final decision, participants are left in limbo. They've voted and locked resources but don't know whether to commit or abort. They must wait (potentially indefinitely) for the coordinator to recover—this is called blocking.

Unlike crash failures in replication where replicas can proceed, 2PC participants cannot safely make progress without the coordinator's decision, violating liveness under certain failure scenarios.

Key Takeaway: Two-phase commit guarantees atomic commitment across distributed nodes through a prepare-then-decide protocol, but coordinator failure can block participants indefinitely, making it unsuitable for systems requiring high availability during failures.