The Coordinator Role in 2PC
What you'll learn: How one special node acts as the "conductor" orchestrating a distributed transaction across all participants.
In Two-Phase Commit (2PC), the coordinator is a single node responsible for driving the entire transaction to completion or rollback. Think of it like a wedding officiant who asks "does anyone object?" and only proceeds when everyone agrees—or cancels the ceremony if anyone says no.
How the Coordinator Works
The coordinator maintains the authoritative decision about whether a transaction commits or aborts. Here's its job:
-
Initiate the transaction: The coordinator receives the transaction request from a client or application.
-
Prepare phase: It sends a
PREPAREmessage to all participant nodes, asking "Can you commit this transaction?" Each participant checks if it can succeed (has resources, no conflicts, etc.) and respondsYESorNO. -
Decision point: The coordinator collects all responses. If every participant votes
YES, the coordinator decides toCOMMIT. If any participant votesNO(or times out), it decides toABORT. -
Commit phase: The coordinator sends the final decision (
COMMITorABORT) to all participants, who then execute it. -
Persistence: The coordinator writes its decision to durable storage before announcing it, ensuring recovery is possible if it crashes.
Why a Single Coordinator?
Having one coordinator creates a single source of truth for the transaction outcome. This prevents split-brain scenarios where some nodes commit while others abort. However, it's also a single point of failure—if the coordinator crashes after deciding but before informing participants, the transaction blocks until recovery.
Key Takeaway: The coordinator in 2PC is the single authority that collects votes from all participants and makes the binding commit/abort decision, ensuring atomicity across distributed nodes.