当前位置:网站首页>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-06 04:17:00 【End code life】
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.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
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};
- 1.
- 2.
- 3.
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 .
边栏推荐
- Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
- P2648 make money
- 关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
- How many of the 10 most common examples of istio traffic management do you know?
- BOM - location, history, pop-up box, timing
- 【leetcode】22. bracket-generating
- One question per day (Mathematics)
- Global and Chinese markets for medical gas manifolds 2022-2028: Research Report on technology, participants, trends, market size and share
- Interface idempotency
- TCP/IP协议里面的网关地址和ip地址有什么区别?
猜你喜欢
电脑钉钉怎么调整声音
记一次excel XXE漏洞
Le compte racine de la base de données MySQL ne peut pas se connecter à distance à la solution
Slow SQL fetching and analysis of MySQL database
Overturn your cognition? The nature of get and post requests
MySQL master-slave replication
Web components series (VII) -- life cycle of custom components
About some basic DP -- those things about coins (the basic introduction of DP)
Ipv4中的A 、B、C类网络及子网掩码
DM8 backup set deletion
随机推荐
Global and Chinese market of rubber wheel wedges 2022-2028: Research Report on technology, participants, trends, market size and share
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
拉格朗日插值法
VPP性能测试
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
电脑钉钉怎么调整声音
2328. 网格图中递增路径的数目(记忆化搜索)
Explain in simple terms node template parsing error escape is not a function
Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
2/12 didn't learn anything
About some basic DP -- those things about coins (the basic introduction of DP)
Detailed explanation of serialization and deserialization
2327. 知道秘密的人数(递推)
Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control
Cross domain and jsonp details
Mixed development of QML and QWidget (preliminary exploration)
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
How does technology have the ability to solve problems perfectly
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
729. 我的日程安排表 I(set or 动态开点线段树)