Quick revision
The short answer
We call redisson.getLock("lock:invoice:42"), then tryLock(5, TimeUnit.SECONDS) to wait up to five seconds, do the work, and unlock() in a finally block. Because we gave no lease time, Redisson's watchdog holds the lock with a 30-second lease and renews it every 10 seconds while our process is alive and still holds it. If the process dies, renewal stops and the key expires within about 30 seconds, after which another instance can take the lock.
What to remember
In this answer, we took an RLock with tryLock(5, TimeUnit.SECONDS), released it in finally, and saw that Redisson stores it as a hash keyed by client and thread with a reentry count, which makes it reentrant and stops other threads from unlocking it. Without an explicit lease, the watchdog keeps a 30-second lease renewed every 10 seconds, so a dead process loses the lock within about 30 seconds. With an explicit lease, the watchdog is off and the lock expires after the lease even if the work continues.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We call redisson.getLock("lock:invoice:42"), then tryLock(5, TimeUnit.SECONDS) to wait up to five seconds, do the work, and unlock() in a finally block. Because we gave no lease time, Redisson's watchdog holds the lock with a 30-second lease and renews it every 10 seconds while our process is alive and still holds it. If the process dies, renewal stops and the key expires within about 30 seconds, after which another instance can take the lock.
Why this question is asked
Many Spring Boot services need 'only one instance does this at a time': generating an invoice, running a nightly job, refreshing a token. Writing SET NX locks by hand is easy to get subtly wrong, so Redisson's RLock is a common choice. Interviewers ask about it to check that we know what the lock actually guarantees, especially what keeps it alive and what happens when an instance crashes. In this answer, we will take and release a lock correctly, look at how Redisson stores it, see how the watchdog keeps it alive and when the lock goes away after a crash, and compare it with an explicit lease time.
1Taking and Releasing the Lock
An RLock behaves like java.util.concurrent.locks.Lock, except that it lives in Redis and so is shared by every instance of our service. tryLock with a wait time tries to acquire the lock and, if another instance holds it, waits up to that time before giving up and returning false. That is usually better than lock(), which waits forever.
Notice that unlock() sits in finally, and only inside the branch where we got the lock. Calling unlock() on a lock we do not hold throws IllegalMonitorStateException, so an unlock outside the if would fail every time tryLock returns false. tryLock(long, TimeUnit) can also throw InterruptedException, which the calling method must declare or handle.
2How Redisson Stores the Lock
The lock is a Redis hash stored under the lock name. It has one field, made of the Redisson client's unique id and the Java thread id, for example 'b5e1c0d2-...:57', and the value is a reentry count. The key has a TTL, which is the lease. Acquiring and releasing are Lua scripts, so checking the owner and changing the count happen atomically.
Because the owner includes the thread, the same thread can lock again; the count goes to 2, and it must unlock twice before the key is deleted. That is reentrancy, the same behaviour as ReentrantLock. A different thread, even in the same JVM, is a different owner, so if it calls unlock() Redisson refuses with IllegalMonitorStateException rather than releasing someone else's lock.
3What the Watchdog Does
We did not pass a lease time, so Redisson uses its watchdog. The lease is lockWatchdogTimeout, 30 seconds by default, and a background timer in our process extends the TTL back to 30 seconds every third of that, so every 10 seconds, for as long as the lock is held and the process is alive. When we unlock, renewal stops and the key is deleted.
The reason for this design is that we rarely know how long the work will take. A fixed 30-second lease would expire under a 2-minute invoice and let another instance in. The watchdog keeps the lease short, so a crash frees the lock quickly, while still covering work of any length.
7 messages in order. Play them, or step through them one at a time.
After a crash, the lock disappears within one lease of the last renewal.
4When the Lock Is Released if the Process Dies
If our process crashes, is killed or loses its network, the watchdog stops renewing. The key still has whatever TTL the last renewal gave it, at most 30 seconds, and then Redis deletes it. So another instance can acquire the lock within about 30 seconds of the crash, never immediately, and never 'forever', which is the main advantage over a lock without a TTL.
Waiting instances are not polling. Redisson subscribes them to a pub/sub channel tied to the lock name, and the unlock script publishes a message on it, so a waiter wakes up and retries at once. When a lock expires instead of being unlocked, there is no such message, and waiters find out when their next timed retry runs.
5Explicit Lease Time Turns the Watchdog Off
tryLock(waitTime, leaseTime, unit) and lock(leaseTime, unit) take an explicit lease. With one, there is no watchdog: the key expires after leaseTime even if the work is still running. That is useful when we would rather lose the lock than let a stuck process hold it for long, but it means work longer than the lease runs without the lock, and another instance can start the same work.
So the rule of thumb is: no lease time, and the watchdog covers long work; an explicit lease only when we know the work is always far shorter than it. In both cases the lock is a lease, so for work where a double run would be harmful, the protected operation should also be idempotent.
6Follow-up questions
How are waiting threads told that the lock was released?
Through Redis pub/sub. A waiting tryLock subscribes to a channel derived from the lock name, and the unlock script publishes a message there when it deletes the key. The waiting thread wakes and tries to acquire again immediately. It also uses timed retries based on the remaining TTL, so it still notices a lock that expired without an unlock message.
Can I change the watchdog timeout?
Yes, through the Redisson configuration's lockWatchdogTimeout setting. A shorter value frees locks faster after a crash but renews more often and makes the lock more sensitive to long pauses, such as garbage collection, which also stop renewal.
Does the same thread locking twice cause a deadlock?
No. RLock is reentrant: the second lock() from the same thread increments the count in the hash. It must call unlock() as many times as it locked; the key is deleted when the count reaches zero.
7Conclusion
In this answer, we took an RLock with tryLock(5, TimeUnit.SECONDS), released it in finally, and saw that Redisson stores it as a hash keyed by client and thread with a reentry count, which makes it reentrant and stops other threads from unlocking it. Without an explicit lease, the watchdog keeps a 30-second lease renewed every 10 seconds, so a dead process loses the lock within about 30 seconds. With an explicit lease, the watchdog is off and the lock expires after the lease even if the work continues.
Open with
I get the lock with redisson.getLock and the name, say lock:invoice:42, then call tryLock(5, TimeUnit.SECONDS), which waits up to five seconds and returns false if someone else still holds it.
Then, if they want more
If it returns true, I do the work in a try block and call unlock in finally, so the lock is released even after an exception, and I only unlock inside that branch, because unlocking a lock I don't hold throws IllegalMonitorStateException.
Internally the lock is a hash under that key, with a field for the client id and thread id and a reentry count, so the same thread can lock again and a different thread can't release it.
Because I didn't pass a lease time, Redisson's watchdog manages the TTL: the lease is lockWatchdogTimeout, 30 seconds by default, and it's extended every 10 seconds while my process is alive and holds the lock.
If the process dies, renewal stops and Redis expires the key within about 30 seconds, and another instance can then acquire it; waiters are woken through pub/sub when someone unlocks normally.
If I passed an explicit lease, like tryLock(5, 30, SECONDS), the watchdog would be off and the lock would expire after 30 seconds even if the work were still running.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Show how to take and release an RLock with a wait time, explain what the watchdog does...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/f3d5af7b68/