Distributed Locking with Redisson in Spring Boot
Fast recall
Quick revision
Core answer
Practical experience with distributed coordination problems and the specific challenges of lock expiry, clock skew, and crash scenarios.
Using SETNX directly without an expiry, which can leave orphaned locks if the process crashes, permanently blocking other nodes.
Discuss the Martin Kleppmann vs Salvatore Sanfilippo debate about Redlock's safety guarantees and mention fencing tokens as a complement to Redlock.
Interview-ready guide
Interview answer
Distributed locking with Redis is a common interview topic because it looks simple but has real failure modes. The naive implementation — SET key NX EX 30 — works in single-node Redis but not across a cluster under failure scenarios.
Why the naive `SETNX` approach is fragile
SET key NX EX 30creates a key only if it doesn't exist, with a 30-second TTL — which means the first caller acquires the lock and others fail fast; this works on a single Redis instance- The problem: if the Redis primary fails after the key is set but before the replica replicates it, a new primary is elected without the lock key, and a second caller acquires the lock — two holders of the "exclusive" lock simultaneously
Redlock — the multi-node algorithm
- Redlock acquires the lock on N independent Redis instances (not a cluster) — for a lock to be held, it must be acquired on a majority (N/2 + 1) of instances; a single instance failure doesn't break the lock because the majority still holds it
- Redisson implements Redlock in Java with
RLock—lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS)handles the multi-instance coordination automatically
Lease time and watchdog
- Every distributed lock needs a TTL to prevent a crashed holder from holding the lock forever — Redisson's
RLockdefaults to a 30-second lease and a watchdog thread that renews the TTL every 10 seconds while the holder is alive - The watchdog stops renewing if the application process dies — which means the lock eventually expires naturally, and other holders can acquire it
When distributed locking is overkill
- For most application-level mutual exclusion, optimistic locking with a database version column is simpler and doesn't require Redis — use
@Versionin JPA; a conflicting update throwsOptimisticLockExceptionand the caller retries
Distributed Locking with Redisson in Spring Boot
Direct Answer
Redisson provides a RLock implementation that uses the Redlock algorithm to acquire a distributed lock across Redis nodes, ensuring mutual exclusion across JVM instances. You call lock.tryLock() with a wait time and lease time, execute your critical section, and release in a finally block. Key pitfalls include not releasing the lock in a finally block, setting lease times too short relative to the critical section duration, and using a single Redis node instead of RedLock's majority-quorum approach for strong guarantees.
Common Mistakes & Pitfalls
Using SETNX directly without an expiry, which can leave orphaned locks if the process crashes, permanently blocking other nodes.
Check your understanding
Answer these without looking back at the article.
- What is Redisson's watchdog mechanism and why does it matter?
- How does the Redlock algorithm differ from a simple single-node distributed lock?
- What is a fencing token and when would you use it alongside a distributed lock?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.