Counter Expiration and TTL Management
What You'll Learn
You'll learn how to automatically clean up expired rate limit counters by setting appropriate Time-To-Live (TTL) values, preventing memory bloat without manual intervention.
The Core Problem
When implementing rate limiters in Redis, every user request creates or updates a counter key. Without cleanup, these keys accumulate forever—even for users who never return. In a system handling millions of users, this leads to unbounded memory growth and eventual crashes.
How TTL Solves This
Time-To-Live (TTL) is a Redis feature that automatically deletes a key after a specified duration. When you create a rate limit counter, you set its TTL to match (or slightly exceed) your rate limit window.
The Mechanism
For a "100 requests per hour" limit:
- Create counter key:
rate_limit:user123 - Set TTL: 3600 seconds (1 hour)
- Redis automatically deletes the key after expiration
Analogy: Think of TTL like a self-destructing note. Once the time window passes, the evidence disappears automatically—no janitor needed.
Implementation Pattern
When using INCR for counting:
INCR rate_limit:user123
EXPIRE rate_limit:user123 3600
Critical: These must be atomic. Use a Lua script or SET with EX option to avoid race conditions where the counter exists but has no TTL (memory leak).
For sliding window counters, set TTL to the window size plus a small buffer (e.g., 3660 seconds for a 1-hour window) to ensure data persists through the entire evaluation period.
Best Practices
- Always set TTL atomically with key creation
- Add 10-20% buffer to TTL for clock skew
- Monitor keys without TTL (
redis-cli --bigkeysorTTL -1checks) - Use longer TTLs for analytics/debugging needs
Key Takeaway: Setting appropriate TTLs on rate limit counters ensures automatic garbage collection, keeping memory usage bounded and your system healthy without manual cleanup jobs.