当前位置:网站首页>Introduction to distributed transaction Seata

Introduction to distributed transaction Seata

2022-04-23 14:52:00 Top high school students

Seata What is it?

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 .

Seata The three characters of

stay Seata In the framework of , There are three characters :

TC (Transaction Coordinator) - A business coordinator

Maintain the state of global and branch transactions , Drive global transaction commit or rollback .

TM (Transaction Manager) - Transaction manager

Define the scope of the global transaction : Start global transaction 、 Commit or roll back global transactions .

RM (Resource Manager) - Explorer

Manage resources for branch transactions , And TC Talk to register branch transactions and report the status of branch transactions , And drive branch transaction commit or rollback .

among ,TC For separately deployed Server Server side ,TM and RM For embedded in the application Client client .

stay Seata in , The life cycle of a distributed transaction is as follows :

 1.TM request TC Start a global transaction .TC Will generate a XID As the number of the global transaction .XID, It will propagate in the invocation link of microservices , Ensure that the subtransactions of multiple microservices are associated together . As soon as it enters the transaction method, it will generate XID , global_table Is the stored global transaction information .

2.RM request TC Register local transaction as branch transaction of global transaction , Through global transactions XID Association . When running the database operation method ,branch_table Storage transaction participant .

3.TM request TC tell XID Whether the corresponding global transaction is committed or rolled back .

4.TC drive RM We will XID Commit or roll back the corresponding local transaction .

Design thinking

AT The core of the pattern is no intrusion into the business , It's an improved two-stage submission , The design idea is shown in the figure

The first stage

Business data and rollback logging are committed in the same local transaction , Release local locks and connection resources . The core is to the business sql To analyze , convert to undolog, And put it in storage at the same time , How is this done ? First throw out a concept DataSourceProxy Proxy data sources , Through the name, you can basically guess what operation it is , Specific analysis will be made later

The second stage

Distributed transaction operation succeeded , be TC notice RM Delete asynchronously undolog

Distributed transaction operation failed ,TM towards TC Send rollback request ,RM Roger the coordinator TC Rollback request from , adopt XID and Branch ID Find the corresponding rollback log record , Generate reverse updates by rolling back records SQL And implement , To complete the rollback of the branch .

Overall execution process

​​​​​​ Design highlights

Compared with other distributed transaction frameworks ,Seata There are several highlights of the architecture :

1. The application layer is based on SQL The analysis realizes automatic compensation , So as to minimize business intrusion .

2. In distributed transactions TC( A business coordinator ) Independent deployment , Responsible for the registration of affairs 、 Roll back .

3. Write isolation and read isolation are realized through global lock .

The problem is

Performance loss

One Update Of SQL, A global transaction is required xid obtain ( And TC Communications )、before image( analysis SQL, Query the database once )、after image( Query the database once )、insert undo log( Write a database )、before commit( And TC Communications , Judge lock conflict ), All these operations require a remote communication RPC, And it's synchronous . in addition undo log When writing blob The insertion performance of the field is also not high . Write each SQL It's going to cost so much , A rough estimate would increase 5 Times the response time .

Cost performance

For automatic compensation , All transactions need to be mirrored and persisted , But in the actual business scenario , This is the success rate , Or how many percentage of distributed transaction failures need to be rolled back ? Estimate according to the principle of 28 , in order to 20% The transaction is rolled back , Need to put 80% Increased response time for successful transactions 5 times , This cost is compared to whether it's worth letting the application develop a compensation transaction ?

Global lock , Hot data

comparison XA,Seata Although in a successful stage will release the database lock , But one stage is commit The determination of the front global lock also lengthens the occupation time of the data lock , The cost ratio is XA Of prepare How much lower needs to be tested according to the actual business scenario . The introduction of global locks enables isolation , But the problem is congestion , Reduce concurrency , Especially hot data , This problem will be more serious .

Rollback lock release time

comparison XA,Seata Although in a successful stage will release the database lock , But one stage is commit The determination of the front global lock also lengthens the occupation time of the data lock , The cost ratio is XA Of prepare How much lower needs to be tested according to the actual business scenario . The introduction of global locks enables isolation , But the problem is congestion , Reduce concurrency , Especially hot data , This problem will be more serious .

Rollback lock release time

Seata When rolling back , You need to delete the undo log, Then we can release TC Lock in memory , So if the second phase is rollback , It takes longer to release the lock .

The deadlock problem

Seata The introduction of global lock will increase the risk of deadlock , But if a deadlock occurs , Will keep retrying , Finally, wait for the global lock timeout , It's not elegant , It also extends the time of database lock possession .

版权声明
本文为[Top high school students]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231450328733.html