当前位置:网站首页>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 .
边栏推荐
- How Oracle backs up indexes
- PostgreSQL source code (59) analysis of transaction ID allocation and overflow judgment methods
- 計算機服務中缺失MySQL服務
- JS small exercise
- readonly 只读
- Apache AB stress test
- 【mysqld】Can't create/write to file
- Basic process of network transmission using tcp/ip four layer model
- Introduction to abnova's in vitro mRNA transcription workflow and capping method
- Asynchronous components and suspend (in real development)
猜你喜欢
How to model and simulate the target robot [mathematical / control significance]
Bindingexception exception (error reporting) processing
MIPS uclibc cross compile ffmpeg, support g711a encoding and decoding
FPGA course: application scenario of jesd204b (dry goods sharing)
Pass child component to parent component
Leetcode t1165: log analysis
$refs: get the element object or sub component instance in the component:
LC interview question 02.07 Linked list intersection & lc142 Circular linked list II
transform-origin属性详解
Answer to the second stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
随机推荐
freeswitch拨打分机号源代码跟踪
AVL树的实现
[Luogu p1971] rabbit and egg game (bipartite game)
Sqlmap tutorial (IV) practical skills three: bypass the firewall
【JDBC以及内部类的讲解】
RuntimeError: CUDA error: CUBLAS_ STATUS_ ALLOC_ Failed when calling `cublascreate (handle) `problem solving
Abnova immunohistochemical service solution
OOM(内存溢出)造成原因及解决方案
1089: highest order of factorial
Composition API 前提
Flexible layout (II)
Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.9 introduction to network interface (IX) extending the request3 met
Nesting and splitting of components
Mobx knowledge point collection case (quick start)
main函数在import语句中的特殊行为
深度学习花书+机器学习西瓜书电子版我找到了
How DHCP router works
【mysqld】Can't create/write to file
Kuboard无法发送邮件和钉钉告警问题解决
Complete process of MySQL SQL