A coordinator asks everyone to prepare, then tells everyone to commit or abort.
Two-Phase Commit (2PC) is a protocol that makes several databases all commit or all abort. A coordinator runs it in two steps: first it asks everyone to prepare, then it tells everyone the final decision.
We want several databases to agree on one outcome. The trick of 2PC is to split the commit into two steps, so that the point of no return comes after everyone has promised that they can finish.
Phase 1: Prepare. The coordinator sends PREPARE to every participant. Each participant does the work but does not commit. It writes everything needed to finish into its disk log (a prepare record), keeps its locks, and replies YES. If it cannot do the work, it replies NO and rolls back at once.
Phase 2: Commit or abort. The coordinator counts the votes. All YES means COMMIT, anything else means ABORT. It writes the decision to its own log first. This write is the commit point. Then it sends the decision to every participant, who obey, release their locks and reply with an ACK.
| # | Who | Does what |
|---|---|---|
| 1 | Coordinator | Starts the transaction and sends PREPARE to all participants. |
| 2 | Each participant | Does the work, writes a prepare record to its log, keeps its locks, votes YES (or rolls back and votes NO). |
| 3 | Coordinator | Collects votes. All YES: logs COMMIT. Any NO or missing vote: logs ABORT. This log write is the commit point. |
| 4 | Coordinator | Sends the decision to every participant. |
| 5 | Each participant | Commits or rolls back, releases its locks, and replies ACK. |
| 6 | Coordinator | After all ACKs, writes an end record and forgets the transaction. |
| Failure | Result | Why |
|---|---|---|
| A participant crashes before voting | ABORT | The coordinator times out, treats the missing vote as NO. No one had committed. |
| A participant votes NO | ABORT | One NO is enough. Everyone else rolls back. |
| A participant crashes after voting YES | COMMIT or ABORT, as decided | On restart its log says 'prepared', so it asks the coordinator for the decision. |
| The coordinator crashes before deciding | Participants BLOCK until it returns | They voted YES, so they cannot abort alone, and they do not know the outcome. |
| The coordinator crashes after deciding | Participants BLOCK until it returns | The decision is in its log. Participants who did not hear it must wait for the log. |
A simplified picture of the textbook protocol. Real systems keep logs on disk, retry messages and let a recovering participant ask the others. The coordinator-down slider only matters for the last scenario.
| Term | Meaning in easy words |
|---|---|
| Coordinator | The process that runs the protocol and makes the final decision (also called the transaction manager) |
| Participant | A database or resource that takes part (also called a resource manager or cohort) |
| Prepare / vote phase | Phase 1: the coordinator asks, each participant votes YES or NO |
| Commit / decision phase | Phase 2: the coordinator announces COMMIT or ABORT |
| Commit point | The moment the coordinator durably logs its decision. After this the outcome cannot change |
| Prepared state | A participant has promised to commit, and now waits for the decision |
| In-doubt transaction | A prepared transaction that has not yet heard the decision |
| Blocking protocol | A protocol where a crash can make others wait with locks held |
| Write-ahead log (WAL) | A log saved to disk before acting, so a restart can recover |
| XA | A standard interface for a transaction manager to run 2PC across resources |
| Heuristic decision | A manual choice by an operator to commit or abort a stuck transaction, at the risk of inconsistency |
| Choice | We gain | We lose |
|---|---|---|
| 2PC | All-or-nothing correctness | Latency, locks, availability |
| SAGA instead | Speed, no global locks, independent services | Temporary inconsistency, and compensations to write |
| Replicated coordinator | No single point of failure | More complexity and more messages |
| Short vote timeout | Locks released sooner | More transactions abort when one node is slow |
| Fewer participants | Faster and more reliable | Services must share databases or be merged |