当前位置:网站首页>Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
2022-07-05 02:52:00 【End code life】
Click on “ End of life ”, Focus on , Official account
Daily technical dry goods , First time delivery !
In the process of commodity purchase , Inventory deduction process , The general operation is as follows :
select According to the goods id Query the inventory of goods .
According to the number of orders , Calculate whether the inventory is sufficient , If the inventory is insufficient, an exception of insufficient inventory will be thrown , If there is enough stock , Then subtract the deducted inventory to get the latest inventory residual value .
set Set the latest inventory remaining value .
The pseudo code of the above process is as follows :
// According to the goods id Get the remaining inventory of goods
select stock_remaing from stock_table where id=${goodsId};
// Operating inventory
// Compare inventory
if(stock_remaing <quantity){
// Throw the exception of insufficient inventory
}
else{
// Inventory value after deduction
int new_stock=stock_remaing - quantity;
}
// According to the goods id Set the calculated inventory
update stock_table set stock_remaing =${new_stock} id=${goodsId};
1、 Concurrent modification of database storage oversold
If the isolation level of database transactions is not serialization (serializable), According to the characteristics of transactions , During concurrent modification , Write overwrite may occur .
hypothesis , Remaining inventory of goods stock_remaing by 100, Customer A Place an order 20, Customer B Place an order 30, When deducting inventory concurrently , There may be oversold . If the customer A And the customer B At the same time, the remaining inventory is 100, The value submitted after the transaction will overwrite the value submitted by the previous customer , It is possible that the remaining inventory is 80 perhaps 70. The process is as follows :
2、 Lock update Repository
In order to control transactions , Prevent write overwrite , You'll think of using select for update The way , Lock the inventory of this commodity , Then do the rest .
The process is as follows :
above , Use pessimistic locking , In distributed services , If concurrency is high , Stock deduction is a serial operation , Efficiency is very low .
3、 Update with optimistic lock
At the time of the update , Use (CAS+ Version number update )+ Retry conditions ( Number of retries or retry time limit ) Update inventory in an optimistic way . here , If , Customer A And the customer B At the same time, the remaining inventory is read 100, At the time of the update , One operation will fail . The process is as follows :
This method can greatly improve the concurrency , It can also ensure the consistency of data ; It is controlled by the conditions of retry times and retry time , It can prevent the database pressure caused by too many retries .
4、 Can it be executed by direct decrement ?
When deducting inventory , Some people propose not to implement select, Calculation ,set Three stage operation , The way of direct deduction , And make a judgment when the deduction is less than zero . The pseudocode is as follows :
update stock_table set remaing_stock=remaing_stock-${quantity}
where id = goods id
and remaing_stock>${quantity};
In a distributed service call , Because the network is abnormal , Get server exception , Maybe when the microservice is invoked , There is a service retry . for example , Gateway timeout for scenario , Service retry mechanism . here , This method does not satisfy idempotency , And there are many deductions . for example , When the same user deducts inventory , Service retry , In extreme cases , The user has performed inventory deduction for multiple times , Then there is oversold .
5、 have access to redis Do you want to deduct inventory ?
Because I haven't studied redis Source code , For this way, I refer to Daniel's reply , The answer is that you can use redis Transaction deduction balance of , But in CAS Mechanism ratio mysql No advantage , High performance is due to its memory storage , The side effect is the risk of data loss .
PS: Prevent missing this article , You can like it , It's easy to browse and search
边栏推荐
- Design and implementation of kindergarten management system
- [Yu Yue education] National Open University spring 2019 0505-22t basic nursing reference questions
- Bumblebee: build, deliver, and run ebpf programs smoothly like silk
- [daily problem insight] Li Kou - the 280th weekly match (I really didn't know it could be so simple to solve other people's problems)
- openresty ngx_lua执行阶段
- 【LeetCode】404. Sum of left leaves (2 brushes of wrong questions)
- Good documentation
- ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
- LeetCode --- 1071. Great common divisor of strings problem solving Report
- Master Fur
猜你喜欢
【LeetCode】111. Minimum depth of binary tree (2 brushes of wrong questions)
Design and implementation of kindergarten management system
【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Acwing game 58 [End]
Asynchronous and promise
Azkaban安装部署
Azkaban实战
问题解决:AttributeError: ‘NoneType‘ object has no attribute ‘append‘
随机推荐
Openresty ngx Lua Execution stage
Idea inheritance relationship
Three line by line explanations of the source code of anchor free series network yolox (a total of ten articles, which are guaranteed to be explained line by line. After reading it, you can change the
Flume配置4——自定义MYSQLSource
Hmi-30- [motion mode] the module on the right side of the instrument starts to write
D3js notes
Pat class a 1162 postfix expression
Bumblebee: build, deliver, and run ebpf programs smoothly like silk
Leetcode takes out the least number of magic beans
Avoid material "minefields"! Play with super high conversion rate
Simple use of devtools
2021 Li Hongyi machine learning (1): basic concepts
Good documentation
8. Commodity management - commodity classification
GFS distributed file system
Design and implementation of campus epidemic prevention and control system based on SSM
单项框 复选框
GFS分布式文件系统
Anchor free series network yolox source code line by line explanation Part 2 (a total of 10, ensure to explain line by line, after reading, you can change the network at will, not just as a participan
Usage scenarios and solutions of ledger sharing