Java Interview Questions

Redis Distributed Lock: SETNX, Redlock, and Redisson

"How would you implement a distributed lock with Redis? What edge cases do you need to handle?"

Why Interviewers Ask This

Distributed locks are the second most-asked Redis interview question and a notorious source of production incidents. Interviewers use it to test whether you think about failure modes — expiration racing with business logic, process GC pauses, client-side delete-by-other-owner bugs — not just "SETNX and done."

How to Answer

  1. 1

    State the minimum correct primitive: SET key uniqueValue NX PX 30000. NX ensures atomic acquire-if-absent; PX sets an expiration so a dead client can't hold the lock forever; uniqueValue lets only the rightful owner release it.

  2. 2

    Explain the release must be atomic: use a Lua script that checks value == uniqueValue then DEL. Otherwise between GET and DEL, the lock may have expired and been reacquired by someone else.

  3. 3

    Cover the expiration pitfall: if the business logic runs longer than the lock TTL, the lock auto-expires mid-work and another worker acquires it, so two workers run concurrently. Solution: a watch-dog that periodically extends the TTL while the business is still executing.

  4. 4

    Add reentrancy: count acquisitions per thread (in a hash or local counter) so the same thread can re-acquire its own lock. Redisson's RLock does this using a hash with thread ID as field.

  5. 5

    Discuss Redlock: Antirez's multi-master algorithm for HA clusters. Acquire on majority of N independent Redis nodes within a bounded time. Still debated — Martin Kleppmann showed failure modes under GC pauses and clock skew.

Sample Answer

A minimum correct Redis lock uses SET key uniqueValue NX PX 30000. NX makes the set atomic "only if absent", PX sets an expiration so a crashed client can't hold the lock forever, and uniqueValue — typically a UUID per client — lets only the owner release it. Release must be atomic: you can't GET and then DEL, because the lock could have expired and been reacquired by another client between those two calls, and then you'd delete their lock. The standard solution is a Lua script that checks value equality and deletes in one atomic step. The harder problem is expiration vs business time. If I set the TTL to 30s and my job takes 35s, the lock expires at 30s, another worker acquires it, and now two workers are both running the critical section. The fix is a watch-dog — a background thread that extends the TTL periodically (say every 10s, TTL stays 30s) for as long as the business logic is still running. This is exactly what Redisson's RLock does by default. For reentrancy, you store a counter keyed by thread ID — Redisson uses a Redis hash where the field is "threadId:lockName" and the value is the reentry count, incrementing on re-acquisition and decrementing on release, only deleting the key when the count hits zero. For high availability, the Redlock algorithm acquires on a majority of N independent Redis masters within a bounded time window, handling single-node failures. Redlock is debated — Martin Kleppmann pointed out that a long GC pause on the client can let a competitor acquire the lock before the original client realizes it lost ownership. For strict correctness, you need a fencing token — a monotonically increasing number returned on acquire that the downstream storage uses to reject stale writers. For most business use cases, a single-node Redis with watch-dog and atomic release is more than good enough.

Pro Tips

  • Always use a Lua script for release — a plain DEL after GET is a classic production bug.

  • Know the watch-dog pattern or mention Redisson by name — most candidates skip the TTL vs business-time problem.

  • For senior roles, be ready to discuss Redlock, fencing tokens, and the Kleppmann-Antirez debate.

  • If you blank on the SET flags during the interview, HireMe AI can surface the exact SET key value NX PX syntax.

Frequently Asked Questions

Why not use SETNX + EXPIRE as two commands?

They are not atomic. If the client crashes between SETNX and EXPIRE, the key has no TTL and becomes a permanent lock. Use SET with NX PX in one command.

What's the difference between a fair and non-fair lock?

A fair lock grants the lock in acquisition order (queue-based); a non-fair lock lets any waiting client grab it when released. Redisson supports both.

Should I always use Redlock?

No. For most applications a single master with sentinel or cluster is sufficient. Redlock adds latency and complexity and has known failure modes under GC pauses.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free