In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Spring Boot + redis is an example analysis of solving the problem of oversold inventory in seconds. Aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Problem description
In many panic buying activities, under the limitation of the limited number of goods, how to ensure that the number of users snapping up goods can not be greater than the number of goods, that is, the problem of overselling can not occur; in addition, there will be a large number of user visits during panic buying. How to improve the effect of user experience is also a problem, that is, to solve the performance problem of the second kill system. This paper mainly introduces the realization of commodity second kill function based on redis. First of all, let's give you a general idea.
The general idea is to reduce the access to the database, cache the data into the Redis cache as much as possible, and get the data from the cache.
When the system is initialized, the inventory quantity of the goods is loaded into the Redis cache; when the second kill request is received, the inventory is pre-reduced in Redis, and when the stock in Redis is insufficient, the second kill fails directly, otherwise step 3 is continued; the request is put into the asynchronous queue and the return is queued The server asynchronous queue will request dequeuing. Successful dequeuing requests can generate a second kill order, reduce database inventory, and return second kill order details. When the backend order is created successfully, you can send a second kill success notification to the user through websocket. The front end uses this to determine whether the second kill is successful, and if the second kill is successful, enter the details of the second kill order, otherwise the second kill fails.
The following is the code directly to put the inventory of second-kill items into the redis cache when the system is initialized.
Second, create a message queue (here, for convenience, I directly use the redis queue for simulation)
Third configure RedisTemplate serialization
Let's create an interface in which 10000 threads are created to simulate the user's panic buying scenario.
Here, the decrement operation in redis api is used to reduce the number of panic purchases by users in advance, and at the same time determine whether the inventory in redis is greater than the number of panic purchases by users. If it is less than 0, directly prompt the user that the second kill fails, otherwise the second kill succeeds, and enter the redis message queue to perform the database inventory building operation. The above operations pay attention to ensure that the redis cache is consistent with the database inventory data.
The following test demonstration
Initialize the merchandise inventory 100, after testing 10,000 concurrent shipments, it is found that there will be no oversold problem. Because ten thousand concurrency here, each concurrency snapped up 10 items. After redis reduces inventory, there will be only 10 threads to update the database.
This is the answer to the sample analysis question of Spring Boot + redis to solve the problem of oversold inventory. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.