Sliding Window Log
You'll learn how to track individual request timestamps for precise rate limiting without fixed window boundary problems.
What Is Sliding Window Log?
Sliding Window Log stores the exact timestamp of each incoming request (the "log"). When evaluating rate limits, you look back from the current time for exactly your window duration (say, 60 seconds) and count how many requests fall within that range. Requests older than your window are discarded.
Unlike Fixed Window Counter (which resets at boundary times like :00 seconds), or algorithms that approximate across buckets, Sliding Window Log gives you exact counting over a true rolling window.
How It Works
Analogy: Imagine a notebook where you write down every time someone rings your doorbell. When someone rings at 12:34:50, you flip back through pages, cross out all entries before 12:33:50 (older than 60 seconds), and count what's left. If that count exceeds your limit, you don't answer.
Step-by-step:
- A new request arrives at time
T - Remove all logged timestamps older than
T - window_duration - Add current timestamp
Tto the log - Count remaining entries
- If count ≤ limit, accept; otherwise, reject
Why Use It?
- No boundary problem: A user can't exploit window edges to exceed limits (100 requests at 12:59:59, then 100 more at 13:00:00)
- Precision: Every request counted exactly where it occurred
- Fairness: True rolling enforcement
The Tradeoff
Storing every timestamp consumes memory proportional to request volume. For high-traffic scenarios, this can become expensive compared to counter-based algorithms.
Key Takeaway:
Sliding Window Log tracks every request timestamp for perfectly accurate rate limiting across true rolling windows, eliminating boundary exploits at the cost of higher memory usage.