Sliding Window vs Fixed Window
You'll learn: How sliding and fixed time windows differ when tracking failure rates in circuit breakers — and why the choice matters for accuracy and resource usage.
When a circuit breaker monitors failures to decide whether to open, it needs to count errors over some time period. Two common approaches are fixed windows and sliding windows.
Fixed Window
A fixed window divides time into distinct buckets (e.g., one-minute intervals). All failures within 10:00-10:01 count in one bucket, then at 10:01 the count resets to zero for the new bucket.
Analogy: Imagine counting customers entering a store each hour. At the top of each hour, you reset your counter to zero.
Pros: Simple, memory-efficient (just one counter per window).
Cons: Boundary problem — if 50 failures happen at 10:00:59 and 50 at 10:01:01, the fixed window sees them as separate. The circuit breaker might miss a spike that spans the boundary.
Sliding Window
A sliding window continuously tracks the last N seconds (or N requests) as a moving window. At any moment, you're looking at "the past 60 seconds from right now."
Analogy: A rolling camera recording the last minute of activity, not just snapshots on the hour.
Pros: More accurate failure rate tracking; no boundary artifacts.
Cons: Requires storing timestamps or counters for multiple sub-buckets (e.g., 60 one-second buckets), consuming more memory and CPU.
Which to Choose?
Fixed windows work well for low-memory environments or when approximate failure rates suffice. Sliding windows provide smoother, more accurate detection but at higher computational cost — crucial when milliseconds and precision matter.
Key Takeaway: Fixed windows are simple and efficient but can miss boundary-spanning spikes; sliding windows offer continuous accuracy at the cost of added complexity and resource usage.