当前位置:网站首页>Idempotency practice operation, explaining idempotency based on business
Idempotency practice operation, explaining idempotency based on business
2022-04-23 03:17:00 【Xiaodaoxian 97】
The question about idempotence was also mentioned in the previous interview , I checked Baidu about its concept, but it's very simple , But I don't quite understand , Discuss with someone today , Then I understood it in combination with reality .
-
Idempotent concept comes from mathematics , Express N Secondary transformation and 1 The result of secondary transformation is the same . Here we discuss in some scenarios , When the client invokes the service and does not achieve the expected result , Multiple calls will be made , To avoid side effects on service resources caused by repeated calls , The service provider will promise to meet idempotent .
-
HTTP/1.1 The definition of idempotence in is : One and more requests for a resource should have the same side effects on the resource itself ( Except for network timeout and other problems ). in other words , The impact of any multiple execution on the resource itself is the same as that of one execution .
-
In a nutshell : Call the same interface multiple times , The result returned is the same .
In fact, we may have done it in the actual code idempotent But because we don't know this concept, we don't know ( In fact, there are many such situations )
You may not understand the concept alone , Here are two examples , One is what my colleague told me , One is my previous practical example .
One 、
There is an order receiving operation in the work order system , One work order can be distributed to multiple people , Everyone can take orders , The rule is whoever receives it first .
If we don't Idempotent processing Then it will happen that the second person covers the first person's order .
The error operation is as follows :
Assign work order to A、B Two people , then A、B At the same time, enter the order receiving interface , Now A The order is accepted , however B There's a call , He went to answer the phone , After answering the phone, he still stayed in the order receiving interface , Then he took the order .
The transformation is as follows :
- Get... Every time you come in Redis Inside key, Judge whether it exists , If it doesn't exist, go on
- Judge the status of the current work order , Is it equal to To be received , If it's going down
- Use work order id As key Deposit in Redis Go inside ( Distributed lock implementation )
- Execute a series of business logic
- Delete Redis Inside key
Pseudo code implementation :
public String fun(String id) {
if (RedisUtils.get(id) != null) {
return " The work order has been received ";
}
RedisUtils.put(id,id);
if (testDao.getStateById(id) != 3) {
return " The work order has been received ";
}
// Execute specific business logic
//......
if (RedisUtils.get(id) != null) {
RedisUtils.delete(id);
}
return " Successful operation ";
}
Two 、
This is a typical payment scenario : User payment and order scenario , Theoretically, an order user can only pay once , But if you don't do idempotent processing, you may pay multiple times .
The error operation is as follows :
Stuck because of the network or various reasons , The user refreshes the page and clicks pay again , This will result in two payments
The transformation is as follows :
- Lock the button every time you click pay .( But refreshing the page will cause unlocking )
- Return to the front end after payment , The front end jumps to the payment success page .
- Get... Every time you come in Redis Inside key, Judge whether it exists , If it doesn't exist, go on
- Judge the status of the current order , Is it equal to To be paid , If it's going down
- Use order id As key Deposit in Redis Go inside ( Distributed lock implementation )
- Execute a series of business logic
- Delete Redis Inside key
This is the same as the above code implementation, so it is no longer written .
版权声明
本文为[Xiaodaoxian 97]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230315096248.html
边栏推荐
- Experiment 6 input / output stream
- “如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
- xutils3修改了我提报的一个bug,开心
- The website JS in. Net core cefsharp chromium WebBrowser calls the C method in winfrom program
- 2022年做跨境电商五大技巧小分享
- 类似Jira的十大项目管理软件
- [mock data] fastmock dynamically returns the mock content according to the incoming parameters
- JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
- 搭建XAMPP时mysql端口被占用
- MySql分组查询规则
猜你喜欢
IDEA查看历史记录【文件历史和项目历史】
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
Tencent video price rise: earn more than 7.4 billion a year! Pay attention to me to receive Tencent VIP members, and the weekly card is as low as 7 yuan
2022A特种设备相关管理(电梯)上岗证题库及模拟考试
Use of slice grammar sugar in C #
一套组合拳,打造一款 IDEA 护眼方案
软件测试相关知识~
MySql关键字GROUP_CONCAT,组合连接查询
PID debugging of coding motor (speed loop | position loop | follow)
超好用的【通用Excel导入功能】
随机推荐
IDEA查看历史记录【文件历史和项目历史】
C语言实现通讯录----(静态版本)
《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案
Improvement of ref and struct in C 11
The most easy to understand service container and scope of dependency injection
12.<tag-链表和常考点综合>-lt.234-回文链表
Preview of converting doc and PDF to SWF file
JS implementation of new
It can receive multiple data type parameters - variable parameters
Student achievement management
Top ten project management software similar to JIRA
Quartz. Www. 18fu Used in net core
oracle 查询外键含有逗号分隔的数据
[Mysql] LEFT函数 | RIGHT函数
Fiddler use
关于idea调试模式下启动特别慢的优化
A comprehensive understanding of static code analysis
OLED multi-level menu record
Flink real-time data warehouse project - Design and implementation of DWS layer
2022A特种设备相关管理(电梯)上岗证题库及模拟考试