当前位置:网站首页>Distributed transactions
Distributed transactions
2022-07-03 06:59:00 【W_ Meng_ H】
1、 What is business
Business (Transaction) Is a program execution unit that accesses and possibly updates various data items in the database (unit). In a relational database , A transaction consists of a group of SQL Sentence composition . The transaction should have 4 Attributes : Atomicity 、 Uniformity 、 Isolation, 、 persistence . These four attributes are often called ACID characteristic .
Atomicity (atomicity): A transaction is an indivisible unit of work , All operations included in a transaction are either done , Either not .
Uniformity (consistency): The transaction must be to change the database from one consistency state to another , The intermediate state of a transaction cannot be observed .
Isolation, (isolation): The execution of one transaction cannot be interfered by other transactions . That is, the operations and data used within a transaction are isolated from other concurrent transactions , Transactions that execute concurrently cannot interfere with each other . There are four levels of isolation : Read uncommitted (read uncommitted)、 Read submitted (read committed, To solve the dirty read )、 Repeatable (repeatable read, Solve the problem of false reading )、 Serialization (serializable, Solve unreal reading ).
persistence (durability): Permanence is also called permanence (permanence), Once a transaction is committed , Its changes to the data in the database should be permanent . Subsequent operations or failures should not have any impact on it .
When any transaction mechanism is implemented , It's all about business ACID characteristic , Include : Local transactions 、 Distributed transactions , Even if it can't be well satisfied , Also consider the level of support .
Two 、 Local transactions
In most scenarios , Our applications only need to operate a single database , The transaction in this case is called a local transaction (Local Transaction). Local affairs ACID The feature is that the database provides direct support .
quite a lot java Applications are integrated spring, And use its declarative transaction management function to complete the transaction function . The general steps are as follows :
1、 Configure transaction manager .spring Provides a PlatformTransactionManager Interface , Its have 2 An important implementation class :
DataSourceTransactionManager: Used to support local transactions , in fact , Its interior is also through operation java.sql.Connection To open 、 Commit and rollback transactions .
JtaTransactionManager: Used to support distributed transactions , Its implementation JTA standard , Use XA The agreement is submitted in two phases . It should be noted that , It's just a proxy , We need to provide it with a JTA provider, It's usually Java EE The transaction coordinator provided by the container (Java EE server's transaction coordinator), You can also be container independent , Configure a local JTA provider.
2、 At the beginning of the transaction that needs to be opened bean Add @Transitional annotation
spring In addition to supporting local transactions , Distributed transactions are also supported .
3、 ... and 、 Distributed transaction scenario
1、 Cross-database transaction
Cross database transactions refer to , A certain function of an application needs to operate multiple libraries , Different business data are stored in different libraries .
2、 Sub database and sub table
Generally, a database has a large amount of data, or it is expected that there will be a large amount of data in the future , Will be split horizontally , That is, sub database and sub table .
3、 As a service (SOA)
After the micro service is split , Independent services through RPC Framework to make remote calls , To communicate with each other .. We need to ensure that these cross service operations on multiple databases are successful , Or they all fail .
Four 、 Solution
XA standard : Distributed transaction specification , A distributed transaction model is defined .
Four characters :
Applications (Application Program , abbreviation AP): Used to define transaction boundaries ( Define the beginning and end of a transaction ), And operate on resources within the transaction boundaries .
Explorer (Resource Manager, abbreviation RM): Such as a database 、 File system, etc , And provide access to resources .
Transaction manager (Transaction Manager , abbreviation TM): Responsible for assigning transaction unique identifier , Monitor the progress of transactions , And responsible for the submission of affairs 、 Roll back, etc .
Communication resource manager (Communication Resource Manager, abbreviation CRM): Control one TM Domain (TM domain) Inside or across TM Communication between distributed applications in the domain .
Global transaction : A global transaction contains multiple independent transaction branches , Either this set of transaction branches is successful , Or they all fail . For each transaction branch ACID Together, features make up the ACID characteristic . That is to say, the support of a single transaction branch ACID Features go up a level (up a level) To the realm of distributed transactions .
Strong consistency
1、 Two phase submission agreement (2PC), stay DTP Reference model (<<Distributed Transaction Processing: Reference Model>>) in , Specifies that the commit of a global transaction uses two-phase commit agreement ; and XA standard (<< Distributed Transaction Processing: The XA Specification>>) It's just a definition Interfaces to be used in the two-phase commit protocol , That is to say RM-TM Interface of interaction , Because the participants in the two-phase submission process , Only TM and RMs.

The problem is :
- Synchronization blocking problem . In the two-phase commit scheme, the global transaction ACID characteristic , It depends on RM Of .
- A single point of failure . Because of the importance of the coordinator , Once the coordinator TM failure . participants RM It will keep blocking . Especially in the second stage , Coordinator failed , Then all participants are still in the state of locking transaction resources , Cannot continue to complete the transaction .
- Data inconsistency . In phase II of phase II submission , When the coordinator sends commit After the request , There is a local network exception or sending commit The coordinator failed during the request , This time, only a few participants received commit request . And in this part of the participants received commit The request is then executed commit operation . But the rest didn't receive commit The requested machine is unable to perform a transaction commit . So the whole distributed system appears the phenomenon of data inconsistency .
2、 Three stage submission agreement
Different from the two-stage submission is , There are two changes in the three-phase submission .
- Introduce timeout mechanism . At the same time, the timeout mechanism is introduced in both the coordinator and the participants .
- Insert a preparation stage in the first and second stages . It ensures that the states of participating nodes are consistent before the final submission stage . in other words , In addition to introducing a timeout mechanism ,3PC hold 2PC Once again, the preparation phase of the project is divided into two parts , In this way, there are three stages of submission CanCommit、PreCommit、DoCommit Three stages .

stay doCommit Stage , If the participant cannot receive the... From the coordinator in time doCommit perhaps rebort When asked , After the timeout , Transaction commit will continue .( In fact, this should be based on probability , When entering the third stage , Indicate that participants have received... In the second phase PreCommit request , Then the coordinator produces PreCommit The premise of the request is that he is , Received... From all participants CanCommit The responses are all Yes.( Once the participants have received PreCommit, It means that he knows that everyone actually agrees to modify ) therefore , In a word, it is , When entering the third stage , Due to network timeout and other reasons , Although participants did not receive commit perhaps abort Respond to , But he has reason to believe : The chances of a successful submission are great . )
Summary :2PC And 3PC The difference between
be relative to 2PC,3PC The main single point of failure to solve , And reduce congestion , Because once the participants can't receive the information from the coordinator in time , He will default to commit. Instead of holding transaction resources and blocking them all the time . But this mechanism also leads to data consistency problems , because , Because of the Internet , Sent by the coordinator abort The response was not received by the participants in time , Then the participant executes after the timeout commit operation . In this way, we will receive abort There is data inconsistency between the participants who command and perform the rollback .
I understand 2PC and 3PC after , We can find out , No matter two-phase commit or three-phase commit, it can't completely solve the problem of distributed consistency .
To be continued ...
Related reference :
Thank you up The share of !
边栏推荐
- Laravel Web Framework
- Tool class static method calls @autowired injected service
- VMware virtual machine C disk expansion
- Asynchronous programming: async/await in asp Net
- The difference between CONDA and pip
- Shim and Polyfill in [concept collection]
- SQL implementation merges multiple rows of records into one row
- 多个全局异常处理类,怎么规定执行顺序
- Sorting out the core ideas of the pyramid principle
- 这两种驱蚊成份对宝宝有害,有宝宝的家庭,选购驱蚊产品要注意
猜你喜欢

【类和对象】深入浅出类和对象
![[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones](/img/d0/850e095a43610366d6144b2471f3f7.jpg)
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones

这两种驱蚊成份对宝宝有害,有宝宝的家庭,选购驱蚊产品要注意

Liang Ning: 30 lectures on brain map notes for growth thinking

My 2020 summary "don't love the past, indulge in moving forward"

Software testing learning - day one

Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record

Software testing assignment - the next day

Winter vacation work of software engineering practice

2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
随机推荐
Understand software testing
Journal quotidien des questions (11)
VMware virtual machine C disk expansion
What are the characteristics and functions of the scientific thinking mode of mechanical view and system view
DBNet:具有可微分二值化的实时场景文本检测
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
Yolov3 learning notes
10000小时定律不会让你成为编程大师,但至少是个好的起点
Yolov2 learning and summary
Selenium key knowledge explanation
利用C#实现Pdf转图片
Software testing assignment - day 1
Software testing learning - day one
golang操作redis:写入、读取hash类型数据
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
Ruoyi interface permission verification
Thoughts in Starbucks
Resttemplate configuration use
Application scenarios of Catalan number
Code management tools