Distributed technology stack 1.2-basic theory of 2PC and 3PC distributed transactions

introduction

Distributed consistency can be divided into strong consistency, weak consistency and final consistency. Among them, the two-phase commit model (2PC) and the three-phase commit model (2PC) are two common models to achieve strong consistency.

1 Two-phase submission model

1.1 Principle introduction

The two-phase commit model is divided into participants and coordinators . Participants can be understood as data nodes in distributed storage, for example; the coordinator is the brain that controls distributed transactions. The whole process is completed in two phases, namely the preparation phase and the submission phase .

1.1.1 Preparation stage

The preparation phase is as follows:

  • The coordinator initiates a pre-operation request to all participants and waits for the response of each participant;
  • Participants perform operations after receiving the pre-operation request from the coordinator, and write Undo information, Redo information, and Lock records into the log. The whole process is carried out in a local transaction;
  • If the participant performs the operation successfully, it responds successfully to the coordinator; otherwise, it returns a failure response.

1.1.2 Submission phase

If in the preparation phase, the coordinator waits for a response from a participant to time out or at least one participant returns a failure, then the cancellation operation will be executed ; otherwise, if all participants return success, the formal submission will be executed .

Undo operation :

  • The coordinator initiates a cancellation request to all participants;
  • After receiving the cancellation request, the participant uses the Undo information to roll back the operation, and deletes the Undo information, Redo information, and Lock records. The whole process is carried out in a local transaction;
  • Participants will return to the coordinator to cancel after completing the execution;
  • When the cancellation of all participants is received, the coordinator cancels the transaction.

Formally submitted :

  • The coordinator initiates a submission request to all participants;
  • After the participant receives the submission request, it deletes Undo information, Redo information and Lock records to release transaction resources. The whole process is carried out in a local transaction;
  • Participants will return to the coordinator after completing the execution;
  • After receiving all the participants' submissions, the coordinator completes the transaction.

1.2 Summary of advantages and disadvantages

1.2.1 Advantages

  • Compared with the three-phase submission model, the principle is simple and easy to implement.

1.2.1 Disadvantages

  • Synchronous blocking : All participants will lock the resource until the end, and the concurrency is not high. For example, after the coordinator goes down, the participant resources cannot be released.
  • Data inconsistency : When the coordinator makes a formal submission request to some participants, a network abnormality occurs, which will cause some participants to submit and the other part not to submit, resulting in inconsistencies. But it can be solved through manual operation and maintenance monitoring and repair.
  • Loss of state : When the coordinator sends a formal submission request, the only participant and the coordinator that have been received are down at the same time. Then other participants cannot know the current status of the transaction even if they renegotiate a coordinator.

2 Three-phase submission model

2.1 Principle introduction

The three-phase submission model is also divided into participants and coordinators . Join the inquiry stage before the two stages, namely the inquiry stage , the preparation stage and the submission stage .

2.1.1 Inquiry phase

The inquiry phase is also called the CanCommit phase. The process is as follows:

  • The coordinator sends a CanCommit request to all participants to see if the transaction commit operation can be performed. Then wait for responses from all participants.
  • After each participant receives the CanCommit request, if the transaction can be successfully executed, it will return a Yes response and enter the ready state; otherwise, it will be No. Then wait for further requests from the coordinator.

2.1.2 Preparation stage

The preparation phase is also called the PreCommit phase. If the negotiator in the inquiry phase receives a Yes response from all participants, it will perform the pre-commit ; otherwise, it will perform the interrupt operation .

Interrupt operation :

  • The coordinator sends interrupt requests to all participants;
  • After the participant receives the interrupt request, the transaction is interrupted. In particular, in the interrogation phase, after the participant returns a response, waiting for the coordinator's next request to time out, the transaction will also be interrupted.

Pre-submission :

  • The coordinator sends a PreCommit request to all participants, and then waits for responses from all participants.
  • Participants perform operations after receiving the PreCommit request from the coordinator, and write Undo information, Redo information, and Lock records to the log. The whole process is carried out in a local transaction;
  • If the participant performs the operation successfully, it responds successfully to the coordinator; otherwise, it returns a failure response. Then wait for the next request.

2.1.3 Submission phase

The preparation phase is also called the doCommit phase. If the pre-commit is performed in the preparation phase, and the coordinator waits for a response from a participant to time out or at least one participant returns a failure, then the cancel operation will be performed ; otherwise, all participants return success, then the formal submission will be performed .

Undo operation :

  • The coordinator initiates a cancellation request to all participants;
  • After receiving the cancellation request, the participant uses the Undo information to roll back the operation, and deletes the Undo information, Redo information, and Lock records. The whole process is carried out in a local transaction;
  • Participants will return to the coordinator to cancel after completing the execution;
  • When the cancellation of all participants is received, the coordinator cancels the transaction.

Formally submitted :

  • The coordinator initiates a submission request to all participants;
  • After the participant receives the submission request, it deletes Undo information, Redo information and Lock records to release transaction resources. The whole process is carried out in a local transaction. In particular, after the participant has pre-submitted in the preparation phase, this step and subsequent operations will be executed after waiting for the coordinator's next request to time out ;
  • Participants complete the execution and return to the coordinator to submit;
  • After receiving all the participants' submissions, the coordinator completes the transaction.

2.2 Summary of advantages and disadvantages

2.2.1 Advantages

  • Since participants have also added a timeout mechanism, there is no such thing as a two-stage situation where resources are always locked.

2.2.1 Disadvantages

  • When there is a network reason or the coordinator goes down, the participant nodes that successfully perform the pre-submission in the preparation phase will automatically submit the formal submission after the timeout. At the same time, if other nodes fail to pre-submit successfully, data inconsistency will occur .

Guess you like

Origin blog.csdn.net/fs3296/article/details/105694089