当前位置:网站首页>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 .
边栏推荐
- $refs:组件中获取元素对象或者子组件实例:
- Fast quantitative, abbkine protein quantitative kit BCA method is coming!
- Graduation design game mall
- Networkx drawing and common library function coordinate drawing
- Sqlserver multithreaded query problem
- Torefs API and toref API
- 弹性布局(二)
- Academic report series (VI) - autonomous driving on the journey to full autonomy
- Databinding exception of kotlin
- Modify the jupyter notebook file path
猜你喜欢

Release notes of JMeter version 5.5

About binary cannot express decimals accurately

The currently released SKU (sales specification) information contains words that are suspected to have nothing to do with baby

我理想的软件测试人员发展状态

How DHCP router works

How can brand e-commerce grow against the trend? See the future here!

非父子组件的通信

1090: integer power (multi instance test)

Introduction to abnova's in vitro mRNA transcription workflow and capping method

Detailed explanation of transform origin attribute
随机推荐
PostgreSQL source code (59) analysis of transaction ID allocation and overflow judgment methods
Precise space-time travel flow regulation system - ultra-high precision positioning system based on UWB
Outlier detection technology of time series data
Multithreading and high concurrency (9) -- other synchronization components of AQS (semaphore, reentrantreadwritelock, exchanger)
FPGA course: application scenario of jesd204b (dry goods sharing)
At the age of 20, I got the ByteDance offer on four sides, and I still can't believe it
考研失败,卷不进大厂,感觉没戏了
组件的通信
1090: integer power (multi instance test)
$parent(获取父组件) 和 $root(获取根组件)
Fullgc problem analysis and solution summary
MIPS uclibc cross compile ffmpeg, support g711a encoding and decoding
Config distributed configuration center
Procedure in PostgreSQL supports transaction syntax (instance & Analysis)
$refs: get the element object or sub component instance in the component:
Pass parent component to child component: props
Exception of DB2 getting table information: caused by: com ibm. db2.jcc. am. SqlException: [jcc][t4][1065][12306][4.25.13]
【JDBC以及内部类的讲解】
Differences between H5 architecture and native architecture
【mysqld】Can't create/write to file