Interview Databases & Caching

How would you use Redis for distributed locking, and what are the caveats?

Databases & Caching · Advanced level

Answer

Redis distributed locks commonly use SET key value NX PX ttl with a unique token and compare-and-delete release. They are useful for efficiency locks but require caution for correctness-critical workflows.

Technical explanation

Rate limits can use INCR/EXPIRE or sorted sets, ideally through Lua for atomicity.

Distributed locks should use SET key value NX PX ttl with unique token and safe compare-and-delete release; they are better for efficiency than hard correctness.

Security baseline is private subnets, SG restrictions, TLS, AUTH/ACLs, secret rotation, and no public exposure.

Hands-on example

Rate limit example:

MULTI

INCR rl:tenant:42:minute:202606301015

EXPIRE rl:tenant:42:minute:202606301015 120

EXEC

Lock example:

SET lock:refresh:product:123 <uuid> NX PX 5000

Release only if GET lock value matches your uuid. Never DEL blindly.

Preparing for an interview?

Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.

More Databases & Caching interview questions

← All Databases & Caching questions