Home  >  Article  >  Database  >  Will redis be locked when saving data?

Will redis be locked when saving data?

(*-*)浩
(*-*)浩Original
2019-11-23 10:02:533864browse

Will redis be locked when saving data?

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

However, errors such as connection timeout, connection being blocked, or connection being closed may occur during concurrency. (Recommended learning: Redis video tutorial)

Generally, you can pool the connection on the client side (such as using synchronized and adding internal locks when reading and writing redis). Or use the transaction processing command setnx that comes with redis on the server side to implement the lock.

In e-commerce activities, there are often "flash sale" sales activities. In this scenario, the server usually faces high concurrent request processing.

That is to say, at the same time, there will be a large number of concurrent users purchasing a certain product at the same time. At this time, it is necessary to ensure that there will not be over-selling (the number of products ultimately purchased by the user exceeds the actual number) Number of Products).

Here we need to apply the locking and unlocking features in redis to ensure that only one user is performing each shopping operation to avoid competition leading to dirty data.

Next, we will introduce how to use locking and unlocking correctly.

setnx

redis official recommends using the set command for lock operations. The usage method is as follows:

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

NX — Indicates that the key is set only when the key does not exist.

There is a problem with this method. If a client crashes after acquiring the lock or keeps occupying the lock and does not release it, It will lead to deadlock, making it impossible for subsequent users to obtain the lock for operations. So this operation needs to set a timeout.

Improvements of setnx

In response to the problems of the above method, we use the expire method to set the timeout. But is the problem solved here?

No! Because expire is not an atomic operation here, if the client crashes after operating setnx, the timeout will not be successfully set at this time, which will also cause the locking operation to face the above problems.

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

For more Redis-related technical articles, please visit the Redis database usage tutorial column to learn!

The above is the detailed content of Will redis be locked when saving data?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn