当前位置:网站首页>Read distributed consistency protocols 2pc and 3pc
Read distributed consistency protocols 2pc and 3pc
2022-06-21 13:57:00 【The forest wind is at ease】
Distributed environment , There are multiple nodes , It is necessary to ensure data consistency between different nodes , Distributed transaction is proposed , and 2PC and 3PC Is the representation of distributed transactions .
One 、 What is? 2PC
2PC Two phase submission , As the name suggests, the transaction submission process is divided into two stages ,P It's the preparation stage (Prepare),C It's the submission phase (Commit).
Preparation stage , The transaction manager sends... To each participant Prepare news , Each participant performs transactions locally , And write the local Undo/Redo journal , The transaction is not committed at this time .
Submission phase , If the transaction manager receives an execution failure or timeout message from the participant , Send... Directly to each participant Rollback news , otherwise , send out Commit news .
The participant performs commit or rollback operations according to the instructions of the transaction manager , And release the lock resources used in transaction processing . Relational database Mysql、Oracle The processing flow is the same , In fact, they also support the two-phase submission agreement , Let's take a closer look 2PC The implementation process of .
Two 、2PC Execute the process
2.1 Execute the process

The first stage :
① Business inquiry , The transaction manager sends transaction content to all participants , Ask if the transaction is executed , And start waiting
② Participant received request , Start executing the transaction ( Write local Undo/Redo journal )
③ Each participant feeds back the implementation of the transaction to the coordinator
The second stage :
① Send submit request , The transaction manager issues to all participants Commit/Rollback request
② Transaction submission , Participants receive Commit/Rollback After the request , If it is Commit The transaction commit operation will be formally executed , If it is Rollback According to the previous record Undo Log rollback , And release the transaction resources occupied during the whole transaction execution after the commit
③ Feedback transaction submission results , After the participant completes the transaction commit , Send... To the coordinator ack Information , The coordinator receives feedback from all participants ack After the message , Complete the business
The first stage is mainly to let each participant vote whether to let the transaction proceed , The second stage mainly involves two things , Perform transactions , Another is to vote on the execution results of the transaction .
2.2 characteristic
2PC The advantages are obvious : The principle of simple , To achieve convenient , But there are also some problems :
Synchronous blocking :
The most obvious and biggest problem of the two-phase commit protocol is synchronization blocking , Throughout the submission process , The logic of all participants' operations is blocked , Each participant waits for a response from the manager , Unable to do other operations , It greatly limits the distributed performance .
A single point of the problem :
The coordinator is important throughout the submission process , If something goes wrong with the coordinator , Then the whole process fails , What is more serious is that the participants will always be in the state of resource locking , Thus, the transaction operation cannot be completed .
Data inconsistency :
Suppose that when the coordinator sends commit after , When network jitter occurs, the coordinator fails to send all Commit I hung up before , As a result, only some participants received commit request , Or is it Prepare Stage success ,Commit Phase failure , This will lead to data inconsistency .
Too conservative :
If in the process of phase II submission , The coordinator is unable to obtain the response information of all participants due to the failure of participants , At this time, the coordinator can only rely on its own timeout mechanism to rollback the transaction . In other words, the two-phase commit does not have a well-designed fault-tolerant mechanism .
3、 ... and 、 What is? 3PC
3PC yes 2PC Improved version , Divide the first phase of a transaction into two , One more stage . Make up the CanCommit、PreCommit and doCommit Three stages .
The first stage :
① Business inquiry , The coordinator sends a request containing the contents of the transaction to all participants , Ask if the transaction commit operation can be performed , And began to wait for the response of each participant .
② After the participant receives a request from the coordinator containing the transaction content , Under normal circumstances , If you think you can do things smoothly , Then feedback Yes Respond to , And get ready , Otherwise feedback No Respond to .
The second stage :
After the coordinator receives the response from all participants , There will be... According to the results 2 There are three situations in which operations are performed : Perform transaction pre commit , Or interrupt the transaction . If all the participants in the feedback are Yes, Then the transaction Precommissioning will be performed . If any participant gives feedback No Respond to , Or after waiting for a timeout , The coordinator has not yet been able to receive feedback from all participants , Then interrupt the transaction .
If it is a transaction request , Three steps are required :
① Send pre submit request : The coordinator sends... To all participant nodes preCommit request , And enter prepared Stage .
② Transaction pre commit : Participant received preCommit After the request , Will perform transaction operations , And will Undo and Redo Information is recorded in the transaction log .
③ Each participant feeds back the result of transaction execution to the coordinator : If the participant successfully executes the transaction operation , So feedback Ack
If it is a rollback request , It takes two steps :
① Send interrupt request : The coordinator sent... To all participants abort request .
② Interrupt the business : Whether it's received from the coordinator abort Timeout while requesting or waiting for the coordinator to request , Participants interrupt the transaction
The third stage :
In this phase, the real transaction is committed or the transaction rollback is completed , Participants will receive the request from the coordinator , Then determine whether transaction commit or rollback is required . In case of network jitter or other problems , As a result, participants cannot receive the request from the coordinator , In this case , Participants will wait for a timeout , By default, it is considered that other participants are successful when receiving , Therefore, the transaction will be committed .
Four 、2PC And 3PC Comparison
Come here , You may not quite understand 2PC and 3PC The difference between , Can't understand 3PC Why add one more stage .
First 3PC Timeout mechanism is set for both coordinator and participant , It avoids the situation that participants cannot communicate with the coordinator node for a long time , The problem of being unable to free resources .( stay 2PC in , Only the coordinator has a timeout mechanism , That is, if the participant's message is not received within a certain time, it will fail by default )3PC The participant has its own timeout mechanism, which will be triggered after the timeout , Automatic local commit To release resources . This mechanism also reduces the blocking time and range of the whole transaction .
adopt CanCommit、PreCommit、DoCommit Three stages of design , Compare with 2PC for , An extra buffer stage is set to ensure that the states of participating nodes are consistent before the final commit stage . Just like we usually do before we submit the code Review once , To ensure normal .
however 3PC The problem of data inconsistency is not completely solved .
in fact , I don't think 3PC It must be better than 2PC good ,3PC The solution is to solve the problem of synchronous blocking and single point problem, which causes the participants to be blocked all the time , Data consistency has not been solved . If the system has no high requirements for data consistency and real-time , For ordinary thousands of concurrent systems , I feel 2PC That's enough , After all, it is easier to implement , And the probability of participants being blocked is actually relatively small . So we should analyze it according to the actual situation .
边栏推荐
- Explanation of vim, makefile and GDB tools
- Automatic operation and maintenance 2 - common modules in ansible
- CSDN's test teacher teaches JMeter to generate stress test reports
- 618 Nuggets digital collection? Burberry and other luxury brands fight against metauniverse
- 1. memory partition model
- Configuration of oracle19c under alicloud lightweight application server linux-centos7
- [in depth understanding of tcapulusdb technology] tcapulusdb business data backup
- Alibaba cloud link tracking is on the Net project (Jaeger trace)
- Are you still using generator to generate crud code of XXX management system? Let's see what I wrote
- Prepare for the golden three silver four, are you ready? Summary of software test interview questions
猜你喜欢

Automatic operation and maintenance 4 - variables and encryption in ansible

618 Nuggets digital collection? Burberry and other luxury brands fight against metauniverse

MySQL - table operation
![[untitled]](/img/3c/c34a8cbbe398cecd54050b30f95b66.png)
[untitled]

Async get and post request interface data (add, delete, modify and query pages)

Lamp Architecture 3 -- compilation and use of PHP source code

Hands on data analysis unit 2 section 4 data visualization

Summary of the latest remote deployment O & M tools

MySQL - view properties

Explanation of vim, makefile and GDB tools
随机推荐
Pyqt environment in pycharm
Chapter IX Cisco ASA application nat
3000 frame animation illustrating why MySQL needs binlog, redo log and undo log
How to read AI summit papers?
Lamp Architecture 3 -- compilation and use of PHP source code
Must the database primary key be self incremented? What scenarios do not suggest self augmentation?
MySQL - user management
Setting of Seaborn drawing style
C#&. Net to implement a distributed event bus from 0 (1)
1. memory partition model
map. Values() to copy list and ArrayList
IMU selection, calibration error analysis, AHRS integrated navigation
Master the basic usage of SQLite3
Detailed explanation of dictionary source code in C #
Add SSL security certificate to web site
[in depth understanding of tcapulusdb technology] tcapulusdb construction data
What is software testing?
Navigation bar switching, message board, text box losing focus
Heat mapping using Seaborn
Comprehensively analyze the key points of knowledge required for interface testing and interface testing