Lock on redis cluster
Asked Answered
S

1

8

I have a Redis cluster of 3 master nodes and each master has corresponding slave nodes. I would like to acquire a lock on the cluster to perform some write operations and then release the lock.

From what I've read is - To connect to a cluster we generally connect to one node in the cluster and perform all operations on that node which in-turn handles re-directing to other nodes in the cluster.

Is it possible to acquire lock on a Redis cluster? [P.S I am using Redisson client] From the examples in Redisson client under Multilock and redlock (https://github.com/redisson/redisson/wiki/8.-Distributed-locks-and-synchronizers), they are acquiring a lock on individual nodes.

  • How does Multi lock or Red lock work on a cluster?
  • How and what kind of lock do I use if I have a Redis cluster?
  • Which library (Jedis/Redisson) do I use?

Jedis also seems to have support for locking on the cluster (https://github.com/kaidul/jedis-lock).

P.S: I've read extensively on this, but I've not been able to find clear answers on locking on a cluster. Would really appreciate some help.

Scaphoid answered 20/3, 2020 at 9:22 Comment(0)
S
2

I found a solution to my question above.

As far as we are using the same key to acquire a lock across all client nodes, all the attempts to acquire the lock will go to the same node on the Redis cluster. So you can just use simple Rlock from Redisson.

See comments to this question: https://github.com/leandromoreira/redlock-rb/issues/63

Scaphoid answered 23/3, 2020 at 10:24 Comment(1)
But, if you read from master and slaves instances, it can happen that you won't read current state of that key (due to replica lag)Dunlop

© 2022 - 2024 — McMap. All rights reserved.