Lesson 5 of 14

Two-Phase Commit

Two-Phase Commit (2PC)

A distributed transaction must either commit on all participating nodes or abort on all of them — never a partial commit. Two-Phase Commit (2PC) is the classic protocol to achieve this.

The Two Phases

Phase 1 — Prepare (Voting):

  1. The coordinator sends a PREPARE message to all participants.
  2. Each participant checks if it can commit (checks its local state, writes to a WAL).
  3. Each participant votes YES (ready) or NO (abort).

Phase 2 — Commit (Decision):

  • If all votes are YES: coordinator sends COMMIT to all participants.
  • If any vote is NO (or a timeout): coordinator sends ABORT to all participants.
Coordinator          Participant A      Participant B
    │── PREPARE ──────────►│                 │
    │── PREPARE ───────────────────────────►│
    │◄── YES ──────────────│                 │
    │◄── YES ──────────────────────────────│
    │── COMMIT ────────────►│                │
    │── COMMIT ────────────────────────────►│

The Problem

2PC has a blocking failure mode: if the coordinator crashes after Phase 1 but before sending the commit/abort decision, participants are stuck — they cannot commit or abort without the coordinator. Modern protocols like 3PC and Paxos/Raft solve this.

Your Task

Implement a TwoPhaseCommit coordinator class. It should:

  • Accept a list of participant functions (each returns true for YES or false for NO when prepared).
  • prepare() — call all participants, collect votes.
  • commit() — if all voted YES, commit (return "COMMITTED"). Otherwise abort (return "ABORTED").
JavaScript loading...
Loading...
Click "Run" to execute your code.