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

How to add and unlock redis

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article uses easy-to-understand examples to introduce how to lock and unlock redis. The code is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Redis does not have a locking mechanism, and there is no competition for multiple user connections.

However, there may be errors such as connection timeout, connection blocking, or connection closure during concurrency.

Locking can generally be achieved by pooling the connection on the client side (such as using synchronized to add internal locks when reading and writing redis), or by using the transaction command setnx that comes with redis on the server side.

In e-commerce activities, there is often a "second kill" rush purchase activity, in this scenario, the server is usually faced with high concurrency request processing.

In other words, at the same time, there will be a large number of concurrent users to buy a product at the same time, at this time, it is necessary to ensure that there will not be oversale (the number of goods purchased by the user exceeds the actual number of goods).

Here, we need to apply the locking and unlocking features in redis to ensure that only one user is engaged in each shopping operation, so as to avoid the occurrence of dirty data caused by competition.

Next, let's show you how to use locking and unlocking correctly.

Setnx

Redis officially recommends using the set command for locking operations, as follows:

If ($redis- > set ('my:lock', 1, [' NX'])) {# todo $redis- > del ('my:lock');}

Among them

NX-indicates that it is set only if key does not exist.

There is a problem with this method. If a client crashes after acquiring the lock or keeps occupying the lock without releasing it, it will lead to a deadlock and make it impossible for subsequent users to acquire the lock for operation. So this operation needs to set a timeout.

Improvement of setnx

For the problem with the above method, we use the expire method to set the timeout. But does it solve the problem here?

No! Because the expire is not an atomic operation, if the client crashes after the setnx operation, the timeout will not be set successfully, which also makes the locking operation face the above problems.

If ($redis- > set ('my:lock', 1, [' NX'])) {$redis- > expire ('my:lock', 10); # todo $redis- > del (' my:lock');} this is the end of the method of locking and unlocking redis. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

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

Database

Wechat

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

12
Report