Sliding Window Counter
You'll learn how sliding window counter approximates the accuracy of a sliding window log while keeping memory usage as low as a fixed window counter.
The Core Idea
Sliding window counter is a hybrid approach that combines the memory efficiency of fixed window counters with a smoother approximation of sliding window behavior. Instead of tracking every individual request like sliding window log, it uses counters from just two adjacent time windows—the current one and the previous one—then applies a weighted calculation based on how far you are into the current window.
How It Works
Imagine you're rate limiting to 100 requests per minute. At 10:00:30 (30 seconds into the current minute):
- Previous window (10:00:00–10:00:59): 80 requests
- Current window (10:01:00–ongoing): 40 requests
The sliding window counter calculates:
Estimated count = (previous_count × overlap_percentage) + current_count
= (80 × 50%) + 40
= 40 + 40 = 80 requests
The 50% overlap represents the portion of the previous window still "visible" in a sliding 60-second lookback from 10:00:30.
Why It Matters
This approach approximates sliding window log behavior without storing individual request timestamps. You only need two counters in memory instead of thousands of timestamps. It's not perfectly accurate—it assumes even request distribution within each window—but it's far smoother than fixed windows and dramatically more efficient than sliding logs.
Key Takeaway:
Sliding window counter uses weighted counts from the current and previous fixed windows to approximate true sliding behavior with minimal memory overhead.