[Distributed transaction] The difference between two-phase commit and three-phase commit

foreword

1. Distributed theory: consensus protocol: 2PC

1.1 What is 2PC

2PC, shorthand for the two-phase commit protocol, is that the entire transaction process is divided into two phases, the prepare phase and the commit phase.

1.2. Two stage process:

  1. Preparation phase: The transaction manager sends a Prepare message to each participant, each database participant executes the transaction locally, and writes the local Undo/Redo log, and the transaction is not committed at this time.
    The Undo log is to record the data before modification, and the user database is rolled back.
    The Redo log is to record the modified data, which is used to write the data file after committing the transaction

  2. Commit phase: If the transaction manager receives a participant's execution failure or timeout message, it will directly send a rollback (RollBack) message to each participant, otherwise, send a commit (Commit) message; the participant performs a commit or rollback operation according to the transaction manager's instructions
    . And release the lock resources required in transaction processing.
    Note: the lock resource must be released in the final stage

1.4. 2PC execution process

Successfully execute the transaction transaction submission process, as follows
insert image description here

  • Phase one:
  1. Transaction query: The coordinator sends transaction content to all participants, asking whether the transaction commit operation can be performed.
    and start waiting for responses from each participant

  2. Execute transactions (write local Undo/Redo logs)

  3. Responses from each participant to the coordinator to feed back transactional queries

    Summary: Each participant votes whether to let the transaction proceed

Tip: What is Ack
Ack confirmation character, in data communication, a transmission type control character sent by the receiving station to the sending station. Indicates that the data sent has been confirmed to be received without error

  • Phase two:
  1. Send a commit request:
    the coordinator sends a Commit request to all participants
  2. Transaction submission
    After the participants receive the Commit request, they will formally execute the transaction submission operation, and release the transaction resources occupied during the entire transaction execution period after the submission is completed
  3. Feedback transaction submission results
    Participants send Ack information to the coordinator after completing the transaction submission
  4. Complete the transaction
    After the coordinator receives the Ack information fed back by all participants, the transaction is completed

1.5. Advantages and disadvantages of 2PC:

1. Advantages: simple principle, simple implementation
2. Disadvantages: synchronous blocking, single point problem, different data, too conservative

  • Synchronous blocking
    This is the biggest and most obvious problem in the two-phase commit protocol. During the commit process, all logic involved in the transaction operation is blocked, that is, all participants cannot perform other operations while waiting for other participants to respond. This synchronous blocking greatly limits the performance of distributed systems
  • Single-point problem
    The coordinator is very important in the two-phase commit protocol. Once a failure occurs, the entire process will not work. More importantly, other participants will be in a state of locking the transaction and cannot continue to complete the transaction.
  • Data Inconsistency
    Suppose that after the coordinator sends a commit transaction to all participants, a local network problem occurs or the coordinator suddenly crashes before sending all the commit requests, resulting in only some participants finally receiving the Commit request, which will lead to serious data inconsistency
  • Too conservative
    If during the query phase of the two-phase commit, a participant fails or the coordinator fails to obtain the response information from all participants, the coordinator can only rely on its own timeout mechanism to determine whether to interrupt the transaction. Obviously, this strategy is too conservative. In other words, the two-phase commit
    protocol does not have a relatively complete fault-tolerant mechanism, and the failure of any node will lead to the failure of the entire transaction.

2. What is 3PC

3PC is an improved version of 2PC, which divides the "commit transaction request" process of 2PC into two, and forms a transaction processing protocol consisting of three stages: CanCommit, PreCommit and doCommit
insert image description here

2.1. Phase 1: CanCommit

  1. Transaction query
    The coordinator sends a canCommit request containing transaction content to all participants, asking whether the transaction operation can be performed, and begins to wait for the response of each participant

  2. Each participant feeds back the response of the transaction query to the coordinator.
    After receiving the canCommit request from the coordinator that includes the content of the transaction.
    Under normal circumstances, if you think you can execute the transaction smoothly, you will feed back a Yes response and enter the preparation state, otherwise you will feed back a NO response

2.2. Phase 2: PreCommit

The coordinator gets the response results of all transaction participants. According to the results, the coordinator will have two execution situations. Perform transaction precommit.
Or in the middle of the transaction, if you receive all Yes, execute the transaction.

  1. Executing transaction pre-commit is divided into 3 steps
    - sending pre-commit request:
    the coordinator sends a preCommit request to all participant nodes and enters the prepared stage

    • Transaction pre
      -commit After participants receive the preCommit request, they will perform transaction operations and record Undo and Redo information in the transaction log

      • Each participant feeds back the result of transaction execution to the coordinator:
        if the participant successfully executes the transaction operation, then feedback Ack

    If any participant responds with No response, or the coordinator cannot receive feedback from all participants after waiting for a timeout, the transaction will be interrupted

  2. The interrupt transaction is also divided into two steps

    • Send an abort request
      The coordinator sends an abort request to all participants
      • Abort the transaction
        Whether it receives an abort request from the coordinator or times out while waiting for the coordinator request, the participant will abort the transaction

2.3. Phase 3: do commit

insert image description here

At this stage, the real transaction commits or the transaction rollback is completed, so there will be two situations

  1. Execute transaction commit

    • Send a commit request
      If the coordinator is in a normal working state and it has received Ack responses from all participants, it will transition from pre-commit state to commit state and send a doCommit request to all participants

    • Transaction submission
      After receiving the doCommit request, the participant will formally execute the transaction submission operation, and release the transaction resources occupied during the entire transaction execution process after the submission is completed

    • Feedback transaction submission results
      Participants send an Ack response to the coordinator after completing the transaction submission

    • Complete the transaction: After the coordinator receives the Ack message from all participants, the transaction is completed

  2. interrupt transaction

    • Send an abort request: the coordinator sends an abort request to all participant nodes
    • Transaction rollback: After receiving the abort request, the participant will perform transaction rollback according to the recorded Undo information. And release the resources occupied during the entire transaction execution after the rollback is completed
    • Feedback transaction rollback result: After the participant completes the transaction rollback, it sends an Ack message to the coordinator
    • Interrupt transaction: The coordinator interrupts the transaction after receiving the Ack message from all participants

NOTE: Once in phase three, there are 2 possible failures

  1. There is a problem with the coordinator
  2. Network failure between coordinator and participants

If either situation occurs, the participant will eventually fail to receive the doCommit request or abort request. In this case, the participant will continue to commit the transaction after waiting for the timeout

2.4. Advantages and disadvantages of 3PC:

advantage:

  1. First of all, a timeout mechanism is set for both the coordinator and the participants (in 2PC, only the coordinator has a timeout mechanism, that is, if the message from the participant is not received within a certain period of time, it will fail by default), mainly to avoid the problem that the participant cannot release resources when the participant cannot communicate with the coordinator node for a long time (the coordinator hangs up). And this mechanism also reduces the blocking time and scope of the entire transaction.

  2. Through the design of CanCommit, PreCommit, and DoCommit three stages,
    compared with 2PC, an additional buffer stage is set to ensure that the state of each participating node is consistent before the final commit stage

  3. PreCommit is a buffer that ensures that the state of each participating node is consistent before the final commit phase

insert image description here

Guess you like

Origin blog.csdn.net/u011397981/article/details/131900440