Redis & Caching (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Rate Limiting with Sliding Window Log (ZSET).
senior LevelMost AskedredisAdvanced Internals
In Simple Words: Fixed-window rate limiters suffer from 2x traffic spikes at window boundaries. Sliding Window Log uses a Redis Sorted Set:
- Store timestamp as both member and score:
ZADD rate:user1 <now> <now>. - Remove timestamps older than window:
ZREMRANGEBYSCORE rate:user1 0 (<now> - 60). - Count remaining requests:
ZCARD rate:user1. If $< 100$, allow request!