Part 3 of 5 · 1 min read
Two-phase commit, phase by phase
A prepare phase checks every node can commit; a commit phase tells every node to actually do so, only after every node has agreed it can.
Think of a wedding planner calling every vendor the week before the event to confirm each one is still able to deliver. Only after every single vendor confirms does the planner tell all of them to proceed on the day itself.
In plain words: two-phase commit coordinates a distributed transaction through a designated coordinator node and two distinct phases. In the prepare phase, the coordinator asks every participating node whether it is able to commit its part of the transaction, and each node responds yes only if it has durably recorded enough information to guarantee it can commit later even if it crashes in between. In the commit phase, which only begins once every single node has responded yes, the coordinator tells every node to actually commit; if even one node responded no, or failed to respond, the coordinator instead tells every node to abort.
A worked example. A coordinator asks node A and node B to prepare a funds transfer. Both respond yes, having durably recorded their intended changes. The coordinator then tells both to commit, and both apply their changes for real, since both responses were yes.
The key lines. No new SQL; the prepare and commit messages are coordinator-to-node protocol messages, internal to the distributed transaction manager rather than SQL statements themselves.
Two-phase commit
Two-phase commit is a protocol coordinating a distributed transaction through a prepare phase, where every node confirms it can commit, followed by a commit phase, where the coordinator tells every node to actually commit, only once every node has confirmed.
Example: a wedding planner confirming every vendor can deliver before telling all of them to proceed is two-phase commit's prepare-then-commit structure.