当前位置:网站首页>Implementation scheme of distributed transaction
Implementation scheme of distributed transaction
2022-07-06 11:50:00 【Ride the wind to break the bug】
List of articles
CAP Theorem
To see distributed transactions, you must first understand CAP Theorem
CAP Theorem in distributed system is : Uniformity , Usability , Zone tolerance
- Uniformity : Whether the data of multiple nodes in a distributed environment is strongly consistent
- Usability : Distributed services can consistently ensure availability , When the user makes a request , The service can return results in a limited time
- Zone tolerance : Especially the tolerance of network partition
For shared data systems , At most, you can only have CAP Two of them , There's no way to balance the three
- Any combination of the two has its applicable scenarios
- The real system should be ACID and BASE Get the mixture
- Different types of businesses can and should be treated differently
among ! Partition tolerance is indispensable !!!
BASE theory
The core idea
- Basic available (BasicallyAvailable)
When a distributed system fails , Allow the availability of the lost part to ensure the core availability - Soft state (SoftState)
Allow distributed systems to have intermediate states , This intermediate state will not affect the overall availability of the system - Final consistency (EventualConsistency)
After a certain period of time, all the replica data of the distributed system , In the end, we can reach a consistent state
The consistency pattern of consistency model data can be divided into the following 3 class :
- Weak consistency
After the data is updated successfully , Data in all replicas is consistent at any time , It is generally realized in synchronous mode - Final consistency
A form of weak consistency , After the data is updated successfully , The system does not promise to return the latest written value immediately , But it is guaranteed that the value of the last update operation will be returned finally
Strong consistency of distributed system data , Weak and final consistency can be achieved by Quorum NRW Algorithm analysis
Distributed implementation scheme
There are mainly the following five schemes :
XA programme ,TCC programme , Local message table , Reliable message final consistency scheme , Best effort notification scheme
XA programme ( Two stage proposal )
So-called XA The proposal is also called two-phase submission , There is a concept of transaction manager , Responsible for coordinating multiple databases ( Explorer ) The business of , The transaction manager first asks whether each database is ready , If every database replies OK, Then formally submit the transaction , Perform operations on various databases , If there is any one of the data, answer no OK, Then roll back the transaction
This distributed transaction scheme , It is more suitable for single block applications , Distributed transactions across multiple libraries , And because it relies heavily on the database level to handle complex transactions , Efficiency is very low , Absolutely not suitable for high concurrency scenarios , If you want to play , Then based on Sprign+JTA You can do it
TCC programme
TCC The full name is :Try ,Confirm,Cancel
- try Stage : This stage is about checking the resources of various services and locking or reserving resources
- Confirm Stage : This stage is to perform actual operations in various services
- Cancel Stage : If the business method execution of any service is wrong , Then compensation is needed here , It is to perform the rollback operation of the successful business logic ( Roll back those successful execution )
This scheme is rarely used at present , But there are also scenarios for use , For example, it is generally related to money , Payment when dealing with money , Trading related scenarios , Will use TCC, Strictly guarantee the success of distributed transactions , Or roll back all automatically , Strictly guarantee the correctness of funds , Make sure there are no problems with the money .
Local message table
See below for the general meaning :
- A While the system operates in its own local transaction , Insert a piece of data into the message table
- next A The system sends this message to MQ In the middle
- B After the system receives the message , In a business , Insert a piece of data into your local message table , Perform other business operations at the same time , If the message has been processed , Then this transaction is rolled back , This ensures that messages are not processed repeatedly
- B After the successful execution of the system , It will update the status of its local message table and A Status of the system message table
- If B System processing failed , Then the message table status will not be updated , So at this time A The system will scan its message table regularly , If there is an unprocessed message , It will be sent again to MQ In the middle , Give Way B Deal with... Again
- This scheme guarantees final consistency , Even if the B The transaction failed , however A I'll keep sending messages back , until B Until we succeed there
The biggest problem with this scheme is that the Champions League relies heavily on the message table of the database to manage transactions , What if it is a high merger scene ? Difficult to expand , Therefore, it is generally less used
Reliable message final consistency scheme
This scheme is to use no message table , Based on the direct MQ To implement the transaction , Like Ali's RocketMQ Just support message transactions
The meaning is as follows :
- A The system sends a prepared The message to MQ, If this prepared If the message fails to be sent, cancel the operation directly and don't execute
- If this message has been sent successfully , Then we will execute the local transaction , Tell... If you succeed mq Send a confirmation message , If you fail, tell mq Rollback message
- If a confirmation message is sent , Then the system B You will receive a confirmation message , Then execute the local transaction
- mq Will automatically poll all prepared Message callback your interface , Ask you , Is this message a local transaction failure , So no confirmation message was sent , Continue to retry or rollback ? Generally speaking, you can check the database to see if the local transaction is executed before , If it rolls back , Then roll back here , This is to avoid the possibility that local transaction execution succeeds , And the confirmation message failed to be sent
- In this plan , If the system B The transaction failed ? Retry lo , Automatically retrying until successful , If it doesn't work , Or roll back the important capital business , such as B After the system locally rolls back , Find a way to inform the system A Also roll back , Or send alarm manually to rollback and compensate
- This plan is quite suitable , Or use it directly RocketMq Supported by , Or based on ActiveMQ perhaps RabbitMQ Encapsulate a set of such logic by yourself
Best effort notification scheme
The general meaning of this plan is as follows
- System A After the local transaction is completed , Send a message to MQ
- There will be a special consumption here MQ Try your best to inform the service , This service will consume MQ Then write it down in the database , Or put it into a memory queue , Then call the system B Have to interface
- If the system B If the implementation is successful OK 了 , If the system B Execution failed , Then the best effort notification service will periodically try to call the system again B, Over and over again N Time , In the end, if you can't, just give up
边栏推荐
- L2-007 family real estate (25 points)
- 4、安装部署Spark(Spark on Yarn模式)
- SQL time injection
- B tree and b+ tree of MySQL index implementation
- Detailed explanation of express framework
- FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
- [BSidesCF_2020]Had_ a_ bad_ day
- [Bluebridge cup 2020 preliminary] horizontal segmentation
- 分布式节点免密登录
- Word typesetting (subtotal)
猜你喜欢
Learn winpwn (2) -- GS protection from scratch
小L的试卷
Request object and response object analysis
Mtcnn face detection
Gallery之图片浏览、组件学习
Case analysis of data inconsistency caused by Pt OSC table change
double转int精度丢失问题
快来走进JVM吧
UDS learning notes on fault codes (0x19 and 0x14 services)
【CDH】CDH5.16 配置 yarn 任务集中分配设置不生效问题
随机推荐
{one week summary} take you into the ocean of JS knowledge
Integration test practice (1) theoretical basis
SQL时间注入
【Flink】CDH/CDP Flink on Yarn 日志配置
第4阶段 Mysql数据库
{一周总结}带你走进js知识的海洋
2020网鼎杯_朱雀组_Web_nmap
Antlr4 uses keywords as identifiers
JS array + array method reconstruction
Password free login of distributed nodes
L2-006 tree traversal (25 points)
L2-006 树的遍历 (25 分)
Word typesetting (subtotal)
[MRCTF2020]套娃
使用LinkedHashMap实现一个LRU算法的缓存
Stage 4 MySQL database
Pytorch基础
ES6 promise object
[蓝桥杯2021初赛] 砝码称重
MySQL and C language connection (vs2019 version)