liveBook · Manning

Two-phase commit

As explained in Think Distributed Systems

A distributed transaction commit protocol that coordinates commitment in two phases among participating processes.

Two-phase commit (2PC) is an atomic commit protocol for distributed transactions. It coordinates multiple resource managers so every local transaction commits together or aborts together. Without failures, it provides safety and liveness; with coordinator failure, it preserves safety but may block. section 6.3 section 6.4

The system has exactly one transaction coordinator (TC) and at least two resource managers (RMs). A client performs reads and writes through the RMs, then asks the TC to commit or abort the global transaction. section 6.3.1

2PC proceeds in two phases:

  • Prepare: the TC persistently logs Prepare and asks every RM to vote. An RM may persistently record Vote-to-Commit, or persistently record Abort and abort locally. section 6.3.1
  • Commit: if every RM votes to commit, the TC persistently logs Commit and instructs all RMs to commit. Any Abort vote or timeout makes the TC log Abort and instruct all RMs to abort. section 6.3.1
  • The preparation asymmetry is deliberate: an RM can unilaterally abort, but it cannot unilaterally commit. section 6.3.1

Coordinator failure after a vote

Suppose all RMs vote to commit, the TC tells RM1 to commit, and then the TC fails. RM2 cannot safely choose commit or abort because it does not know whether another RM has already committed, so it remains blocked until the TC is repaired. section 6.3.2

Figure 6.6 2PC protocol execution
A diagram of a process

AI-generated content may be incorrect.

Use the protocol execution figure to trace the coordinator, participants, messages, persistent logs, and two decision phases. section 6.3.1

How Think Distributed Systems covers it

How deep it goes
Explains roles, phases, persistent logs, recovery actions, and failure blocking, while stopping short of implementation code.
Examples
Uses coordinator and resource-manager message flows, persistent log records, recovery actions, and a coordinator-failure scenario.
What it assumes
Assumes distributed transactions, local transactions, resource managers, and safety-versus-liveness distinctions.
What you can do after
Evaluate atomic commit behavior and diagnose why coordinator failure can block participants.
How it connects
Connects distributed transactions, atomic commit protocols, resource managers, coordinators, safety, liveness, and consensus protocols.

Also explained in

Compare how 4 books explain two-phase commit →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Think Distributed Systems. The section links point to the parts of the book it draws on.