当前位置:网站首页>How to reduce inventory with high concurrency on the Internet
How to reduce inventory with high concurrency on the Internet
2022-07-07 07:23:00 【Programmer base camp】
The inventory quantity model used in this paper
This article is for the convenience of description , We use a simplified inventory quantity model , The actual inventory quantity items can be designed according to actual needs .
Inventory quantity table -stockNum
Field name | English name | Field type |
Commodity logo | skuId | Long integer |
Inventory quantity | num | Integers |
The traditional database guarantees that it will not oversold
adopt Sql Judge whether the remaining inventory is enough , Multiple concurrent executions update Only one statement can execute successfully .
Update stockNum set num=num- Order quantity where skuId= goods ID and num- Order quantity >0
This method is not the focus of this article , Use the database to make quantity deduction , In the face of high concurrency, there is no way to resist , Let's next design a way to use Redis Cache the scheme for inventory deduction .
PS: The high concurrency of inventory deduction cannot be fundamentally solved by dividing the warehouse and table , Because the sub warehouse and sub table are based on commodities , High concurrency during promotion is aimed at a small number of goods , Even a commodity , Finally, the concurrent traffic will be sent to a few tables , Sub database and sub table will not work , You can only improve the concurrency of a single partition .
Redis The principle of deduction
Inventory deduction actually includes two parts , Part I oversold verification , The second part is the final deduction of inventory ; In traditional database deduction , The two were completed together , At the same time, oversold verification has been completed (where The judgment in the book ) And deductions (update Number ); If we want to solve these two parts , We need to solve the two problems separately :
The first level is to solve the oversold inspection : We can put the data into Redis in , Every time you deduct inventory , All the Redis The data in incryby Deduction , If the number returned is greater than 0, Indicates that the inventory is sufficient , because Redis A single thread , You can trust the returned results . The first level is Redis, It can resist high concurrency , performance Ok. After the oversold verification is passed , Enter the second level .
The second step is to reduce inventory : After the first level , The second level does not need to judge whether the quantity is enough , You just need a fool to deduct inventory , Execute the following statement on the database :
Update stockNum set num=num- Order quantity where skuId= goods ID
Here comes the question , Here, because the database is divided into warehouses and tables by commodities , The concurrency has not decreased , Isn't the problem unsolved ? Yes , There is also a need to introduce a heavyweight weapon , Task library , We do not directly execute the above deduction statement on the business database , We only record this deduction task in the task library , Then deduct asynchronously . The task library uses the order number or other fields that can be hashed to divide the warehouse and table , In this way, different orders for the same commodity will be hashed in different inventories of the task library , Although it's still database tolerance , But database hotspots have been eliminated .
The overall structure is as follows :

Quantity verification and data persistence are separated ,Redis The cache is responsible for verifying the quantity , Asynchronous tasks are responsible for data persistence .
In subsequent articles , I will introduce the design of task library , The task library can not only do the final consistency of data , It can also be used as a state machine engine to support the state machine , Serve the state machine business .
边栏推荐
- Tool class: object to map hump to underline underline hump
- 抽絲剝繭C語言(高階)數據的儲存+練習
- Reflection (II)
- 1090: integer power (multi instance test)
- AVL树的实现
- Kuboard can't send email and nail alarm problem is solved
- 面试官:你都了解哪些开发模型?
- Explain Bleu in machine translation task in detail
- Introduction to abnova's in vitro mRNA transcription workflow and capping method
- At the age of 20, I got the ByteDance offer on four sides, and I still can't believe it
猜你喜欢

Freeswitch dials extension number source code tracking

sql中对集合进行非空校验

Pass parent component to child component: props

Flexible layout (II)

身边35岁程序员如何建立起技术护城河?

Academic report series (VI) - autonomous driving on the journey to full autonomy

計算機服務中缺失MySQL服務

非父子组件的通信

Communication between non parent and child components

Abnova immunohistochemical service solution
随机推荐
MySQL service is missing from computer service
2018 Jiangsu Vocational College skills competition vocational group "information security management and evaluation" competition assignment
抽丝剥茧C语言(高阶)指针进阶练习
How DHCP router works
异步组件和Suspense(真实开发中)
Lvs+kept (DR mode) learning notes
Project practice five fitting straight lines to obtain the center line
Fast quantitative, abbkine protein quantitative kit BCA method is coming!
MIPS uclibc cross compile ffmpeg, support g711a encoding and decoding
At the age of 20, I got the ByteDance offer on four sides, and I still can't believe it
Networkx drawing and common library function coordinate drawing
Tool class: object to map hump to underline underline hump
Tumor immunotherapy research prosci Lag3 antibody solution
Complete process of MySQL SQL
JS small exercise
Sword finger offer high quality code
Leetcode t1165: log analysis
sql中对集合进行非空校验
transform-origin属性详解
Lm11 reconstruction of K-line and construction of timing trading strategy