Redis › Rate Limiting with Redis

What is the sliding window log algorithm for rate limiting, and how can it be implemented using a Redis sorted set?

Intermediate

Answer

The sliding window log algorithm stores a timestamp for every single request in a sorted set, removes timestamps older than the allowed window before checking the current count, and rejects the request if too many timestamps remain within the current window, providing more accurate rate limiting than a fixed window since it does not have a hard reset boundary that can be gamed.
ZREMRANGEBYSCORE requests:user123 0 (current_time - 60)
ZADD requests:user123 current_time current_time
ZCARD requests:user123
Real-world example A payment API uses a sliding window log to precisely enforce ten requests per minute per customer, correctly preventing a burst of requests that would otherwise slip through right at a fixed window's reset boundary.

Common follow-ups: What is the memory cost of storing a timestamp for every single request?;How does this approach compare in accuracy to a simpler fixed window counter?