Unlock Redis Locks From CLI

7.4k Views Asked by At

I have a java app that has multiple instances over a local network. It uses Redis Redlock to manage integrity of a shared database. Issue here is this java app is still highly unstable so that it crash lot of times. When one instance crashed and it held the lock at the time of crash all other instance get stuck. My question is can I release a lock from a Redis CLI when an instance of Java app which hold the lock crashed.

2

There are 2 best solutions below

0
On BEST ANSWER

With the CLI I could remove lock from Redis server with command DEL <lock name> When doing so the waiting thread could acquire the lock. I don't know this is the right way. But it works.

0
On

del <lock name> is correct and a good way to release the lock.

I just want to explain why the above works, and is a quick and easy way to release a stale lock! The accepted answer suggests it's a "hack" or something, so I was worried to apply it; discovered it's totally fine.

It looks like redis simply stores a unique ID key under the name of the lock. So if you acquire a lock named foo, you can see the lock's value:

get foo

And you see an ID there, e.g.: aeec8bc0598311ffacfd0242ac110005.

So anyone else that tries to acquire the lock, redis simply compares their ID to the value stored at foo and denies them. When the lock is released it's deleted.

If you manually delete the lock (del foo), then it is just manually doing what Redis would have done in the background when the lock was released naturally.