Fixed Window and Sliding Window Rate Limiting
What you'll learn: How fixed and sliding window approaches compare for measuring traffic rates, and why one is more accurate but costlier than the other.
The Core Problem
After learning the Token Bucket Algorithm, you know rate limiting can be implemented many ways. Two common time-windowing approaches are fixed window counters and sliding window logs—each trades off simplicity, accuracy, and resource usage differently.
Fixed Window Counters
This approach divides time into fixed intervals (e.g., 1-minute windows starting at :00, :01, :02). You count requests in the current window and reset the counter when the window expires.
Analogy: Like counting customers entering a store every hour, resetting at the top of each hour (9:00, 10:00, 11:00).
Trade-off: Simple and memory-efficient, but suffers from boundary bursts. If a user makes 100 requests at 9:59 and 100 more at 10:00, they've sent 200 requests in one second despite a "100/hour" limit—both windows show compliance individually.
Sliding Window Logs
This approach stores timestamps of recent requests and counts only those within a rolling time period from "now." For a 1-minute window, you check how many requests occurred in the last 60 seconds at any moment.
Analogy: Like reviewing a security camera's last 60 seconds continuously, not just resetting hourly.
Trade-off: Much more accurate—no boundary exploits—but requires storing individual timestamps, consuming more memory and CPU for filtering expired entries.
Hybrid: Sliding Window Counter
A middle ground uses fixed windows but weighs the previous window proportionally. If 40% into the current minute, count (60% × previous_window_count) + current_window_count. More accurate than pure fixed windows with minimal overhead.
Key Takeaway: Fixed windows are simple and lightweight but allow boundary bursts; sliding window logs provide precise rate enforcement at higher memory cost; hybrid approaches balance both concerns for practical systems.