当前位置:网站首页>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 !
边栏推荐
- vmware虚拟机C盘扩容
- Unittest attempt
- Dbnet: real time scene text detection with differentiable binarization
- [attribute comparison] defer and async
- The 10000 hour rule won't make you a master programmer, but at least it's a good starting point
- Integration test practice (1) theoretical basis
- mysql误删root账户导致无法登录
- 2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
- On the practice of performance optimization and stability guarantee
- 如何迁移或复制VMware虚拟机系统
猜你喜欢
Golang operation redis: write and read hash type data
[untitled]
golang操作redis:写入、读取kv数据
10000小時定律不會讓你成為編程大師,但至少是個好的起點
Software testing assignment - day 1
Software testing learning - day one
机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
IC_EDA_ALL虚拟机(丰富版):questasim、vivado、vcs、verdi、dc、pt、spyglass、icc2、synplify、INCISIVE、IC617、MMSIM、工艺库
How to migrate or replicate VMware virtual machine systems
2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)
随机推荐
Winter vacation work of software engineering practice
Software testing learning - day one
Architecture notes
[classes and objects] explain classes and objects in simple terms
Abstract learning
How to plan well?
MySQL installation
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
Troubleshooting of high CPU load but low CPU usage
php安装composer
10000小时定律不会让你成为编程大师,但至少是个好的起点
2022年华东师范大学计科考研复试机试题-详细题解
Sorting out the core ideas of the pyramid principle
Dbnet: real time scene text detection with differentiable binarization
php artisan
【code】偶尔取值、判空、查表、验证等
修改MySQL密码
Resthighlevelclient gets the mapping of an index
Asynchronous programming: async/await in asp Net
保险公司怎么查高血压?