当前位置:网站首页>Explain the idempotence of distributed system in detail
Explain the idempotence of distributed system in detail
2022-07-27 23:42:00 【YAN_ RONG_ TECHNOLOGY】
Idempotent means that one and more requests for a resource should have the same effect .
What is distributed idempotent
First , Let's imagine the following scenarios :
Scene one : stay App When you confirm your order , Click many times and no response , You can only click several times . under these circumstances , If the idempotence of the interface cannot be guaranteed , Then there will be the problem of repeated orders .
Scene two : When receiving messages , Message push repeat . If the interface that processes the message cannot guarantee idempotence , Then the news of repeated consumption may have a great impact .
Usually , For high availability 、 High performance and high scalability , The distributed system has many service nodes , But it is easy to lead to long and complex service call chain , The network communication complexity between service nodes increases exponentially , A slight network failure may cause the whole service to be abnormal .
meanwhile , Considering the possibility of hardware equipment failure , So distributed systems face a common problem : A message ( Mission ) May be consumed repeatedly . therefore , How to ensure that single consumption and repeated consumption of the same message have the same effect , That is, idempotency of messages has become a hot issue .
Other common distributed solutions
Add a unique index to the database , For example, the order number is the unique index , Prevent duplicate production orders ;
Using distributed locks , Prevent concurrent operations in applications ;
use Token Mechanism , Effectively prevent repeated submissions . Submit background tape Token value , It needs to be judged first Token Whether there is , If it exists , Delete Token, Execute business logic ;
Database through optimistic lock ;(update table_name set version=version+1 where version=0)
Database pessimistic lock , It is usually implemented with transactions through primary keys or unique indexes .
These are other common idempotent solutions , Different methods are often used in different business scenarios . Because our main business is distributed storage ,IO from Client To Server The link of is not very long , So we refer to it Token Mechanism , A set of “ new ” Mechanism to deal with idempotent problems in business scenarios .
Message processing in different states

As shown in the figure , This is a simple task system ,Server Successfully executed ① The command sent , But no corresponding result was received . This is because the storage with network factors encounters problems ,Client You need to retry the task , To eliminate the impact of network instability , But in different cases ,Server When a retry message is received , Different solutions are needed . At this time , There are three possible situations :
Case one ,Server No corresponding message was received , Because there is network instability ,Client You need to try again , At this time Server The message will be received again and processed , After normal processing, the corresponding result is returned .
The second case ,Server Received the corresponding message , Still processing , Due to other factors, the task execution time is too long . This is because the task is still being carried out , So in order to guarantee idempotence , We need to wait until the previous task is completed , Then get the execution result and return it to the client .
The third case ,Server Received the corresponding message , And it has been processed , But network factors lead to Client Have not received ② Result ,Server After receiving the retry request , Get the received result directly and return it to Client.
In order to achieve the above functions , We need a reasonable mechanism to save the received message status , And you need to be able to judge that the message has been Client Clear this state when the result is received , Here we introduce SeqNumber To do these things .
SeqNumber The mechanism of
Every time we request, we will carry a request serial number that has been completed before ACK And current serial number Seq,Server When processing, it will be based on Seq To determine the status of the current message , To ensure a Seq Will not be executed many times . meanwhile , Will release ACK The corresponding serial number , Because only at this time can we guarantee Client The corresponding request has been completed correctly .

We are Client There are two queues at the end finished and running, There are tasks being performed and completed ID. stay Server There's one side map, All uncleaned messages are saved ( Include Being implemented , Execution completed ).
We have the following steps for a request :
Generate an unused Seq, Pictured here request Generated in 12;
Put this Seq Insert running queue ;
From what has been done finished The queue gets the smallest Seq As ACK, Pictured ACK=1, Come here Client Send the prepared message to Server;
Server Receive a request , First of all, I will ACK The cache of the corresponding request is released , Then get the corresponding Seq The news of , If not, it means that it is new news , Directly inserted into the , Otherwise, according to map Do the corresponding processing for the state in ( The processing method corresponding to the above three states );
After finishing the task , Return the result to Client, Come here Server Task processing has been completed ;
Client Received the result of the message , Can confirm Server The task has been completed , At this time release finished In line ACK=1 The item , Then what will just be finished is Seq=12 The task of moves to finished In .
summary
Distributed system is a very large and complex system , Idempotence is only a very important and complex problem , In the construction of distributed system , Different systems have different requirements for idempotence , I hope this article can help you .
边栏推荐
- 重新定义分析 - EventBridge 实时事件分析平台发布
- Spark 离线开发框架设计与实现
- 并发和并行有什么区别?
- Normality of confidence interval
- Redis 哈希Hash底层数据结构
- 【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
- 他山之石 | 蚂蚁超大规模知识图谱构建及应用
- 初步了解Panda3D音频和高级交互组件
- Application of user portrait in precise push of wechat official account of scientific journals
- ZCMU--1720: 死亡如风,我要装逼
猜你喜欢

远程调试 idea配置remote debug、在远程服务器的程序中,添加JVM启动参数-Xdebug

Find and leverage xxE – XML external entity injection

After returning to mixlab for three days, "creative team" cured my spiritual internal friction

习题 --- BFS
![[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code](/img/a7/8f0d929457d285adc8020c7f5d33ea.png)
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code

Exercise --- BFS

低代码开发前景如何,大家都真的看好低代码开发吗?

一位软件投资者的独白:我为什么不追逐快速增长的公司

强化学习——PyTorch 实现 Advantage Actor-Critic (A2C)

详解分布式系统的幂等
随机推荐
RPA流程自动化机器人是什么技术?如何实现办公自动化?
股价暴涨180.46%!国产大硅片龙头沪硅产业上市:近4年净利累计不足6000万
Redis 哈希Hash底层数据结构
加速IGBT国产化!比亚迪半导体将独立上市,市值或达300亿元!
【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析
2022夏暑假每日一题(五)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: un
What is the MySQL data storage method?
MySQL data query (where)
Tita 的OKR系统与其他同类型产品,或者是共享文档等相比,有什么优势?
怎么使用C# Winform实现复制文件显示进度
Accelerate IGBT localization! BYD semiconductor will be listed independently, with a market value of 30billion yuan!
Your list is too laggy? These four optimizations can make your list silky smooth
重新定义分析 - EventBridge 实时事件分析平台发布
疫情之下,台积电一季度增长超预期,7nm占比35%!二季度或创新高
proteus仿真arduino中调用DHT11/22温湿度传感器
Normality of confidence interval
Character stream learning 14.3
What is the difference between concurrency and parallelism?
Introduction to several common usage scenarios of message queue