In this lesson · topic 2 of 7
Topic 2
Prepare, then commit
4 min read
Phase one: prepare
The coordinator sends a prepare request to every participant. Each participant does the work of the transaction short of committing. It writes its changes and a prepare record to its log, forces the log to disk, and keeps its locks. Then it replies.
A yes vote is a promise. The participant is saying that it can commit and will do so if told, even if it crashes and restarts first. A no vote is a refusal. A participant that votes no can abort at once.
Phase two: decide
If every vote was yes, the coordinator writes a commit record to its own log. If any vote was no, or one did not arrive, it writes an abort record. That write is the moment the transaction is decided. After it, the outcome cannot change.
The coordinator sends the decision. Each participant applies it, writes it to its log, releases its locks and acknowledges. When every acknowledgement is in, the coordinator can forget the transaction.
What it costs when nothing fails
Two round trips, and a forced log write at every participant and at the coordinator. A healthy transaction holds its locks for about 2 roundsmeasured: one to prepare and one to decide. The locks are what make the protocol expensive. They are held while the network is busy with someone else’s message.