The Two-Phase Commit Protocol

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 [11]:

  • 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.

Figure 2: The Two-Phase Commit protocol

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 [11].

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.

최 재훈

블로그, 페이스북, 트위터 고성능 서버 엔진, 데이터베이스, 지속적인 통합 등 다양한 주제에 관심이 많다.
Close Menu