Explain in detail how to use redis setnx command to realize distributed lock

Distributed locks can be implemented by using the setnx command of redis. The implementation method is described below.

Introduction to setnx command

Command format

Set the value of the key to value if and only if the key does not exist.

If the given key already exists, setnx will not do anything.

Setnx is short for set if not exists.

Return value

Returns an integer, specifically

-1. When the value of key is set

-0, when the value of key is not set

example

Implementing distributed locks using setnx

Multiple processes execute the following redis commands:

If setnx returns 1, it indicates that the process has obtained the lock, and setnx will press the key lock The value of foo is set to the timeout of the lock (current time + effective time of the lock).

If setnx returns 0, it indicates that other processes have obtained locks and cannot enter the critical area. The process can continuously try the setnx operation in a loop to obtain the lock.

Resolve deadlock

Consider a case where a process disconnects from redis after obtaining a lock (the process may hang up or the network may be interrupted). If there is no effective lock release mechanism, other processes will be in a waiting state, that is, "deadlock".

Above, when using setnx to obtain a lock, we will use the key lock The value of foo is set to the effective time of the lock. After the process obtains the lock, other processes will continue to detect whether the lock has timed out. If it timed out, the waiting process will also have the opportunity to obtain the lock.

However, when the lock times out, we can't simply use the Del command to delete the key lock Foo to release the lock. Considering the following situation, process P1 has first obtained the lock Foo, and then process P1 hangs. Processes P2 and P3 are constantly detecting whether the lock has been released or timed out. The execution process is as follows:

As can be seen from the above situation, after the lock timeout is detected, the process cannot directly and simply perform the Del delete key operation to obtain the lock.

In order to solve the problem that multiple processes may obtain locks at the same time in the above algorithm, let's look at the following algorithm.

We also assume that process P1 has first obtained the lock Foo, and then process P1 hangs. Next:

1. Process P4 executes setnx lock Foo to try to acquire the lock

2. Since process P1 has obtained the lock, P4 executes setnx lock Foo returns 0, that is, obtaining the lock failed

3. P4 execute get lock Foo to check whether the lock has timed out. If not, wait for a period of time and check again

4. If P4 detects that the lock has timed out, that is, the current time is greater than the key lock Foo, P4 will do the following

5. Because the GetSet operation will return the old value of the key while setting the value of the key, by comparing the key lock Whether the old value of foo is less than the current time can determine whether the process has obtained the lock

6. If another process P5 also detects that the lock has timed out and executes the GetSet operation before P4, the GetSet operation of P4 returns a timestamp greater than the current time, so P4 will not obtain the lock and continue to wait. Note that even if P4 next sets the key lock Setting the value of foo to a larger value than that of P5 has no effect.

In addition, it is worth noting that the lock is released in the process, that is, del lock Before foo operation, you need to judge whether the lock has timed out. If the lock has timed out, the lock may have been obtained by other processes. In this case, del lock is directly executed Foo operation will result in the release of locks obtained by other processes.

Program code

Use the following Python code to implement the above algorithm of using setnx command as distributed lock.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>