Skip to main content

Implement an atomic Redis sliding-window rate limiter

Design a distributed rate limiter in Redis using a Sorted Set (ZSET) or Lua script without race conditions.

Official Reference

Model Answer & Explanation

-- Atomic Redis Lua flow
-- 1. ZREMRANGEBYSCORE key 0 (now - window)
-- 2. count = ZCARD key
-- 3. if count < limit: ZADD key now request_id; PEXPIRE key window
-- 4. return count < limit

In a Lua script (or MULTI/EXEC pipeline), remove timestamps older than now - window with ZREMRANGEBYSCORE, check ZCARD < limit, add the current unique member (timestamp + uuid) with ZADD, and set PEXPIRE so idle keys expire. Executing inside Lua guarantees atomicity across workers.

Follow-up Interview Question · What is the memory overhead of a ZSET per user vs a token-bucket or sliding-window counter?

Module Context & References

PostgreSQL & Redis · Practice SQL & schema drills The classic SQL queries, locking patterns and optimization problems tested repeatedly
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.