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

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