Redis--distributed lock with high concurrency

1. What is a distributed lock?

To introduce distributed locks, we must first mention thread locks and process locks that correspond to distributed locks.

  • Thread lock: Mainly used to lock methods and code blocks. When a method or code uses a lock, only one thread executes the method or code segment at the same time. Thread locks are only effective in the same JVM, because the implementation of thread locks is basically achieved by sharing memory between threads. For example, synchronized is the shared object header, and the display lock Lock is sharing a certain variable (state).
  • Process lock: In order to control multiple processes in the same operating system to access a shared resource, each process cannot access the resources of other processes because of the independence of processes, so process locks cannot be achieved through thread locks such as synchronized.
  • Distributed lock: When multiple processes are not in the same system, use distributed locks to control access to resources by multiple processes.

2. Use scenarios of distributed locks

Both inter-thread concurrency problems and inter-process concurrency problems can be solved by distributed locks, but this is strongly not recommended! Because using distributed locks to solve these small problems is very resource intensive! Distributed locks should be used to solve the multi-process concurrency problem in a distributed situation.

There is a situation where both thread A and thread B share a variable X.

  • If it is a single-machine situation (single JVM), shared memory between threads, as long as the use of thread locks can solve the concurrency problem.
  • If it is a distributed situation (multiple JVMs), thread A and thread B are probably not in the same JVM, so thread locks will not be effective. At this time, distributed locks will be used to solve them.

3. Implementation of Distributed Lock (Redis)

The key to the realization of distributed locks is to build a storage server outside the distributed application server to store lock information. At this time, we easily think of Redis. First, we need to build a Redis server, and use the Redis server to store lock information.

Several key points to pay attention to when implementing:

  1. The lock information must expire and timeout, and a thread cannot hold a lock for a long time and cause a deadlock;
  2. Only one thread can acquire the lock at the same time.

Several redis commands to be used:

setnx(key, value): "set if not exits", if the key-value does not exist, it is successfully added to the cache and returns 1, otherwise it returns 0.

get(key): Get the value corresponding to the key, and return nil if it does not exist.

getset(key, value): Get the value corresponding to the key first, return nil if it does not exist, and then update the old value to the new value.

expire(key, seconds): Set the validity period of the key-value to seconds.

Guess you like

Origin blog.csdn.net/weixin_42201180/article/details/112978508