Problem
A single business operation updates several databases or services. If some commit and others fail, the system is left inconsistent. Local transactions cannot span independent participants.
Also known as: 2pc, xa-transactions
Commit a transaction atomically across multiple resource managers so all commit or all abort.
A single business operation updates several databases or services. If some commit and others fail, the system is left inconsistent. Local transactions cannot span independent participants.
Two-phase commit, or 2PC, is the classic atomic commit protocol. A coordinator drives all participants through prepare and commit so the outcome is the same everywhere. It is the basis of XA transactions and many distributed database internals such as cross-shard commits.
Phase 1 (prepare)
Coord -> P1, P2: PREPARE
Pi: write redo log + keep locks, reply YES or NO
Phase 2 (decision)
all YES: Coord logs COMMIT, sends COMMIT, Pi commit + release
any NO / timeout: Coord logs ABORT, sends ABORT
The coordinator must write its decision to durable storage before telling anyone. A participant that voted yes can no longer abort on its own; it is in doubt until it hears the outcome. On restart it asks the coordinator, or peers via the cooperative termination protocol, what happened.
Transaction coordinator and durable logs
High: in-doubt transaction recovery
Medium: protocol states
Coordinator crash leaves in-doubt participants holding locks
Heuristic manual commit causes divergence
Participant timeout during prepare aborts a transaction that others may commit