Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ideas to prevent goods from being oversold

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what are the ideas to prevent overselling". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, how to prevent overselling

When writing logic to prevent overselling, there is no problem with the idea of locking, but what kind of lock to add, which part of the logic to lock becomes a problem.

1. Idea 1

Jvm provides synchronized and reentrantlock.

Are these two locks suitable for inventory reduction?

In theory, it can be used, but the service must be deployed stand-alone. If it is multiple servers, it will become the following scenario, where the lock does not work at all.

3. Idea 3

I have seen someone enumerate the previous two implementation methods on the Internet. Here, stand-alone locks and distributed locks are not recommended!

In fact, the ultimate goal of preventing overselling is to prevent database inventory (goods_num) from being less than 0. The reason why it is less than 0 is that multiple threads calculate inventory in the program and then assign values to the database. So many locks to solve the problem, in fact, a sql can be achieved.

Update t_goods set goods_num=goods_num-1 where goods_id=1 and goods_num > 0

As shown above. For example, one item with id 1 has been sold. At this time, the inventory is reduced by one, and the key point is that goods_num > 0 is judged in the where condition. This indirectly limits that the sql will be reduced by one only if the existence of the library is greater than 1. Directly prevent the phenomenon of overselling.

In fact, someone should be angry at this time. This is an e-commerce scene, and there is a lot of pressure to connect directly to the database. In fact, at this time, it is time to make friendly current restrictions before reducing inventory.

Redis provides several commands:

Incr-- plus

Decr-- minus

Incrby-- ladder addition

Decrby-- ladder subtraction

These are all atomic operations and will return results after successful execution. For example:

Redis > SET failure_times 10OKredis > DECR failure_times (integer) 9

In this way, if there is too much pressure on the database to reduce inventory, you can make a double judgment. Before the goods are sold, redis caches the inventory of goods, first reduce the redis inventory through DECR, and then reduce the database inventory. When the redis inventory is already zero, there is no need to reduce the database data.

This is the end of the content of "what are the ideas for preventing goods from being oversold". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report