当前位置:网站首页>[distributed theory] (I) distributed transactions
[distributed theory] (I) distributed transactions
2022-07-07 17:37:00 【Lin like】
Distributed transactions
1, What is distributed transaction
Local transactions : Transactions controlled by relational databases , All transactions have ACID
Four characteristics , And here's C It means strong consistency , Depend on AID Realize and realize .Mysql The implementation of transactions depends on logs 、MVCC
、ReadView
methods .
Distributed transactions : In a distributed system , Multiple systems operate a transaction together , This transaction can occur in a database , It can also occur in multiple databases , These are all distributed transaction problems . There is another case , Our business has carried out sub database operation , A business involves multiple data sources , It is also a distributed transaction problem .
so , As long as cross system data interaction occurs, distributed transactions are involved . Typical scenes are , Order system and inventory system of e-commerce system ; Recharge and payment in the financial scene .
2, The theoretical basis of distributed transaction processing
2.1,CAP theory
How to deal with distributed transactions , Usually we should follow CAP
theory .
CAP:— Sexual nature (Consistency); Usability (Availability); Partition tolerance (Partition Tolerance). There's only two out of three .
Consistency requirements , The data read by the client from the node must be consistent ; Availability requirements , The data read by the client from the node is not necessarily the latest , But you must be able to read the data ; Partition fault tolerance requirements , Node failure , Other nodes can still provide services .P It must be implemented in distributed systems .
CAP Combination of :
1、CA: Give up zoning tolerance , Enhance consistency and availability , For example, relational databases follow CA Design ;
2、AP: Give up consistency , Enhance availability and partition tolerance , The pursuit of ultimate consistency , quite a lot NoSQL Database according to AP Design . explain : Abandoning consistency here means abandoning strong consistency , Strong consistency is to query the latest data immediately after the write is successful . The pursuit of final consistency means allowing temporary data inconsistencies , As long as the end user accepts the data within the time It's OK to be consistent .
3、CP: Discard availability , Strengthen consistency and zoning tolerance , Some systems with strong consistency requirements are CP Design , For example, inter-bank transfer , A transfer request is not complete until the banking systems of both parties have completed the whole transaction .
In our actual system design , It's using AP+ The final consistent plan . For example, our order refund , The form of delayed arrival .
2.2,Base theory
BASE The theory is CAP In theory AP Extension of contract , Emphasize basic availability (Basically Available)、 Soft state (Soft State) And final conformity (Eventual Consistency).
Basic available : The sacrificial part is available , Ensure the normal operation of the core system ;
Soft state : There is an intermediate state in operation , For example, the delay in master-slave synchronization ;
Final consistency : Reach consistency at the final time .
3, Distributed transaction solutions
3.1,2PC
I.e. two phase submission agreement , By introducing a transaction manager as coordinator .
The first stage : vote . The transaction manager sends CanCommit
request , Other nodes agree , Then execute the transaction operation in the request , Write to the log ; If you disagree, terminate the operation ;
The second stage : Submit . The transaction manager receives yes After reply , Send to participants DoCommit
The commit instruction executes the commit operation ; otherwise , Send rollback instructions DoAbort
, Participants perform rollback locally , End of transaction .
analysis : There is a synchronization blocking problem in transactions , Single point problem , Data inconsistency ( Some participants' networks are abnormal ).2PC Strong consistency , such as Mysql Medium redolog
Log submission adopts 2PC programme .
3.2,3PC
2PC Improvement , Introduce pre submission and timeout mechanisms , solve 2PC Synchronization blocking in . It is divided into CanCommit
,PreCommit
,DoCommit
Stage .3PC Is characterized by , If there is no response for a long time , The transaction is committed by default , Not like 2PC So consistent blocking .
however 3PC Undoubtedly, it increases the communication complexity of the system , And it will also lock resources , Cannot solve the problem of data consistency .
3.3,TCC
2PC Improvement , Pay attention to business compensation . differ 2PC,3PC Implement transactions at the database level ,TCC It is to solve business problems at the business level . Include Try
,Confirm
,Cancle
Three stages .TCC The transaction scheme interface needs to meet idempotency , And each interface needs to be implemented Try,Confirm,Cancle Interface , High development cost .
3.4, Message queuing achieves final consistency
That is, through the retry mechanism of message queue , Try again and again , Achieve ultimate consistency . The premise is that the business interface is idempotent .
边栏推荐
- [fan Tan] those stories that seem to be thinking of the company but are actually very selfish (I: building wheels)
- 本周小贴士#136:无序容器
- 【网络攻防原理与技术】第4章:网络扫描技术
- LeetCode刷题day49
- Functions and usage of imageswitch
- 阿富汗临时政府安全部队对极端组织“伊斯兰国”一处藏匿点展开军事行动
- Notes on installing MySQL in centos7
- datepicket和timepicket,日期、时间选择器的功能和用法
- Rpcms method of obtaining articles under the specified classification
- 本周小贴士#141:注意隐式转换到bool
猜你喜欢
随机推荐
centos7安装mysql笔记
LeetCode 515(C#)
Audio Device Strategy 音频设备输出、输入 选择 基于7.0 代码
Matplotlib绘图界面设置
actionBar 导航栏学习
本周小贴士131:特殊成员函数和`= default`
ViewSwitcher的功能和用法
viewflipper的功能和用法
Examen des lois et règlements sur la sécurité de l'information
With the latest Alibaba P7 technology system, mom doesn't have to worry about me looking for a job anymore
第1章CRM核心业务介绍
大笨钟(Lua)
【网络攻防原理与技术】第3章:网络侦察技术
【OKR目标管理】价值分析
深入浅出图解CNN-卷积神经网络
【网络攻防原理与技术】第7章:口令攻击技术 第8章:网络监听技术
Mysql 索引命中级别分析
LeetCode 535(C#)
The mail server is listed in the blacklist. How to unblock it quickly?
使用popupwindow創建对话框风格的窗口