当前位置:网站首页>Introduction to distributed transactions (Seata)
Introduction to distributed transactions (Seata)
2022-07-01 13:43:00 【ha_ lydms】
One 、 brief introduction
1、 Local and distributed transactions
1.1 Business
Database transactions ( abbreviation : Business ,Transaction) Refers to the database ⾏ In process ⼀ A logical unit , from ⼀ A limited sequence of database operations .
Transactions have the following four characteristics , It's used to be called ACID characteristic :
primary ⼦ sex (Atomicity): Business as ⼀ The whole is held ⾏, The operations on the database contained in it are either all performed ⾏, Or none of them ⾏.
⼀ Sexual nature (Consistency): Transactions should ensure that the state of the database changes from ⼀ individual ⼀ Cause the state to change to another ⼀ individual ⼀ To state .⼀ To state means that the data in the database should be full ⾜ Integrity constraints . besides ,⼀ There's more to sex ⼀ Layer semantics , The intermediate state of a transaction cannot be observed ( This layer of semantics also says that it should belong to the original ⼦ sex ).
Isolation, (Isolation): Multiple transactions execute concurrently ⾏ when ,⼀ The execution of a transaction ⾏ It shall not affect the execution of other affairs ⾏, Just like this ⼀ An operation is being performed by the database ⾏⼀ sample .
persistence (Durability): Changes made to the database by committed transactions should be persisted in the database . At the end of the transaction , This operation will be irreversible .
1.2 Local transactions
At first , Transactions are limited to single transactions ⼀ Access control of database resources , After the service of Architecture , The concept of transactions extends to services . If will ⼀ A list ⼀ Service operation of as ⼀ One transaction , Then the whole service operation can only involve ⼀ A list ⼀ Database resources , This type is based on a single service order ⼀ Database resource access transactions , It's called local affairs (Local Transaction).

1.3 Distributed transactions
Distributed transaction refers to the participants of transaction 、⽀ Server holding transactions 、 Resource servers and transaction managers are located on different nodes of different distributed systems , And belong to different should ⽤, Distributed transactions need to ensure that either all of these operations succeed , All or nothing . In essence , Distributed transaction is to ensure the data of different databases ⼀ Sexual nature . When ⼀ Service operations access different database resources ,⼜ When you want to have transactional access to them , You need to pick ⽤ Distribute transactions to coordinate all transaction participants .
The figure below reflects this ⼀ A distributed transaction that spans multiple services :

If will go up ⾯ These two scenarios (⼀ Services can be adjusted ⽤ Multiple database resources , It can also be adjusted ⽤ Other services ) Combined in ⼀ rise , In this regard ⾏ extend , The participants of the whole distributed transaction will form a tree topology as shown in the figure below .

2、 Distributed transaction theory
2.1 CAP Theorem

CAP The theorem is in 1998 California ⼤ Computer scientists who study Eric Brewer ( Egyptian ⾥ g . Brewer ) Put forward , There are three indicators of a distributed system , These three indicators cannot be achieved at the same time .
- Consistency ⼀ Sexual nature
- Availability can ⽤ sex
- Partition tolerance Partition fault tolerance
Partition fault tolerance (Partition tolerance)
Distributed system cluster ,⼀ A broken machine should not affect other machines
⼤ Most distributed systems are distributed in multiple ⼦⽹ Collateral . Every ⼦⽹ Luo is called ⼀ Districts (partition). Partition fault tolerance means , Interval communication may fail .⽐ Such as ,⼀ Servers are in China , another ⼀ Servers are located in the United States , These are the two areas , There may be ⽆ France communications .
⼀ In general , Partition fault tolerance ⽆ There is no way to avoid , So we can think that CAP Of P Always become ⽴.CAP The theorem tells us , The rest C and A ⽆ At the same time .
can ⽤ sex (Availability)
Just receive ⽤ User's request , The server has to respond ⽤ Users can choose to go to G1 or G2 Initiate read operation . No matter which server , Just receive the request , You have to tell ⽤ Household , What is the v0 still v1, Otherwise, you will be dissatisfied ⾜ can ⽤ sex .
⼀ Sexual nature (Consistency)
⼀ You will be able to read the latest data
Consistency in ⽂ be called ⼀ Sexual nature
. intend , Read operation after write operation , The value must be returned .
⼀ Consistency and predictability ⽤ Sexual ⽭ shield
⼀ Consistency and predictability ⽤ sex (A), Why can't it be at the same time ⽴? The answer is simple , Because communication may fail ( That is, partition fault tolerance ).
If you promise G2 Of ⼀ Sexual nature , that G1 Must be in writing operation , lock G2 Read and write operations . Only after data synchronization , To reopen reading and writing . Lock in period ,G2 Can't read or write , Nothing to ⽤ sex (CP).
If you promise G2 Yes ⽤ sex , Then it's bound to fail to lock in G2, therefore ⼀ It's impossible to be sexual ⽴(AP).
in summary ,G2 ⽆ At the same time ⼀ Consistency and predictability ⽤ sex . Only ⼀ individual ⽬ mark . If you pursue ⼀ Sexual nature , that ⽆ Method to ensure the scalability of all nodes ⽤ sex ; If we pursue the scalability of all nodes ⽤ sex , Then you can't do it ⼀ Sexual nature .
2.2 Base theory
BASE: Full name :Basically Available( Basically ⽤),Soft state( Soft state ), and Eventually consistent( Final ⼀ Sexual nature ) Abbreviations of three phrases , Come on ⾃ ebay The architect of .BASE The theory is right CAP in ⼀ Consistency and predictability ⽤ The result of the sex trade-off , It comes from ⼤ Type Interconnection ⽹ Summary of distributed practice , Is based on CAP The theorem evolves gradually ⽽ To the . Its core ⼼ Thought is :
both ⽆ The law is strong ⼀ Sexual nature (Strong consistency), But everyone should ⽤ According to ⾃ Personal business characteristics , Mining ⽤ proper ⽅ Formula to make the system reach the final ⼀ Sexual nature (Eventual consistency).
Basically Available( Basically ⽤)
understand : Allow service degradation or allow response time to be affected ⼀ Fixed loss
What is basic ⽤ Well ? Suppose the system , There was an unforeseen breakdown , But you can still ⽤, phase ⽐ A more normal system ⽽⾔:
- Loss of response time : Normal search engines 0.5 Seconds is returned to ⽤ Household results ,⽽ Basically ⽤ The search engine can be found in 1 Seconds for ⽤ Return results .
- Loss of function : stay ⼀ E-commerce ⽹ On the site , Under normal circumstances ,⽤ You can successfully complete every ⼀ Orders , But here we are ⼤ Promotion period , In order to protect the stability of the shopping system , Some consumers may be guided to ⼀ Downgrades ⻚⾯.
Soft state( Soft state )
understand : Appears when data synchronization is allowed ⼀ Time delay
What is soft state ? Relative to the original ⼦ sex ⽽⾔, Data copies of multiple nodes are required ⼀ To , This is a ⼀ Kind of “ Hard state ”.
Soft state means : Allow data in the system to have intermediate states , It is considered that this state does not affect the overall availability of the system ⽤ sex , That is to say, the system is allowed to have data delay in data copies of multiple different nodes .
Eventually consistent( Final ⼀ Sexual nature )
understand : after ⼀ After synchronizing data for a period of time , Finally, we can achieve ⼀ individual ⼀ The state of being .
The system can ensure that there is no other new update operation , The data finally ⼀ It is certain to achieve ⼀ The state of being , Therefore, all client data access to the system can finally get the latest value .
3、 Distributed transaction solutions
3.1 be based on XA Two phase submission of the agreement

You know XA Distributed transactions in the specification include AP,RM,TM form :
Among them ⽤ Program (Application Program , abbreviation AP): AP Define transaction boundaries ( Define transaction start and end ) And access the resources within the transaction boundary .
Explorer (Resource Manager, abbreviation RM): Rm Manage computer shared resources , Many software can access these resources , Resources include ⽐ Such as a database 、⽂ Piece system 、 Printer server, etc .
Transaction manager (Transaction Manager , abbreviation TM): Responsible for managing the overall affairs , Assign transactions only ⼀ identification , Monitor the execution of transactions ⾏ speed of progress , And responsible for the submission of affairs 、 Roll back 、 Failure recovery, etc .
⼆ Phase agreement :
The first ⼀ Stage TM: All required RM Prepare to submit the corresponding transaction points ⽀, inquiry RM Is there any way ⼒ Ensure the successful submission of transactions ⽀,RM according to ⾃⼰ The situation of , If you judge ⾃⼰ Into the ⾏ Of ⼯ Work can be submitted , That's right ⼯ Make content into ⾏ Persistence , And give TM receipt OK; If not, give it TM The receipt of NO.RM After sending a negative reply and rolling back the already ⼯ After work , You can discard this transaction ⽀ Information. .
The first ⼆ Stage TM: According to the stage 1 each RM prepare Result , Decide whether to commit or roll back the transaction . If all RM all prepare success , that TM Inform all RM Into the ⾏ Submit ; If there is RM prepare receipt NO Words , be TM Inform all RM Roll back ⾃⼰ Affairs of ⽀.
That is to say TM And RM The agreement is submitted in two stages ⾏ Interaction .
advantage : Try to ensure the strength of the data ⼀ Cause , Suitable for strong data ⼀ The request is very ⾼ Key areas .( In fact, it can't be 100% Strong guarantee ⼀ Cause )
shortcoming : The implementation is complex , Sacrifice, but ⽤ sex , The impact on performance is relatively ⼤, Not suitable for ⾼ Concurrent ⾼ Performance scenarios .
3.2 TCC Compensation mechanism
TCC It's actually picking ⽤ Compensation mechanism , Its core ⼼ Thought is : For each operation , You have to register ⼀ Corresponding confirmation and compensation ( revoke ) operation . It's divided into three stages :
- Try The stage is mainly to test the business system and reserve resources .
- Confirm The phase is mainly to confirm and submit the business system ,Try Stage execution ⾏ Succeed and start to implement ⾏ Confirm When the stage , Default Confirm There is no mistake in the stage . namely : as long as Try success ,Confirm⼀ Success is certain .
- Cancel The stage is mainly in business execution ⾏ error , Execute when rollback is required ⾏ Our business is cancelled , Reserve resource release .

A Want to B Transfer accounts , Ideas ⼤ It's all about :
We have ⼀ A local ⽅ Law ,⾥⾯ In turn ⽤
1、⾸ First in Try Stage , First adjust ⽤ Remote access ⼝ hold B and A The money is frozen .
2、 stay Confirm Stage , Of board ⾏ Remote dispatching ⽤ Transfer operation , The transfer succeeded in ⾏ thaw .
3、 If the first 2 Step execution ⾏ success , Then the transfer is successful , If the first ⼆ Step execution ⾏ Failure , Then tune ⽤ Remote freezing connection ⼝ Corresponding thawing ⽅ Law (Cancel).
advantage : phase ⽐ Two-phase commit , can ⽤ sex ⽐ Stronger
shortcoming : Data ⼀ Poor consistency ⼀ some .TCC Belong to should ⽤ Layer of ⼀ Kind of compensation ⽅ type , So programmers need to write a lot of compensation code in the implementation , stay ⼀ In some scenes ,⼀ Some business processes may ⽤TCC Not very well defined and handled .
3.3 The message is ultimately consistent
The news finally ⼀ Consistency should be made by the industry ⽤ The most , Its core ⼼ The idea is to split distributed transactions into local transactions ⾏ Handle , This kind of thinking comes from ebay. We can start from the bottom ⾯ See... In the flow chart of ⼀ Some details :

The basic idea is :
news ⽣ production ⽅, Additional construction is required ⼀ A message table , And record the message sending status . The message table and business data should be in ⼀ One transaction ⾥ Submit , That is to say, they are going to ⼀ A database ⾥⾯. Then the message will pass MQ Consumption sent to message ⽅. If the message fails to be sent , Will enter ⾏ Try sending again .
News consumption ⽅, This message needs to be processed , And finish ⾃⼰ Business logic of . At this time, if the local transaction is successful , Indicates that the process has been successful , If the processing fails , Then it will try again ⾏. If it is business ⾯ The failure of the , You can give ⽣ production ⽅ send out ⼀ Business compensation messages , notice ⽣ production ⽅ Into the ⾏ Rollback and other operations .
⽣ production ⽅ And consumption ⽅ Scan local message table regularly , Send messages that have not been processed or failed ⼀ All over . If there are reliable ⾃ Dynamic reconciliation
Replenishment logic , such ⽅ The case is still ⾮ Chang Shi ⽤ Of .
advantage : ⼀ Kind of ⾮ Often classic implementation , Avoid distributed transactions , Finally ⼀ Sexual nature .
shortcoming : The message table is coupled to the business system , If there is no encapsulated solution ⽅ case , There will be a lot of chores to deal with .
Two 、 Distributed transactions seata
1、 brief introduction
Seata( Original name Fescar) Yeah ⾥18 Open source framework for distributed transactions .Fescar Open source has a great impact on the field of distributed transaction framework ⼤. do
Open source ⼤ Household ,Fescar Come on ⾃ o ⾥ Of GTS, Experienced good ⼏ Secondary double ⼗⼀ The test of ,⼀ Open source has attracted much attention . later Fescar Renamed as
Seata.
Fescar Although it is ⼆ Phase commit protocol for distributed transactions , But it solved XA Of ⼀ There are some shortcomings :
A single point of the problem : although ⽬ front Fescar(0.4.2) Or just server Of , however Fescar Officer, ⽅ It is expected to be in 0.5.x Launched in HA-Cluster, Then we can solve the single point problem .
Synchronous blocking : Fescar Of ⼆ Stage , Its second ⼀ During the phase, the local transaction has been committed to release resources , Don't like XA There will be two more prepare and commit Stage resources are locked , also Fescar,commit Is asynchronous operation , It also improves performance ⼀⼤ The key .
Data not available ⼀ Cause : If there's a part of commit Failure , that fescar-server According to the current transaction mode and sub ⽀ The result of the return state of the transaction comes in ⾏ Different retry strategies . also fescar The local transaction of will be in ⼀ When the stage goes ⾏ Submit , In fact, just look at the database commit The database was already ⼀ To .
Can only ⽤ Yu Dan ⼀ database : Fescar There are two modes ,AT and TCC. stay AT In mode, transaction resources can be any ⽀ a ACID The database of , stay TCC There is no limit to transaction resources in mode , It can be cache , It can be ⽂ Pieces of , It can be others and so on . Of course, these two modes can also be mixed ⽤.
meanwhile Fescar Also kept close 0 Business ⼊ Advantages of invasion , Just a simple configuration Fescar Data proxy and add a comment , Add ⼀ individual Undolog surface , We can achieve what we want ⽬ Of .
2、 Realization principle
Fescar take ⼀ Local transactions as ⼀ Distributed transactions are divided into ⽀, So if ⼲ Local transactions distributed in different microservices together form ⼀ A global transaction , The structure is as follows .

TM: Global transaction manager , On the label fescar The server of distributed transactions is turned on , And send the global transaction to TC Transaction control side management
TC: In transaction control ⼼, Control the commit or rollback of global transactions . This component needs to be unique ⽴ Deployment maintenance ,⽬ The first one ⽀ Stand alone version , The subsequent iteration plan will have a cluster version
RM: Explorer , Mainly responsible for sub ⽀ Escalation of transactions , Management of local transactions
⼀ This paragraph briefly describes the implementation process :
Service start ⽅ Initiate a global transaction and register to TC.
In tune ⽤ Collaborative services , The transaction of collaborative service is divided into ⽀ The transaction will complete the phase first ⼀ Commit or roll back transactions for , and ⽣ Transaction rollback undo_log⽇ Records ,
At the same time, register the current collaboration service to TC And report its transaction status , Merge into the same ⼀ In the global transactions of businesses .
If there is no problem at this time, continue ⼀ Coordination of collaborative services ⽤, Points of any collaborative services during ⽀ Transaction rollback , You'll be informed TC,TC After notifying the global transaction that all contained have been completed ⼀ Stage submitted sub ⽀ Transaction rollback .
If all points ⽀ Everything is normal , Finally, return to the global transaction initiation ⽅ when , Will also inform TC,TC After notifying all the points contained in the global transaction ⽀ Delete rollback ⽇ Records .
In this process, in order to solve the problems of write isolation and degree isolation, it will involve TC Managed global lock .
3、Fescar Pattern
Fescar The implementation of distributed transactions provides 2 Patterns ,AT Patterns and TCC Pattern :
3.1 TCC Pattern
TCC Pattern : TCC Compensation mechanism , Cause ⼀ Definite invasion ⼊, It is difficult to realize ⼤, such ⽅ The formula is not recommended , however TCC The pattern is characterized by performance ⾼.
TCC The code of the mode part is as follows : You can see clinging ⾏ Transaction rollback , All need to be implemented according to different stages ⾏ State judgment , Invade ⼊ Business code .
@Override
@GlobalTransactional
public boolean transfer(final String from, final String to, final double amount) {
// Deduction participant ,⼀ Stage execution ⾏
boolean ret = firstTccAction.prepareMinus(null, from, amount);
if (!ret) {
// Deduction participant ,⼀ Phase failure ; Rollback local and distributed transactions
throw new RuntimeException(" account number :[" + from + "] Withholding failed ");
}
// Plus participants ,⼀ Stage execution ⾏
ret = secondTccAction.prepareAdd(null, to, amount);
if (!ret) {
throw new RuntimeException(" account number :[" + to + "] Advance collection failed ");
}
System.out.println(String.format("transfer amount[%s] from [%s] to [%s] finish.", String.valueOf(amount), from, to));
return true;
}
3.2 AT Pattern
AT Pattern : Focus on more DB Data accessed ⼀ Sexual nature , Come true ⽐ Simpler , Invasion of business ⼊ a ⼩, But the performance is not TCC⾼, This mode is recommended ⼤ Home agent ⽤.
AT The code of the mode part is as follows : There is no need to pay attention to ⾏ state , Invasion of business code ⼊ a ⼩.
@GlobalTransactional(timeoutMills = 300000, name = "dubbo-demo-tx")
public void purchase(String userId, String commodityCode, int orderCount) {
LOGGER.info("purchase begin ... xid: " + RootContext.getXID());
storageService.deduct(commodityCode, orderCount);
orderService.create(userId, commodityCode, orderCount);
throw new RuntimeException("AT Pattern hair ⽣ abnormal , Roll back the transaction ");
}
3.3 AT Pattern design ideas
AT Kernel of mode ⼼ It's for business ⽆ Invade ⼊, yes ⼀ Improved two-stage submission .
The first stage :

nucleus ⼼ It's about business sql Into the ⾏ analysis , convert to undolog, Two phase commit often locks resources until ⼆ Stage the actual commit or rollback operation ,⽽ With rollback ⽇ After Zhi , In the first ⼀ Phase release lock on resource , Reduced lock range , carry ⾼ efficiency , Even if the ⼆ Stage hair ⽣ Exceptions require rollback , Just find the right undolog The corresponding data in and inversely parsed into sql To achieve rollback ⽬ Of .Seata Through proxy data sources, the business is sql The hold of ⾏ It can be interpreted as undolog To update business data at the same time ⼊ library , To achieve the business ⽆ Invade ⼊ The effect of .
The second stage :
If the resolution is submitted as a whole , At this point ⽀ The transaction has now been committed , There is no need for synchronous coordination ( Just need asynchronous cleanup rollback ⽇ Records ),Phase2 Sure ⾮ Often finish quickly .

If the resolution is a global rollback ,RM Received a rollback request from the coordinator , adopt XID and Branch ID Find the corresponding rollback ⽇ Records , By rolling back records ⽣ Update in reverse SQL And hold ⾏, To complete points ⽀ Roll back of .

4、 Official information
Official website
GitHub Address
https://seata.io/zh-cn/
https://github.com/seata/seata
5、 Quick start
pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>${spring-cloud-alibaba.version}</version>
</dependency>
Code :
package com.work.order.service;
import com.work.order.model.Order;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
@Service
public class OrderService {
@GlobalTransactional
@Transactional(rollbackFor = Exception.class)
public void placeOrder(String userId, String commodityCode, Integer count) {
BigDecimal orderMoney = new BigDecimal(count).multiply(new BigDecimal(5));
Order order = new Order().setUserId(userId).setCommodityCode(commodityCode).setCount(count).setMoney(
orderMoney);
orderDAO.insert(order);
stockFeignClient.deduct(commodityCode, count);
}
}
边栏推荐
- Sign APK with command line
- 面试题目总结(1) https中间人攻击,ConcurrentHashMap的原理 ,serialVersionUID常量,redis单线程,
- Benefiting from the Internet, the scientific and technological performance of overseas exchange volume has returned to high growth
- 单工,半双工,全双工区别以及TDD和FDD区别
- 6年技术迭代,阿里全球化出海&合规的挑战和探索
- 机器学习总结(一):线性回归、岭回归、Lasso回归
- 啟動solr報錯The stack size specified is too small,Specify at least 328k
- Analysis report on the development pattern of China's smart emergency industry and the 14th five year plan Ⓠ 2022 ~ 2028
- Explain IO multiplexing, select, poll, epoll in detail
- QT学习管理系统
猜你喜欢
QT社团管理系统
French Data Protection Agency: using Google Analytics or violating gdpr
终端识别技术和管理技术
5. Use of ly tab plug-in of header component
Explain IO multiplexing, select, poll, epoll in detail
啟動solr報錯The stack size specified is too small,Specify at least 328k
spark源码(五)DAGScheduler TaskScheduler如何配合提交任务,application、job、stage、taskset、task对应关系是什么?
QT学习管理系统
Interpretation of R & D effectiveness measurement framework
The stack size specified is too small, specify at least 328k
随机推荐
Research Report on China's software outsourcing industry investment strategy and the 14th five year plan Ⓡ 2022 ~ 2028
Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
Build a vc2010 development environment and create a tutorial of "realizing Tetris game in C language"
受益互联网出海 汇量科技业绩重回高增长
Camp division of common PLC programming software
Social distance (cow infection)
2022 · 让我带你Jetpack架构组件从入门到精通 — Lifecycle
How much money do novices prepare to play futures? Is agricultural products OK?
Global and Chinese polypropylene industry prospect analysis and market demand forecast report Ⓝ 2022 ~ 2027
[anwangbei 2021] Rev WP
MySQL 66 questions, 20000 words + 50 pictures in detail! Necessary for review
Machine learning summary (I): linear regression, ridge regression, Lasso regression
Global and Chinese n-butanol acetic acid market development trend and prospect forecast report Ⓧ 2022 ~ 2028
leetcode 322. Coin Change 零钱兑换(中等)
C语言课程设计题目
Solution to 0xc000007b error when running the game [easy to understand]
微机原理与接口技术知识点整理复习–纯手打
Have you ever encountered the problem that flynk monitors the PostgreSQL database and checkpoints cannot be used
北斗通信模块 北斗gps模块 北斗通信终端DTU