← back to distributed systems

Distributed Transactions

Wikipedia · wpTwo-phase commit

Two-phase commit (2PC) ensures all-or-nothing across multiple nodes. Phase 1: coordinator asks participants to prepare (vote yes/no). Phase 2: if all vote yes, coordinator commits; otherwise aborts. The weakness: if the coordinator crashes after prepare, participants are stuck waiting. Sagas offer an alternative: execute steps with compensating actions for rollback.

Coordinator Participant 1 Participant 2 Phase 1 prepare? yes yes Phase 2 commit! ack ack done

Two-phase commit

Scheme

The blocking problem

If the coordinator crashes after sending prepare but before sending commit/abort, participants that voted yes are blocked. They cannot commit (the coordinator might abort) or abort (the coordinator might commit). They must wait. This is why 2PC is called a blocking protocol. Three-phase commit (3PC) adds a pre-commit phase to reduce blocking, but is more complex and still not partition-tolerant.

Saga pattern

A saga splits a distributed transaction into a sequence of local transactions. Each step has a compensating transaction that undoes its effects. If step 3 fails, run compensations for steps 2 and 1 in reverse. No coordinator lock. No blocking. The tradeoff: intermediate states are visible (no isolation).

Scheme
Neighbors

Cross-references

  • 🌐 Ch.4 Consensus — 2PC is a degenerate case where the coordinator is the single proposer