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

How to realize distributed Lock through redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article shows the implementation of distributed locks through redis specific operations, code concise and easy to understand, absolutely can make you shine, through the detailed introduction of this article I hope you can gain something.

Distributed locks are a way to control synchronous access to shared resources between distributed systems. In distributed systems, it is often necessary to coordinate their actions. If different systems or different hosts of the same system share a resource or a set of resources, then access to these resources often requires mutual exclusion to prevent interference with each other to ensure consistency, in which case distributed locks are required.

Distributed locks need to solve problems

Mutual exclusion: only one client can have a lock at any time, and multiple clients cannot obtain it at the same time.

Security: locks can only be deleted by the user who holds the lock, not by other users

Deadlock: The client acquiring the lock is down for some reason and fails to release the lock. Other clients cannot acquire the lock. There needs to be a mechanism to avoid this kind of problem.

Fault-tolerant: When some nodes are down, the client can still acquire locks or release locks.

How to implement distributed locks with Redis:

SETNX key value : If the key does not exist, create and assign

Time Complexity: 0(1)

Return value: 1 if setting succeeds; 0 if setting fails.

But in this case, the key we get is long-term valid, so how should we solve the long-term valid problem?

EXPIRE key seconds

Set the lifetime of the key. When the key expires (lifetime is 0) , it will be deleted automatically.

Disadvantages: Atomicity is not satisfied

Here is the pseudocode.

//This program is dangerous. If it crashes after executing the second line, the key will be occupied and cannot be released. RedisService redisService = SpringUtils.getBean(RedisService.class); long status = redisService.setnx(key, "1");if(status == 1) { redisService.expire(key, expire); //Execute exclusive resource logic doOcuppiedWork();}

Redis implements distributed locks:

SET key value [EX seconds] [PX milliseconds] [NX|XX]

EX second : Set the expiration time of the key to second seconds

PX millisecond : Sets the key expiration time to millisecond

NX : Set keys only if they are not present

XX: Set keys only if they already exist

SET operation successfully completed, return OK , otherwise return nil

Here is the pseudocode.

RedisService redisService = SpringUtils.getBean(RedisService.class); .String result = redisService.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);if ("OK".equals(result)) { //Execute exclusive resource logic doOcuppiedWork();}

A large number of key expired at the same time note

Concentration expired, because clearing a large number of keys is time-consuming, there will be a short stuck phenomenon

Release scheme: add random value to each key when setting the expiration time of key

After reading the above, do you know how to implement distributed locks through redis? If you still want to learn more skills or want to know more related content, welcome to pay attention to 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