Home  >  Article  >  Database  >  How to use redis to implement distributed locks

How to use redis to implement distributed locks

(*-*)浩
(*-*)浩Original
2019-11-29 10:25:534479browse

How to use redis to implement distributed locks

Distributed locks can actually be understood as: controlling the distributed system to operate shared resources in an orderly manner, and maintaining consistency through mutual exclusion.

To give an inappropriate example: Suppose the shared resource is a house with various books in it, the distributed system is the people who want to enter the house to read, and the distributed lock ensures that the house only has A door and only one person can enter at a time, and the door has only one key. (Recommended learning: Redis video tutorial)

Use redis to implement distributed locks                          

Use the redis command set key value NX EX max -lock-time implements locking

Use redis command EVAL to implement unlocking

Lock:

Jedis jedis = new Jedis("127.0.0.1", 6379);
 
 private static final String SUCCESS = "OK";
 
 /**
 
  * 加锁操作
 
  * @param key 锁标识
 
  * @param value 客户端标识
 
  * @param timeOut 过期时间
 
  */
 
 public Boolean lock(String key,String value,Long timeOut){
 
     String var1 = jedis.set(key,value,"NX","EX",timeOut);
 
     if(LOCK_SUCCESS.equals(var1)){
 
         return true;
 
     }
 
     return false;
 
 }

Interpretation:

Lock operation: jedis.set(key,value,"NX","EX",timeOut)[Guaranteed atomic operation of locking]

The key is the key value of redis as the identification of the lock, and the value is used as the identification of the client here. Only when the key-value matches can you have the right to delete the lock [Guaranteed security]

Setting the expiration time through timeOut is guaranteed not to happen Deadlock will occur [Avoid deadlock]

NX, what does EX mean?

NX: The operation will only be performed when the key does not exist, if not exists;

EX: Set the expiration time of the key to seconds, the specific time is determined by the fifth Parameter decision

Unlock

Jedis jedis = new Jedis("127.0.0.1", 6379);
 
 private static final Long UNLOCK_SUCCESS = 1L;
 
 /**
 
  * 解锁操作
 
  * @param key 锁标识
 
  * @param value 客户端标识
 
  * @return
 
  */
 
 public static Boolean unLock(String key,String value){
 
     String luaScript = "if redis.call(\"get\",KEYS[1]) == ARGV[1] then return redis.call(\"del\",KEYS[1]) else  return 0 end";
 
     Object var2 = jedis.eval(luaScript,Collections.singletonList(key), Collections.singletonList(value));
 
     if (UNLOCK_SUCCESS == var2) {
 
         return true;
 
     }
    return false;
 }

Interpretation:

luaScript This string is a lua script, which means if according to If the value obtained by key is the same as the value passed in, execute del, otherwise it will return 0 [Ensure security]

jedis.eval(String, list, list); This command is to execute the lua script, KEYS The set of ARGV is the second parameter, and the set of ARGV is the third parameter [Atomic operation to ensure unlocking]

The above realizes how to use redis to correctly implement distributed locks, but there is a small flaw in the lock The appropriate expiration time should be set to an appropriate value. This actually needs to be considered based on the business scenario.

For more Redis-related technical articles, please visit the Redis Getting Started Tutorial column to learn!

The above is the detailed content of How to use redis to implement distributed locks. 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
Previous article:How to do paging in redisNext article:How to do paging in redis