Memory Optimization with Sliding Window Log
What You'll Learn
You'll learn how to balance rate-limiting accuracy with memory efficiency by storing only essential timestamps and pruning expired entries automatically.
The Memory Challenge with Sliding Window Log
The Sliding Window Log algorithm tracks individual request timestamps to provide precise rate limiting. If your limit is 100 requests per minute, you store 100 timestamps per user. But at scale—with millions of users—this becomes memory-intensive fast.
The problem: A naive implementation might store every timestamp indefinitely, or clean up too slowly, wasting precious Redis memory.
Optimization Strategies
1. Store Only What You Need
Instead of keeping all historical data, store timestamps only within the current window. For a 1-minute window, you never need timestamps older than 60 seconds.
2. Automatic Expiration
Use Redis's ZREMRANGEBYSCORE to prune old timestamps atomically:
ZREMRANGEBYSCORE user:123:requests 0 (current_time - window_size)
This removes all entries older than your window before counting.
3. Set TTL on the Entire Key
Apply a Redis EXPIRE to the sorted set itself. If a user goes inactive, the entire key vanishes automatically after the window duration, reclaiming memory.
4. Limit Maximum Entries
Cap the sorted set size. If your limit is 100 req/min, you'll never legitimately have more than 100 entries. Reject excess and trim:
ZREMRANGEBYRANK user:123:requests 0 -101 // Keep only newest 100
The Analogy
Think of a security logbook at a building entrance. You don't keep every visitor's entry from the past decade—you shred entries older than 30 days and destroy the entire logbook if the building closes.
Key Takeaway:
Memory-optimized Sliding Window Log stores only timestamps within the current window, uses automatic pruning with ZREMRANGEBYSCORE, applies TTL for inactive users, and caps entry counts to balance accuracy with efficient resource usage.