Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of realizing distributed Lock by redis

2025-04-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article shows the specific operation of redis to achieve distributed locks, the code is concise and easy to understand, can definitely make your eyes bright, through the detailed introduction of this article, I hope you can get something.

In fact, distributed locking can be understood as controlling the distributed system to operate on shared resources in an orderly manner and maintain consistency through mutual exclusion.

To take an inappropriate example: suppose the shared resource is a house with all kinds of books, and the distributed system is the person who wants to enter the house to read. The distributed lock is to ensure that the house has only one door and only one person can enter at a time. And the door has only one key.

Using redis to realize distributed Lock

Locking using the redis command set key value NX EX max-lock-time

Use the redis command EVAL to unlock

Lock:

Jedis jedis = new Jedis ("127.0.0.1", 6379); private static final String SUCCESS = "OK"; / * * locking operation * @ param key lock ID * @ param value client ID * @ param timeOut expiration * / 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:

Locking operation: jedis.set (key,value, "NX", "EX", timeOut) [guaranteed atomic operation]

Key is the key value of redis as the identification of the lock. Here, value is used as the identification of the client. Only key-value matching has the right to delete the lock [guarantee security]

Set the expiration time through timeOut to ensure that there is no deadlock [avoid deadlock]

What does NX,EX mean?

NX: the operation will only be performed when the key is not available, if not exists

EX: set the expiration time of key to seconds, which is determined by the fifth parameter

Unlock

Jedis jedis = new Jedis ("127.0.0.1", 6379); private static final Long UNLOCK_SUCCESS = 1L / * * unlock operation * @ param key lock ID * @ param value client ID * @ 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:

The string luaScript is a lua script, which means that if the value obtained by key is the same as the value passed in, del is executed, otherwise 0 [guarantee security] is returned.

Jedis.eval (String,list,list); this command is to execute the lua script, the collection of KEYS is the second parameter, and the set of ARGV is the third parameter [atomic operation to ensure unlocking]

The above implementation of how to use redis to correctly implement distributed locks, but there is a small flaw is how appropriate the lock expiration time should be set, which actually needs to be considered according to the business scenario.

After reading the above, have you mastered the method of implementing distributed locks in redis? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report