当前位置:网站首页>[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 .
边栏推荐
猜你喜欢

请将磁盘插入“U盘(H)“的情况&无法访问 磁盘结构损坏且无法读取

让保险更“保险”!麒麟信安一云多芯云桌面中标中国人寿, 助力金融保险信息技术创新发展

Sator推出Web3遊戲“Satorspace” ,並上線Huobi

A tour of grpc:03 - proto serialization / deserialization

麒麟信安云平台全新升级!

【TPM2.0原理及应用指南】 9、10、11章

How to choose the appropriate automated testing tools?

What is cloud computing?

serachview的功能和用法

【TPM2.0原理及应用指南】 5、7、8章
随机推荐
网络攻防复习篇
使用Stace排除故障的5种简单方法
Flask搭建api服务-SQL配置文件
让保险更“保险”!麒麟信安一云多芯云桌面中标中国人寿, 助力金融保险信息技术创新发展
On Apache Doris Fe processing query SQL source code analysis
actionBar 导航栏学习
Siggraph 2022 best technical paper award comes out! Chen Baoquan team of Peking University was nominated for honorary nomination
User defined view essential knowledge, Android R & D post must ask 30+ advanced interview questions
viewflipper的功能和用法
LeetCode刷题day49
第3章业务功能开发(用户登录)
MRS离线数据分析:通过Flink作业处理OBS数据
Sator推出Web3游戏“Satorspace” ,并上线Huobi
L1-028 判断素数(Lua)
Biped robot controlled by Arduino
Functions and usage of viewflipper
企业经营12法的领悟
自定义View必备知识,Android研发岗必问30+道高级面试题
L1-019 谁先倒(Lua)
字符串 - string(Lua)