In my article on Distributed consistency issues, I mentioned the solution of distributed consistency, and today we will talk about 2PC (two-phase commit protocol) and 3PC (three-phase commit protocol).

Consistency protocol 2PC

What is 2PC?

2PC (two-phase Commit) divides the entire transaction process into Two phases: Prepare Phase and Commit Phase.

Some relational databases support 2PC protocols, such as Oracle and MySql.

2PC Execution process

  • Phase one
  1. The transaction query coordinator sends the transaction content to each participant, asks if the transaction can be committed, and waits for each participant to respond.
  2. Each participant executes a transaction and writes Undo/Redo logs when the transaction has been executed but not committed. Undo log records the data before modification and is used for database transaction rollback. Redo logs record modified data and are used to write data after a transaction has been committed.
  3. Each participant reports back to the coordinator the response to the transaction query.
  • Phase two

    Stage 2 According to the query response in stage 1, there are the following two situations:

    • Successfully committed transaction

      If all participants respond Yes to the transaction query in phase 1;

      1. The coordinator sends to each participantcommitRequest (transaction commit).
      2. Each participant receivedcommitAfter the request, the transaction is actually committed and the lock resources used during the transaction execution are released upon completion.
      3. When the participant completes the transaction, it sends it to the coordinatorAckInformation. (Ack is an acknowledgement character. In data communication, it is a transmission control character sent from the receiving station to the sending station, indicating that the received data has been confirmed.)
      4. The coordinator receives all participantsAckAfter the information, the transaction is completed.

    • Failed interrupt transaction

      If any participant gives a No response to the coordinator, or if the coordinator is unable to get feedback from all participants after the wait times out, the transaction is interrupted.

      1. The coordinator sends to all participantsRollBackRequest (rollback request).
      2. The participant receivesRollbackAfter the request is made, it will be used as recorded in phase oneUndoInformation to perform the transaction rollback and, upon completion of the rollback, release the lock resources held for the entire duration of the transaction.
      3. The participant sends to the coordinator after completing the transaction rollbackAckInformation.
      4. Interrupt transaction: The coordinator receives feedback from all participantsAckInformation after completion of the transaction is interrupted.

Advantages and disadvantages of 2PC

advantages

The principle is simple and the implementation is convenient.

disadvantages

  • A synchronized block

    This is the most obvious of the existence of the 2 PC protocol is also one of the biggest problems, in the process of the implementation of two-phase commit, all involved in the transaction commit logic in the blocking state, all the participants in the process of waiting for the response of other participants, unable to perform other operations, the synchronized block greatly limits the performance of the distributed system.

  • Single point of failure

    In the execution of a two-phase commit, the coordinator is very important. Once the coordinator fails, the whole process will not proceed, and most importantly, other participants will remain locked in the resource and unable to complete the transaction.

  • Data inconsistency

    Serious data inconsistencies can occur when a local network exception occurs after the coordinator sends all the commit requests, or when the coordinator itself crashes before sending all the commit requests, causing only some participants to receive the commit requests.

  • Too conservative

    In the query stage of two-stage submission, if the coordinator fails to obtain all the responses of the participants due to the failure of the participants, the coordinator can only rely on its own timeout mechanism to determine whether to interrupt the transaction, which is too conservative.

    Because the two-phase commit protocol does not have a well-designed fault tolerance mechanism, the failure of any node will lead to the failure of the whole transaction.

Consistency protocol 3PC

What is three-phase commit?

3PC (Three Phase Commit), is an improved version of 2PC, is made bycanCommit,preCommit,doCommitA transaction protocol consisting of three phases.

CanCommit phase

  1. Transaction to ask

    The coordinator sends a message containing the transaction content to all participantscanCommitRequest, asking if the transaction commit action can be performed and waiting for the participant to respond.
  2. Each participant reports back to the coordinator the response to the transaction query

    Participants receivecanCommitAfter the request, report back to the coordinator if you think you can perform the transaction commit smoothlyYesResponse, otherwise returnNoThe response.

PreCommit phase

Depending on the result of the transaction query, the coordinator may choose to either perform the transaction pre-commit or interrupt the transaction.

  • If all the participants respondYesIn response, the transaction precommit is performed:
  1. Send a pre-commit request

    The coordinator sends to all participantspreCommitRequest and enter the Prepared phase.
  2. Transaction precommit

    Participants receivepreCommitAfter the request, the transaction is performed and the Undo/Redo log is written.
  3. Participants report pre-submission results to the coordinator

    If the participant successfully executes the transaction, feedback is givenAck.
  • If any of the participants report backNoResponse, or wait for a timeout and still cannot obtain responses from all participants, then the interrupt transaction is performed:
  1. Send interrupt transaction request

    The coordinator sends to each participantabortThe request.
  2. Interrupt the transaction

    Participants receiveabortThe participant interrupts the transaction either by requesting or by waiting for a timeout on the coordinator’s request.

DoCommit phase

There are also two situations that occur at this stage: transaction commit and transaction rollback.

  • If the coordinator receives all participantsAckIn response, the transaction commit is performed
  1. Send a transaction commit request

    The coordinator sends to all participantsdoCommitThe request.
  2. Commit the transaction

    Participants receivedoCommitAfter the request, the transaction commit operation is formally executed and the lock resources occupied during the transaction execution are released.
  3. Feedback transaction commit results

    After completing the transaction commit operation, the participant gives feedback to the coordinatorAckInformation.
  4. To complete the transaction

    The coordinator receives messages from all participantsAckResponse completes the transaction.
  • Otherwise interrupt transaction
  1. Send interrupt request

    The coordinator sends to all participantsabortThe request.
  2. Transaction rollback

    Participants receiveabortAfter the request, the transaction is rolled back based on the Redo log and the lock resources occupied during the entire transaction are released when the rollback is complete.
  3. Feedback transaction rollback results

    Participants give feedback to the coordinator after the transaction is completedAckInformation.
  4. Interrupt the transaction

    The coordinator receives responses from all participantsAckMessage, interrupt transaction.
3PC into the third stage may appear problems
  1. The coordinator itself fails;
  2. The network between the coordinator and the participant is faulty.

The failure of either of the above causes participants to fail to receive doCommit or ABORT requests sent by the coordinator, which can lead to serious data inconsistencies.

The difference between 2PC and 3Pc (optimization of 3Pc to 2PC)

  • To optimize the

    • The timeout mechanism is set by both the coordinator and the participant (only the coordinator in 2PC sets the timeout mechanism), which avoids the situation that the participant cannot release the lock resource in the case that the participant cannot communicate with the coordinator for a long time (coordinator failure), and reduces the time and scope of the whole transaction blocking.
    • A buffer phase is added to ensure that the state of each node is consistent before the final submission phase.
  • The problem

    • 3PC does not completely solve the data inconsistency problem.

      Since the above mentioned problems may occur in phase 3, once the participant does not normally receive the ABORT request sent by the coordinator, the participant will commit the transaction and release the resources after its own wait times out, resulting in data inconsistency problems.