当前位置:网站首页>Distributed transactions: the final consistency scheme of reliable messages
Distributed transactions: the final consistency scheme of reliable messages
2022-07-02 06:26:00 【Ah size】
We must be familiar with business , For example, it is often mentioned ACID, But for the content of subsequent distributed transactions , Let's talk first ACID, Then I will introduce what is distributed transaction , Finally, we will focus on the distributed transaction solution based on reliable messages .
What is business
Strictly speaking, transactions should be atomic 、 Uniformity 、 Isolation and persistence , abbreviation ACID.
- Atomicity (Atomicity), It can be understood that all operations within a transaction are executed , Either not .
- Uniformity (Consistency), It can be understood that the data satisfies the integrity constraints , That is, there will be no intermediate state data , For example, your wallet has 100, I have... In my wallet 100, You call me 50 block , The money in your wallet should be 50, The money in my wallet should be 150, It won't exist. I'll add my money , Your middle state without deduction .
- Isolation, (Isolation), It means that multiple transactions will not interfere with each other when they are executed simultaneously , In other words, the data in one transaction is isolated from other transactions .
- persistence (Durability), It means that after a transaction is completed, the data is saved forever , Any subsequent operations or failures will not affect the outcome of the transaction .
In the popular sense, transaction is to make some update operations successful , Or they all failed .
What is distributed transaction
As the name implies, distributed transaction is to implement transaction in distributed system , It is actually a combination of multiple local transactions .
A large operation consists of different small operations , These small operations are distributed on different servers , Distributed transactions need to ensure that these small operations are all successful , All or nothing . essentially , Distributed transaction is to ensure the data consistency of different databases .
There are several common solutions for distributed transactions :2PC,3PC,TCC, Local message table 、 The final consistency of reliable information 、 Try your best to inform, etc
Today we will focus on the solution to the final consistency of reliable messages
What is the ultimate consistency scheme for reliable messages
The final consistency scheme of reliable messages refers to sending messages to the message middleware after the transaction initiator completes the local transaction , Transaction participants ( Message consumer ) You must be able to receive the message and process the transaction successfully , This scheme emphasizes that as long as the message is sent to the transaction participant , Then the final transaction must be consistent .
What are the problems with this approach ?
This scheme is implemented through message oriented middleware , Transaction initiator ( Message producer ) Send messages to message middleware , Transaction participants receive messages from message middleware , Due to the uncertainty of network communication, it will lead to distributed transaction problems , Here's the picture :

- Local transactions and atomicity of messages

As shown in the figure above, it is in the dotted box , There are several situations :
1) Local transaction commit failed , The message is not sent .
2) Local transaction succeeded , Message delivery failed , Local transaction rollback .
3) Local message succeeded , Message timeout , Local transaction rollback , The message eventually failed .
4) Local message succeeded , Message timeout , Local transaction rollback , The message finally succeeded .
in summary , There is a fourth case , Cause local transactions , Inconsistent with the transaction of the message party .
- Reliability of message received by transaction participants .
Message oriented middleware and transaction participants should ensure that messages can be successfully consumed .
- Message re consumption
Note the idempotency of the interface of the transaction participants , Message participants may have successfully consumed , Due to network problems, message middleware thinks that messages are not consumed , Problems after initiating retry .
Solution
- Local message table
The key to the local message table is that there is a local record table for storing message logs , You need to start a scheduled task to constantly scan message logs , Ensure that the message can be sent . The specific process is shown in the following figure :

The flow chart above :
1) The local transaction of the transaction initiator was executed successfully , Record the message log in the local message table .
2) Start timing task , Loop scan local message table .
3) When the scheduled task scans the message, it sends the message to the message middleware .
4) The message middleware receives a message , The success return message sends a success notification to the transaction initiator .
5) If the transaction initiator receives the message and sends it successfully, the log message will be deleted .
6) Transaction participants subscribe to messages , News consumption .
7) Transaction participants handle local transactions .
8) Local transaction successfully , Send successfully ack To message middleware .
Something to watch out for :
Transaction participants guarantee the idempotency of the interface .
- RocketMq Transaction message scheme
Apache RocketMQ 4.3 Later versions officially support transaction messages , It provides convenient support for distributed transaction implementation . stay RocketMQ 4.3 After the implementation of the complete transaction message , In fact, it is an encapsulation of the local message table , Moved the local message table to MQ Inside , solve Producer The atomicity of message sending and local transaction execution at the end .

Implementation process :
1) The transaction initiator sends Half Transaction message
2)RocketMq reply Half Send successfully
3) The transaction initiator executes a local transaction
4) The transaction initiator successfully executed the local transaction , send out commit To RocketMq,mq Post messages to transaction participants ; Transaction initiator failed to execute local transaction , send out rollback To RocketMq,mq removal message .
5) When RocketMq No confirmation from the transaction initiator has been received within a certain period of time , The transaction initiator will be checked back .
6) The transaction initiator queries the local transaction status .
7) The transaction initiator sends a message according to the queried transaction status commint/rollback To RocketMq.
8) When RocketMq launch commit after , Failed to receive or failed to receive within a certain period of time ack, A retry will be initiated .
advantage :
Message data is stored independently , Reduce the coupling between business system and message system .
Throughput is better than the local message table scheme .
shortcoming :
Two network requests are needed to send a message (half news + commit/rollback).
You need to implement the message retrieval interface .
In fact, every distributed transaction solution has its advantages and disadvantages , We need to weigh the pros and cons , Choosing the most appropriate business scenario is the king !
Okay . That's all for today , I will continue to share what I have learned and thought , I hope we are on the road to success together !
边栏推荐
- 底层机制Mvcc
- Code skills - Controller Parameter annotation @requestparam
- LeetCode 27. 移除元素
- Reading classic literature -- Suma++
- Introduce two automatic code generators to help improve work efficiency
- Sparse array (nonlinear structure)
- 日志 - 7 - 记录一次丢失文件(A4纸)的重大失误
- 标签属性disabled selected checked等布尔类型赋值不生效?
- Sentinel规则持久化到Nacos
- Generic classes and parameterized classes of SystemVerilog
猜你喜欢

I/o impressions from readers | prize collection winners list

VRRP之监视上行链路

LeetCode 90. 子集 II

Redis - big key problem

加密压缩文件解密技巧

Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table

Current situation analysis of Devops and noops

The difference between session and cookies

深入学习JVM底层(三):垃圾回收器与内存分配策略

Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
随机推荐
Network related knowledge (Hardware Engineer)
LeetCode 27. Removing Elements
Support new and old imperial CMS collection and warehousing tutorials
Redis——热点key问题
重载全局和成员new/delete
日志(常用的日志框架)
递归(迷宫问题、8皇后问题)
一起学习SQL中各种join以及它们的区别
Golang--map扩容机制(含源码)
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
LeetCode 90. Subset II
Community theory | kotlin flow's principle and design philosophy
Singleton mode compilation
Log (common log framework)
Linked list (linear structure)
Zhuanzhuanben - LAN construction - Notes
稀疏数组(非线性结构)
LeetCode 39. 组合总和
In depth understanding of JUC concurrency (II) concurrency theory
LeetCode 47. Full arrangement II