当前位置:网站首页>Cartoon: what is distributed transaction?
Cartoon: what is distributed transaction?
2022-07-05 16:10:00 【Small ash】
————— the second day —————
————————————
If there is no distributed transaction
In a series of microservice systems , If there are no distributed transactions , What will happen ? Let's take the trading business commonly used in the Internet as an example :
The figure above contains two independent microservices, inventory and order , Each microservice maintains its own database . In the business logic of the trading system , A product needs to call inventory service before placing an order , Deduct inventory , Call the order service , Create order records .
Under normal circumstances , The two databases were updated successfully , The data on both sides is consistent .
however , Under abnormal circumstances , It's possible that the inventory deduction has been completed , The subsequent order record failed to be inserted for some reason . This is the time , The data on both sides will lose their due consistency .
What is distributed transaction ?
Distributed transactions are used to ensure data consistency between different nodes in a distributed system . 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 contains Two-phase commit (2PC) and Three stage commit (3PC) Two implementations , Here we focus on the specific process of two-stage submission .
In world of Warcraft , Copy group play BOSS When , In order to facilitate the cooperation between the team leader and the team members , The captain can initiate a “ Position confirmation ” The operation of :
When the team member receives the confirmation of position , If it's in place , Just choose “ yes ”, If it's not in place yet , Just choose “ no ”.
When the captain receives confirmation of everyone's position , It's going to be released to all the players , Tell them to start fighting BOSS.
Corresponding , When the captain initiates the confirmation of position , It's possible that some of the players are not in place yet :
That's the world of Warcraft BOSS The confirmation process of . This process and XA The two-phase commit of the distributed transaction protocol is very similar .
that XA What exactly does the agreement look like ? stay XA There are two roles in the agreement : A business coordinator and Transaction participants . Let's take a look at the interaction process between them :
The first stage :
stay XA The first phase of distributed transactions , The node that acts as the transaction coordinator will first send Prepare request .
After receiving Prepare After the request , Each participant node will update the data related to the transaction , write in Undo Log and Redo Log. If the participant performs successfully , Do not commit the transaction for the time being , Instead, it returns... To the transaction coordination node “ complete ” news .
When the transaction coordinator receives a return message from all participants , The whole distributed transaction will enter the second phase .
The second stage :
stay XA The second phase of distributed transactions , If the transaction coordination node has received all the data before, it will return in a positive direction , Then it will issue... To all transaction participants Commit request .
Receive Commit After the request , The transaction participant nodes will commit their own local transactions , And release the lock resource . When the local transaction is committed , Will return to the transaction coordinator “ complete ” news .
When the transaction coordinator receives the “ complete ” feedback , The whole distributed transaction is completed .
What is described above is XA The forward process of two-phase submission , Next, let's take a look at the process of handling the failure situation :
The first stage :
The second stage :
stay XA The first stage of , If a transaction participant feeds back a failure message , Indicates that the local transaction execution of this node is not successful , Must roll back .
So in the second stage , The transaction coordination node sends Abort request . Received Abort After the request , Each transaction participant node needs to roll back the transaction locally , The rollback operation follows Undo Log To carry out .
That's all XA The detailed process of submitting the agreement in two stages .
XA The shortcomings of two-phase submission
XA What are the shortcomings of the two-stage submission ?
1. Performance issues
XA The protocol follows strong consistency . During transaction execution , Each node occupies the database resources , Only when all nodes are ready , The transaction coordinator will notify the commit , Release resources after participants submit . This process has very obvious performance problems .
2. Coordinator single point of failure problem
The transaction coordinator is the whole XA The core of the model , Once the transaction coordinator node fails , Participants cannot receive commit or rollback notification , Participants will always be in an intermediate state, unable to complete the transaction .
3. Inconsistencies caused by missing messages .
stay XA The second phase of the agreement , If there is a local network problem , Some transaction participants received a commit message , Another part of the transaction participants did not receive the commit message , This leads to data inconsistency between nodes .
If you avoid XA All kinds of problems in the two-stage submission ? There are many other distributed transaction schemes to choose from :
1.XA Three stage commit
XA The three-phase commit adds... To the two-phase commit CanCommit Stage , And introduced the timeout mechanism . Once the participants don't get a call from the coordinator commit request , Will be automatically local commit. This effectively solves the problem of coordinator single point of failure . But the problems of performance and inconsistency are still not fundamentally solved .
2.MQ Business
Using message oriented middleware to update the second half of the transaction asynchronously , Achieve the ultimate consistency of the system . This approach avoids things like XA Protocol performance issues .
3.TCC Business
TCC The business is Try、Commit、Cancel Abbreviations for the three instructions , Its logical pattern is similar to XA Two-phase commit , But the way to implement it is to implement it artificially at the code level .
Some supplements :
. This cartoon is purely entertainment , Please cherish your current work as much as possible , Don't imitate Xiao Hui's behavior .
—————END—————
边栏推荐
猜你喜欢

单商户 V4.4,初心未变,实力依旧!

Coding devsecops helps financial enterprises run out of digital acceleration

The visual experience has been comprehensively upgraded, and Howell group and Intel Evo 3.0 have jointly accelerated the reform of the PC industry

obj集合转为实体集合

写单元测试的时候犯的错

降本40%!Redis多租户集群的容器化实践

数据湖(十四):Spark与Iceberg整合查询操作

《21天精通TypeScript-3》-安装搭建TypeScript开发环境.md

Which keywords will conflict with the abstract keyword

【网易云信】超分辨率技术在实时音视频领域的研究与实践
随机推荐
Quick completion guide for manipulator (IX): forward kinematics analysis
《MongoDB入门教程》第04篇 MongoDB客户端
List de duplication and count the number
vant tabbar遮挡内容的解决方式
[graduation season] as a sophomore majoring in planning, I have something to say
利用GrayLog告警功能实现钉钉群机器人定时工作提醒
Intelligent metal detector based on openharmony
Data communication foundation - route republication
Defining strict standards, Intel Evo 3.0 is accelerating the upgrading of the PC industry
公司自用的国产API管理神器
Solve the Hanoi Tower problem [modified version]
Is it safe for Guotai Junan to open an account online
【网易云信】超分辨率技术在实时音视频领域的研究与实践
Cartoon: what is the eight queens problem?
Transaction rollback exception
abstract关键字和哪些关键字会发生冲突呢
【毕业季】作为一名大二计科在校生,我有话想说
Noi / 1.4 07: collect bottle caps to win awards
Fundamentals of data communication - Principles of IP routing
开发中Boolean类型使用遇到的坑