Interview › Databases & Caching
How would you use Redis for rate limiting?
Databases & Caching · Advanced level
Answer
For rate limiting, Redis commonly uses atomic counters with INCR and EXPIRE for fixed windows or sorted sets for sliding windows. Lua scripts help make multi-step limiter logic atomic.
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
- What is the difference between Redis as a cache and Redis as a primary datastore?
- How would you use Redis for distributed locking, and what are the caveats?
- Why are some Redis commands (KEYS, FLUSHALL) dangerous in production?
- What is a cache hit ratio, and how do you improve it?
- How does ElastiCache provide high availability and automatic failover?
- How do you decide what to cache and what not to cache?
- What is the difference between Redis Sentinel and Redis Cluster?
- How do you handle cache warming after a restart or failover?