当前位置:网站首页>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—————
边栏推荐
- The difference between abstract classes and interfaces
- 《MongoDB入门教程》第04篇 MongoDB客户端
- Data communication foundation NAT network address translation
- 基于OpenHarmony的智能金属探测器
- 开发中Boolean类型使用遇到的坑
- ES6深入—async 函数 与 Symbol 类型
- Basic JSON operations of MySQL 5.7
- 超分辨率技术在实时音视频领域的研究与实践
- obj集合转为实体集合
- CODING DevSecOps 助力金融企业跑出数字加速度
猜你喜欢
How difficult is it to pass the certification of Intel Evo 3.0? Yilian technology tells you
ES6深入—ES6 Class 类
Example project: simple hexapod Walker
Subclasses and superclasses of abstract classes
Vulnhub-Moneybox
18.[stm32] read the ROM of DS18B20 temperature sensor and realize multi-point temperature measurement
18.[STM32]读取DS18B20温度传感器的ROM并实现多点测量温度
19.[STM32]HC_SR04超声波测距_定时器方式(OLED显示)
17.[STM32]仅用三根线带你驱动LCD1602液晶
【 note 】 résoudre l'erreur de code IDE golang
随机推荐
Intelligent metal detector based on openharmony
《21天精通TypeScript-3》-安装搭建TypeScript开发环境.md
降本40%!Redis多租户集群的容器化实践
Five common negotiation strategies of consulting companies and how to safeguard their own interests
Boost the development of digital economy and consolidate the base of digital talents - the digital talent competition was successfully held in Kunming
This article takes you through the addition, deletion, modification and query of JS processing tree structure data
通过的英特尔Evo 3.0整机认证到底有多难?忆联科技告诉你
抽象类和接口的区别
超分辨率技术在实时音视频领域的研究与实践
Modify PyUnit_ Time makes it support the time text of 'xx~xx months'
迁移/home分区
Arduino controls a tiny hexapod 3D printing robot
异常com.alibaba.fastjson.JSONException: not match : - =
RLock锁的使用
《MongoDB入门教程》第04篇 MongoDB客户端
Codasip为RISC-V处理器系列增加Veridify安全启动功能
Use of RLOCK lock
Noi / 1.4 07: collect bottle caps to win awards
16.[STM32]从原理开始带你了解DS18B20温度传感器-四位数码管显示温度
sql中查询最近一条记录