Lazy Deletion and Background Cleanup
You'll learn: How deferring deletion of expired rate-limiter counters to background processes keeps your critical request path fast and responsive.
The Problem with Immediate Deletion
When a rate-limiting counter expires, the naive approach deletes it immediately during the request that detects expiration. This adds latency to your critical path—the milliseconds between receiving a user request and returning a rate-limit decision. Every microsecond counts when you're handling thousands of requests per second.
The Lazy Deletion Approach
Instead of cleaning up expired keys synchronously, lazy deletion means you ignore expired counters when you encounter them but don't actively delete them during the request flow. The key insight: reading an expired counter and treating it as non-existent is functionally identical to deleting it—but much faster.
Think of it like ignoring expired coupons in your wallet rather than stopping to throw them away every time you open it. You only clean them out during dedicated "wallet organizing" sessions.
Background Cleanup Process
A separate background job runs periodically (e.g., every few minutes) to scan for and delete expired keys. This process:
- Runs during low-traffic periods when possible
- Processes deletions in batches to minimize overhead
- Operates independently of user-facing requests
- Can be throttled to avoid overwhelming storage systems
In Redis, combining TTL expiration policies with occasional manual scans (SCAN + DEL) achieves this pattern. The TTL handles most cases automatically, while background jobs catch edge cases or perform deeper cleanup.
Why This Matters
Your rate limiter's primary job is answering "allowed or blocked?" in single-digit milliseconds. Offloading housekeeping to background workers keeps that promise. Some memory overhead from zombie keys is acceptable when it buys you consistent, predictable latency.
Key Takeaway: Lazy deletion separates the critical path (serving requests fast) from maintenance work (cleaning expired data), ensuring your rate limiter stays responsive under load while preventing unbounded memory growth.