Skip to main content
  1. References/
  2. Architecture Design Basics/
  3. Pattern Taxonomy/
  4. Reliability, Consistency & Synchronisation/

Two-Phase Commit (2PC)

·· 256 words· 2 mins

πŸ”΄ P0 — the classical distributed transaction protocol and why it’s limited

Problem #

A transaction spans multiple databases or services. All must commit or all must abort. How do you coordinate?

Mechanism #

Phase 1 (Prepare):
  Coordinator β†’ all participants: "Can you commit?"
  Each participant: lock resources, respond YES or NO

Phase 2 (Commit/Abort):
  If ALL said YES β†’ Coordinator: "COMMIT"
  If ANY said NO  β†’ Coordinator: "ABORT"
  Participants execute and release locks

Why 2PC is Limited #

ProblemConsequence
Coordinator is SPOFIf coordinator crashes after prepare, participants are stuck holding locks
Blocking protocolParticipants hold locks until coordinator decides β€” high contention
LatencyAt least 2 round-trips; participants blocked during both
AvailabilityAny participant failure causes abort β€” availability decreases with more participants

Why Not 3PC? #

Three-phase commit adds a “pre-commit” phase to avoid blocking. In practice, rarely used: doesn’t handle partitions correctly, adds latency, and Saga solves the real problem better.

Instinct #

2PC is fine for 2-3 participants within a single datacenter. Beyond that, the availability and latency costs are usually unacceptable. For cross-service transactions, use the Saga pattern instead β€” it trades atomicity for availability via compensating transactions. See also: Saga Pattern.

  • MISCONCEPTION: negative statement: “Most NoSQL databases don’t support distributed transactions.”

    This was true in the early NoSQL era but is outdated. MongoDB, CockroachDB, YugabyteDB, and FoundationDB all now support distributed transactions (with performance tradeoffs). Systems like Cassandra and Redis Cluster still don’t support general cross-partition transactions. The modern accurate framing:

Early NoSQL systems generally avoided distributed transactions, but many modern distributed databases now support them, usually with performance tradeoffs. Support β‰  free: it means “available but often discouraged for hot paths.”

References #

DDIA 2e Reference #

  • Chapter 9: Distributed Transactions and Consensus