The background,

Recently in learning Redis related knowledge, learned the Redis distributed lock implementation and application. In this process, encountered some questions, this paper mainly records for this process encountered questions and distributed lock related knowledge points summary.

Ps. Online about Redis implementation of distributed lock articles, blogs, columns, etc. Many references are very complete.

Distributed lock

When it comes to distributed locks, there are several problems:

  • 1. What are the scenarios of distributed lock application? What is distributed lock and what characteristics should it have?
  • 2. Why is Redis suitable for distributed locking?
  • 3, using Redis to achieve distributed lock, what are the specific implementation methods?

With these questions, many answers can be found on the Internet, and a brief summary of these questions will be recorded here.

1. What are the scenarios of distributed lock application? What is distributed lock and what characteristics should it have?

Locks are used to ensure the correctness of shared resources when multiple processes or threads concurrently operate on them. In distributed applications, multiple instances of a service need to be deployed. To operate shared resources in distributed environments, distributed locks are required to ensure correct operations.

Distributed locks should be mutually exclusive, reentrant, lock timeout, high availability and other features. The first few features are the same as the specific features of local locks. High availability is an important feature of distributed locks.

As for the application scenarios of distributed lock, I looked for some recent projects I participated in, but few of them are actually applied to distributed lock. What I can learn are application scenarios like inventory deduction.

2. Why is Redis suitable for distributed locking?

  • High performance: Redis has high read and write performance and can cope with high concurrency lock scenarios
  • High reliability: Redis is a distributed system with high availability solutions

3, using Redis to achieve distributed lock, what are the specific implementation methods?

Using Redis to realize distributed locking should meet two conditions. The process of locking and unlocking must be atomic operation. Ensure high availability and use Redis Cluster for deployment.

The specific implementation scheme includes high reliable distributed lock based on single Redis node and multiple Redis nodes.

Redis implements distributed locking

3.1 Implementation based on single node

Online Redis deployment is generally in Cluster mode, based on the single-node implementation, that is, in Cluster mode, only one master will be unlocked. As for which master, it needs to be determined by the hash slot calculated by the Redis key (see Redis Cluster mode for details).

Locking and unlocking operations may require multiple operations, so atomic operations need to be ensured. Atomic operations can be realized through Redis single command and Lua script.

lock

Precautions for locking

  • 1, locking process to ensure atomicity
  • 2. Ensure that the lock added by the user can only be unlocked by the user, that is, the value of the lock added by Redis. The same value must be passed in to ensure the uniqueness of the value
  • 3. Set the lock timeout period to prevent other clients from obtaining the lock if the lock holder fails to release the lock abnormally. At the same time, the timeout period should be longer than the service processing time

Run the Redis command SET lock_key unique_value NX EX seconds to add a lock. The Redis command is executed in sequence, so only one lock can be added successfully.

unlock

Unlock is deleted by DEL command, in order to avoid wrong unlock (A locks, B unlocks), so we need to compare value, the whole process to ensure atomicity, so we use Lua script (unlock.

// Unlock compares whether unique_value is equalif redis.call("get",KEYS[1]) == ARGV[1] then
    return redis.call("del",KEYS[1])
else
    return 0
end
Copy the code
redis-cli  --eval  unlock.script lock_key , unique_value 
Copy the code

This implementation is based on a single node to save the lock information, which does not have high reliability. To ensure high reliability, it should be implemented based on multiple nodes.

Another disadvantage is that the lock may be released before the expiration of the set time, and other requests can continue to obtain the lock. The solution is to use Redisson, which basically works by starting a daemon thread that periodically checks for the existence of the lock, extending the key if it exists, and the client that attempts to acquire the lock spins to acquire it.

Github.com/redisson/re…

3.2 Implementation based on multiple nodes

RedLock algorithm

Antirez, the developer of Redis, proposed that the basic idea of RedLock algorithm is to let the client and a number of independent Instances of Redis request locking in turn. If the client can successfully lock with more than half of the instances, it can be considered that the client has successfully obtained the distributed lock.

The implementation steps are as follows:

  • 1. The client obtains the current time

  • 2. The client performs locking operation like N Redis nodes in turn: Locking operation is the same as unit price locking, and the expiration time must be set, but the Redis instance identifier must be added.

  • 3. The client completes the lock operation on all Redis nodes and calculates the total time of the whole process, which should also meet the following requirements:

    • (1) The client successfully locks more than half of the instances
    • (2) The total time for the client to obtain the lock is less than the expiration time

    If both conditions are not met, lock release is performed on all Redis nodes.

As for the RedLock algorithm, Martin Kleppmann, a distributed systems expert in the industry, had a debate with Antirez to evaluate the reliability of the algorithm. The details of the debate were all about scenarios in which abnormal conditions might cause RedLock to fail. For example, the client is blocked during locking or the machine clock jumps. (visible zhangtielei.com/posts/blog-…

3.3 summarize

Distributed system design is about achieving a balance between complexity and benefit, being as secure and reliable as possible, while avoiding over-design. For the sake of correctness, businesses have strict requirements on results, so it is recommended to use RedLock, but its disadvantages are heavy usage and high deployment cost. If the distributed lock based on a single Redis node is used for efficiency, the disadvantage of this scheme is that the lock is allowed to fail occasionally, and the advantage is that it is simple and efficient. In most cases, choose a distributed lock based on a single Redis node.

Four, other

  • The online Redis tool: try.redis. IO/can be used to familiarize yourself with re…

  • Reference: www.infoq.cn/article/dva…