当前位置:网站首页>[microservice Architecture] distributed transactions
[microservice Architecture] distributed transactions
2022-07-23 23:42:00 【curd_ boy】
List of articles
Phase II message
Local message table
In the scheme based on local message table , Operate this transaction outside , Record in the message table
Other business , Provide operation interface
The scheduled task polls the local message table , Send the unexecuted message to the operation interface .
Operation interface processing succeeded , Returns the success identifier , Processing failed , Return failure ID .
The scheduled task receives the identification , Update the status of the message
Regular tasks are executed repeatedly according to a certain period
For repeated failures , You can set the maximum number of failures
Messages that exceed the maximum number of failures , No interface calls
Waiting for manual processing
For example, the payment scenario using Alipay , The system generates orders , After Alipay system pays successfully , Call the callback interface provided by our system , The callback interface updates the order status to paid . Callback notification execution failed , Alipay will call again after a period of time .
Disadvantages of the local message table scheme :
Message data and business data coupling , The message table needs to be formulated according to specific business scenarios , Not for public use . Even if you can share the message table , For the sub database business, each database needs a message table .
Only applicable to the final consistent business scenario .
RocketMQ Transaction message
Message queue RocketMQ Version provides similar XA or Open XA Distributed transaction function of , Through message queuing RocketMQ Version transaction message , It can achieve the final consistency of distributed transactions .
Rocketmq Disadvantages of message transaction
In extreme cases , Automatic processing of backchecks in transaction messages , It's solved RocketMQ Check the data inconsistency in extreme cases ( Has applied for a patent )
Multiple distributed transaction protocols support
SAGA:
SAGA It first appeared in 1987 year Hector Garcaa-Molrna & Kenneth Salem Published papers SAGAS in . Its core idea is to split long transactions into multiple short transactions , from Saga Transaction coordinator coordination , If each short transaction is successfully committed , Then the global transaction will complete normally , If a step fails , The compensation operation is called once in reverse order .
SAGA The transaction mode is DTM The most common pattern in , Mainly because SAGA The model is simple and easy to use , Less work , And it can solve the needs of most businesses .
For example, we want to carry out a business similar to bank inter-bank transfer , take A Medium 30 Yuan to B, according to Saga How things work , We will deal with the overall situation , It is divided into the following services :
- Transfer out (TransOut) service , Transfer out here will be operated A-30
- Transfer out compensation (TransOutCompensate) service , Rollback the above transfer out operation , namely A+30
- into (TransIn) service , The transfer will take place B+30
- Transfer in compensation (TransInCompensate) service , Rollback the above transfer in operation , namely B-30
Whole SAGA The logic of the transaction is :
Transfer out succeeded => Execution transfer succeeded => Global transaction completed
If an error occurs in the middle , For example, transfer in B An error occurred , Then the compensation operation of the executed branch will be called , namely :
Transfer out succeeded => Failed to execute transfer in => Successful implementation of transfer in compensation => Successfully execute transfer out compensation => Global transaction rollback completed

In this picture , Our global business sponsor , Arrange the information of the whole global transaction , Including the definition of forward operation and reverse compensation operation of each step , Submit to server , The server will follow the steps above SAGA The logic of .
If any forward operation fails , For example, the account balance is insufficient or the account is frozen , that dtm It will call the compensation operation of each branch , Roll back , Finally, the transaction rolled back successfully .

TCC: Try-Confirm-Cancel
TCC Transaction mode
What is? TCC,TCC yes Try、Confirm、Cancel Abbreviations of three words , The first is the Pat Helland On 2007 A piece published in was called 《Life beyond Distributed Transactions:an Apostate’s Opinion》 The paper put forward .
TCC It is divided into 3 Stages
- Try Stage : Trying to perform , Complete all business checks ( Uniformity ), Reserve necessary business resources ( Quasi isolation )
- Confirm Stage : If all branches Try It's all done , Go to Confirm Stage .Confirm Really do business , No business checks , Use only Try Business resources reserved in the stage
- Cancel Stage : If all branches Try One failed , Go to Cancel Stage .Cancel Release Try Business resources reserved in the stage .
In design ,TCC It is mainly used to deal with high consistency requirements 、 Short transactions that require more flexibility :
TCC How to achieve better consistency
For our A Inter bank transfer to B Scene , If the SAGA, Adjust the balance in the forward operation middle note , In compensation operation , Reverse adjustment balance , Then this will happen : If A Deduction successful , Amount transferred to B Failure , Finally roll back , hold A The balance of is adjusted to the initial value . In the whole process, if A I found my balance deducted , But the payee B Delayed receipt of funds , Then it will be right A Cause great trouble .
The above requirements are in SAGA Can't solve , But it can go through TCC To solve , The design skills are as follows :
- In the account balance Outside the field , Introduce another trading_balance Field
- Try Stage check whether the account is frozen , Check whether the account balance is sufficient , No problem , adjustment trading_balance ( That is, frozen funds in business )
- Confirm Stage , adjustment balance , adjustment trading_balance ( That is, unfrozen funds in business )
- Cancel Stage , adjustment trading_balance ( That is, unfrozen funds in business )
In this case , End user A You won't see your balance deducted , however B And the delay in receiving funds
XA Transaction mode
With the distributed transaction scenario , There will be ways to solve the problem ,XA Specification is the specification for distributed transactions . There are many ways to implement distributed transactions , The most representative is by Oracle Tuxedo The system proposes XA Distributed transaction protocol .XA The agreement consists of two phases (2PC) And three phase submission (3PC) Two implementations .
XA What is it?
XA By X/Open The specification of distributed transaction proposed by the organization ,XA The specification mainly defines ( overall situation ) Transaction manager and ( Local ) Explorer (RM) Interface between . Local databases such as mysql stay XA It plays RM role
AT Pattern is a non intrusive distributed transaction solution .
XA It is divided into two stages :
- The first stage (prepare): That is, all participants RM Prepare to execute the transaction and lock the required resources . participants ready when , towards TM The report is ready .
- The second stage (commit/rollback): When the transaction manager confirms that all participants (RM) all ready after , Send... To all participants commit command .
At present, the mainstream databases basically support XA Business , Include mysql、oracle、sqlserver、postgre
How does the local database support XA Of :
XA start '4fPqCNTYeSG' -- To start a xa Business
UPDATE `user_account` SET `balance`=balance + 30,`update_time`='2021-06-09 11:50:42.438' WHERE user_id = '1'
XA end '4fPqCNTYeSG'
XA prepare '4fPqCNTYeSG' -- Before this call , Connection is broken , Then the transaction will be rolled back automatically
-- When all participants have finished prepare, Then enter the second stage Submit
xa commit '4fPqCNTYeSG'


XA The characteristics of transactions are :
Easy to understand
Easy to develop , Operations like rollback , Automatically completed by the underlying database
Locked the resource for a long time , Low concurrency , It is not suitable for high concurrency businesses

AT(Auto Transaction) Pattern
Distributed transaction framework
SEATA
Seata Is an open source distributed transaction solution , Committed to providing high-performance and easy-to-use distributed transaction services .Seata Will provide users with AT、TCC、SAGA and XA Transaction mode , Create a one-stop distributed solution for users .
边栏推荐
猜你喜欢

ciscn_ 2019_ c_ one

ciscn_ 2019_ n_ eight

Code generation of DGS

虚拟机导入iso后 Operating System not found 解决方法

bjdctf_ 2020_ babystack

Arrayslist and sequence table -- Simulation Implementation

No wonder the application effect of ERP in domestic enterprises is generally not ideal

Esp8266 nodemcu - get real-time weather from Suning API

ciscn_2019_n_8

System memory introduction and memory management
随机推荐
js把数字转大写
Use of pairwise
Grey prediction (matlab)
[OGeek2019]babyrop
2022-7-22 face review + simple topic sorting
Navicat15 download and installation
第三章、组织代码
怎么开户买收益6%的理财产品呢?
The basic syntax of go language (variables, constants, basic data types, for, switch, case, array, slice, make, new, map)
jarvisoj_ level0
归并排序思想应用
难怪国内企业ERP应用效果普遍不理想
BGP basic experiment
世界最小的物质排名,灵子,弦子,夸克
PushGateway+Prometheus+Grafana构建Flink实时监控
PHP(2)
Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?
TOPSIS method (matlab)
bjdctf_2020_babystack
DGS file upload