search
HomeDatabaseRedisWhy is distributed lock needed in Redis? How to achieve?

This article will introduce you to distributed locks in Redis, why distributed locks are needed, and how Redis implements distributed locks. I hope it will be helpful to you!

Why is distributed lock needed in Redis? How to achieve?

Why distributed locks are needed

Why distributed locks are needed

Use The purpose of distributed locks is nothing more than to ensure that only one client can operate on shared resources at the same time.

We often encounter concurrency problems when performing logical processing in distributed applications. [Related recommendations: Redis Video Tutorial]

For example, an operation requires modifying the user's status. To modify the status, you need to read the user's status first, modify it in the memory, and then save it back after the modification. . If such operations are performed at the same time, concurrency problems will arise because the two operations of reading and saving state are not atomic.

At this time, distributed locks are used to limit the concurrent execution of the program. As a caching middleware system, redis can provide this kind of distributed lock mechanism.

The essence is to occupy a pit in redis. When other processes want to occupy the pit, they find that they have already occupied the pit. If it is occupied, just wait and try again later

Generally speaking, the distributed locks available in the production environment need to meet the following points:

  • Mutual exclusion, mutual exclusion is the basic feature of the lock. Only one thread can hold the lock at the same time and perform critical operations;
  • Timeout release, timeout release is another necessary feature of the lock, you can Compare the innodb_lock_wait_timeout configuration in the MySQL InnoDB engine to prevent unnecessary thread waiting and resource waste through timeout release;
  • Reentrancy, in a distributed environment, on the same node If the same thread acquires the lock, the request can still be successful again;

Implementation method

Use SETNX to implement

The usage method of SETNX is: SETNX key value. Only when the key key does not exist, the value of the key key is set to value. If the key key exists, SETNX does not take any action. .

boolean result = jedis.setnx("lock-key",true)== 1L;
if  (result) {
    try {
        // do something
    } finally {
        jedis.del("lock-key");
    }
 }

This solution has a fatal problem, that is, after a thread acquires the lock and cannot perform the unlocking operation normally due to some abnormal factors (such as downtime), then the lock will never be released. .

To this end, we can add a timeout period to this lock

The effect of executing SET key value EX seconds is equivalent to executing SETEX key seconds value

The effect of executing SET key value PX milliseconds is equivalent to executing PSETEX key milliseconds value

String result = jedis.set("lock-key",true, 5);
if ("OK".equals(result)) {
    try {
        // do something
    } finally {
        jedis.del("lock-key");
    }
}

The solution looks perfect , but in fact there will still be problems

Just imagine that a certain thread A acquires the lock and sets the expiration time to 10s, and then it takes 15s to execute the business logic. At this time, thread A acquires The lock has already been automatically released by the expiration mechanism of Redis

After thread A acquires the lock and 10 seconds have passed, the changed lock may have been acquired by other threads. When thread A finishes executing the business logic and prepares to unlock (DEL key), it is possible to delete the lock that has been acquired by other threads.

So the best way is to determine whether the lock belongs to you when unlocking. We can set the value to a unique value when setting keyuniqueValue ( It can be a random value, UUID, or a combination of machine number and thread number, signature, etc.).

When unlocking, that is, when deleting the key, first determine whether the value corresponding to the key is equal to the previously set value. If it is equal, the key can be deleted

String velue= String.valueOf(System.currentTimeMillis())
String result = jedis.set("lock-key",velue, 5);
if ("OK".equals(result)) {
    try {
        // do something
    } finally {
      	//非原子操作
	      if(jedis.get("lock-key")==value){
		        jedis.del("lock-key");
        }    
    }
}

We can see the problem at a glance here Come on: GET and DEL are two separate operations. An exception may occur in the gap between GET execution and before DEL execution.

If we only need to ensure that the unlocking code is atomic we can solve the problem

Here we introduce a new method, which is Lua script, the example is as follows :

if redis.call("get",KEYS[1]) == ARGV[1] then
    return redis.call("del",KEYS[1])
else
    return 0
end

where ARGV[1] represents the unique value specified when setting the key.

Due to the atomicity of the Lua script, during the process of Redis executing the script, other client commands need to wait for the Lua script to be executed before they can be executed.

Ensure that the expiration time is greater than the business execution time

In order to prevent multiple threads from executing business code at the same time, it is necessary to ensure that the expiration time is greater than the business execution time

Add A boolean type attribute isOpenExpirationRenewal, used to identify whether to enable scheduled refresh expiration time

In addition, a scheduleExpirationRenewal method is used to enable the thread to refresh the expiration time

The locking code sets isOpenExpirationRenewal to true after successfully acquiring the lock, and calls the scheduleExpirationRenewal method to start the thread that refreshes the expiration time.

The unlocking code adds a line of code and sets the isOpenExpirationRenewal attribute. If false, stop the thread polling for refreshing the expiration time

Redisson implementation

If the lock is successfully acquired, a scheduled task will be started, and the scheduled task will be checked regularly for renewal

The time difference between each call of this scheduled schedule is internalLockLeaseTime / 3, which is 10 seconds

By default, the locking time is 30 seconds. If the locked business does not After execution, when 30-10 = 20 seconds, a renewal will be performed and the lock will be reset to 30 seconds

RedLock

In the cluster, when the master node hangs up, the slave node will take its place, but there is no obvious perception on the client. It turns out that the first client successfully applied for a lock on the master node, but before the lock could be synchronized to the slave node, the master node suddenly died. Then the slave node becomes the master node. This new node does not have this lock inside, so when another client comes to request a lock, it is approved immediately. This will cause the same lock in the system to be held by two clients at the same time, resulting in insecurity

The Redlock algorithm is to solve this problem

To use Redlock, you need to provide multiple Redis instances. These instances were previously independent of each other and had no master-slave relationship. Like many distributed algorithms, redlock also uses most mechanisms

When locking, it will send set instructions to more than half of the nodes. As long as more than half of the nodesset succeed, the lock is considered successful. . When releasing the lock, a del instruction needs to be sent to all nodes. However, the Redlock algorithm also needs to consider many detailed issues such as error retry and clock drift. At the same time, because Redlock needs to read and write to multiple nodes, it means that the performance of Redis will be lower than that of a single instance.

Redlock algorithm is a high-availability mode introduced on the basis of a single Redis node. Redlock is based on N completely independent Redis nodes, usually an odd number greater than 3 (usually N can be set to 5), which can basically ensure that the cluster Each node will not go down at the same time.

Assuming that the current cluster has 5 nodes, the client running the Redlock algorithm performs the following steps in order to complete the operation of acquiring the lock

  • Client record Current system time, in milliseconds;
  • Try to obtain locks from 5 Redis instances using the same key in sequence. When requesting to obtain a lock from Redis, the client should set a network connection and response timeout. , the timeout should be less than the expiration time of the lock to avoid problems due to network failures;
  • The client uses the current time minus the time to start acquiring the lock to get the time to acquire the lock, if and only if from half The above Redis node acquires the lock, and when the time used is less than the lock expiration time, the lock is successfully acquired;
  • If the lock is acquired, the real effective time of the key is equal to the effective time minus the time used to acquire the lock. time to reduce the chance of timeout;
  • If the lock acquisition fails, the client should unlock it on all Redis instances, even the node where the previous operation request failed, to prevent the server response message from being lost, but Inconsistency caused by successful addition of actual data.

In other words, assuming that the lock expires in 30 seconds and it takes 31 seconds to lock the three nodes, naturally the locking failed

In the Java client officially recommended by RedisRedisson has a built-in implementation of RedLock

https://redis.io/topics/distlock

https:// github.com/redisson/redisson/wiki

RedLock problem:

RedLock only ensures the high availability of the lock, but does not guarantee the correctness of the lock

RedLock is a distributed system that relies heavily on the system clock

Martin’s criticism of RedLock:

  • For efficiency improvement scenarios, RedLock is too heavy.
  • For scenarios that require extremely high accuracy, RedLock cannot guarantee accuracy.

This article is reproduced from: https://juejin.cn/post/7018968452136173576

Author: With a distant heart

More programming related For knowledge, please visit: programming video! !

The above is the detailed content of Why is distributed lock needed in Redis? How to achieve?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
Redis: Beyond SQL - The NoSQL PerspectiveRedis: Beyond SQL - The NoSQL PerspectiveMay 08, 2025 am 12:25 AM

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis: A Comparison to Traditional Database ServersRedis: A Comparison to Traditional Database ServersMay 07, 2025 am 12:09 AM

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redis: Introduction to a Powerful In-Memory Data StoreRedis: Introduction to a Powerful In-Memory Data StoreMay 06, 2025 am 12:08 AM

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Is Redis Primarily a Database?Is Redis Primarily a Database?May 05, 2025 am 12:07 AM

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redis: Database, Server, or Something Else?Redis: Database, Server, or Something Else?May 04, 2025 am 12:08 AM

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redis: Unveiling Its Purpose and Key ApplicationsRedis: Unveiling Its Purpose and Key ApplicationsMay 03, 2025 am 12:11 AM

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis: A Guide to Key-Value Data StoresRedis: A Guide to Key-Value Data StoresMay 02, 2025 am 12:10 AM

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis: Caching, Session Management, and MoreRedis: Caching, Session Management, and MoreMay 01, 2025 am 12:03 AM

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools