当前位置:网站首页>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—————
边栏推荐
- 力扣今日题-729. 我的日程安排表 I
- Virtual base class (a little difficult)
- EDI许可证和ICP经营性证有什么区别
- The list set is summed up according to a certain attribute of the object, the maximum value, etc
- list去重并统计个数
- 一键安装脚本实现快速部署GrayLog Server 4.2.10单机版
- Write a go program with vscode in one article
- 公司自用的国产API管理神器
- 五种常见的咨询公司谈判策略以及如何维护自己的利益
- 16.[STM32]从原理开始带你了解DS18B20温度传感器-四位数码管显示温度
猜你喜欢

通过的英特尔Evo 3.0整机认证到底有多难?忆联科技告诉你

Data communication foundation OSPF Foundation

vlunhub- BoredHackerBlog Social Network

机械臂速成小指南(九):正运动学分析

Research and development efficiency measurement index composition and efficiency measurement methodology

sql中set标签的使用

Data communication foundation smart_ Link_&_ Monitor_ Link

OceanBase社区版之OBD方式部署方式本地安装

基于OpenHarmony的智能金属探测器

ES6深入—ES6 Generator 函数
随机推荐
21. [STM32] I don't understand the I2C protocol. Dig deep into the sequence diagram to help you write the underlying driver
Intelligent metal detector based on openharmony
The visual experience has been comprehensively upgraded, and Howell group and Intel Evo 3.0 have jointly accelerated the reform of the PC industry
通过的英特尔Evo 3.0整机认证到底有多难?忆联科技告诉你
vulnhub-FirstBlood
五种常见的咨询公司谈判策略以及如何维护自己的利益
Replknet: it's not that large convolution is bad, but that convolution is not large enough. 31x31 convolution. Let's have a look at | CVPR 2022
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Go language programming specification combing summary
写单元测试的时候犯的错
《21天精通TypeScript-3》-安装搭建TypeScript开发环境.md
Data communication foundation - dynamic routing protocol rip
Data communication foundation ACL access control list
ES6 deep - ES6 class class
Apiccloud cloud debugging solution
【漏洞预警】CVE-2022-26134 Confluence 远程代码执行漏洞POC验证与修复过程
单商户 V4.4,初心未变,实力依旧!
Record the pits encountered in the raspberry pie construction environment...
漫画:什么是蓝绿部署?
Five common negotiation strategies of consulting companies and how to safeguard their own interests