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
EXECLock example:
SET lock:refresh:product:123 <uuid> NX PX 5000
Release only if GET lock value matches your uuid. Never DEL blindly.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
- How would you use Redis for rate limiting?
- What is the difference between Redis as a cache and Redis as a primary datastore?
- Why are some Redis commands (KEYS, FLUSHALL) dangerous in production?
- How do you decide what to cache and what not to cache?
- What is a cache hit ratio, and how do you improve it?
- How do you handle cache warming after a restart or failover?
- How does ElastiCache provide high availability and automatic failover?
- What is the blast radius of a cache outage, and how do you make the app degrade gracefully?