The Two-Phase Commit (2PC) protocol is a simple and elegant ACP with two types of processes: A single coordinator that decides whether to reach a global commit or abort decision, and the participants that execute the transaction’s resource accesses and vote whether to commit or abort. The commit decision is made according to the global commit rule :
- If even one participant votes to abort the transaction, the coordinator has to reach a global abort decision.
- If all participants vote to commit the transaction, the coordinator has to reach a global commit decision.
A description of 2PC that does not consider failures is provided in this section. Failure scenarios and a recovery protocol is described in section 2.6.2. Figure 2 should be helpful: The circles represent states, the whole lines represent state changes, the dashed lines represent messages (labeled with the message types), and the rectangles represent logging actions.
2PC has, not surprisingly, two phases:
Phase 1: A coordinator process is started (usually at the site where the transaction is initialized), writes a begin commit record in its log, sends a prepare message to the participants, and enters the wait state. This message also contains a unique transaction id (TID), which is also in all further messages in this protocol run.
When a participant receives a prepare message, it checks if it can commit the transaction. If it can, the participant writes a ready record in its log, sends a vote_commit message to the coordinator, and enters the ready state. Otherwise, the participants decides to unilaterally abort10 the transaction—it writes an abort record in the log and sends a vote_abort message to the coordinator. It enters the abort state and can forget about the transaction.
Phase 2: After the coordinator has received votes from all participants it decides whether to commit or abort according to the global commit rule, and writes this decision in the log. If the decision is to commit, it sends a global_commit message to all sites. Otherwise, it sends a global_abort message to all sites that voted to commit. Finally, it writes an end of transaction record in its log. The participants finish the transaction according to the decision and write the result in their logs .
To be accurate, the protocol described here is centralized 2PC with reliable message channels. There are many variations on 2PC that account for different network topologies, channel reliability and synchrony. These will not be discussed further here, as the described protocol is usable in our network model (see section 2.1) and strikes a balance between few messages and high concurrency.