当前位置:网站首页>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 .
边栏推荐
- main函数在import语句中的特殊行为
- . Net 5 fluentftp connection FTP failure problem: this operation is only allowed using a successfully authenticated context
- 95后CV工程师晒出工资单,狠补了这个,真香...
- Leetcode t1165: log analysis
- Role of virtual machine
- How DHCP router works
- Graduation design game mall
- Select the product attribute pop-up box to pop up the animation effect from the bottom
- Multithreading and high concurrency (9) -- other synchronization components of AQS (semaphore, reentrantreadwritelock, exchanger)
- Stockage et pratique des données en langage C (haut niveau)
猜你喜欢
记一个并发规则验证实现
Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp
mips uclibc 交叉编译ffmpeg,支持 G711A 编解码
Advanced level of C language (high level) pointer
Fast quantitative, abbkine protein quantitative kit BCA method is coming!
. Net 5 fluentftp connection FTP failure problem: this operation is only allowed using a successfully authenticated context
Outlier detection technology of time series data
我理想的软件测试人员发展状态
Esxi attaching mobile (Mechanical) hard disk detailed tutorial
Kuboard无法发送邮件和钉钉告警问题解决
随机推荐
Four goals for the construction of intelligent safety risk management and control platform for hazardous chemical enterprises in Chemical Industry Park
Tool class: object to map hump to underline underline hump
身边35岁程序员如何建立起技术护城河?
Leetcode t1165: log analysis
transform-origin属性详解
点亮显示屏的几个重要步骤
Kuboard无法发送邮件和钉钉告警问题解决
LC interview question 02.07 Linked list intersection & lc142 Circular linked list II
Non empty verification of collection in SQL
异步组件和Suspense(真实开发中)
How DHCP router works
計算機服務中缺失MySQL服務
main函数在import语句中的特殊行为
freeswitch拨打分机号源代码跟踪
MIPS uclibc cross compile ffmpeg, support g711a encoding and decoding
FPGA course: application scenario of jesd204b (dry goods sharing)
PostgreSQL source code (60) transaction system summary
Détailler le bleu dans les tâches de traduction automatique
Kuboard can't send email and nail alarm problem is solved
$refs:组件中获取元素对象或者子组件实例: