1. One-Liner
The Fixed Window Counter divides time into fixed-size windows (e.g., 1 minute) and limits how many requests are allowed per window. When the window changes, the counter resets.
2. The Problem It Solves
Rate limiting protects APIs from abuse. Fixed Window is one of the simplest approaches: count requests in the current time bucket. When the bucket (window) expires, start fresh.
3. The Core Idea
Imagine a whiteboard that gets wiped every minute:
That's Fixed Window: fixed time chunks, count per chunk, reset on boundary.
4. How It Works
| Step | What Happens |
|---|---|
| 1. Get time | Get current timestamp |
| 2. Calc window | window_id = now / window_size |
| 3. Check window | Did we cross into a new window? |
| 4. Reset | If new window, set count = 0 |
| 5. Check limit | Is count >= limit? |
| 6a. YES → Deny | Window full, reject request |
| 6b. NO → Allow | Increment count, allow request |
5. Key Properties
| Property | Value |
|---|---|
| Edge burst? | Yes — 2x burst possible at window boundary |
| Memory | O(1) — window_id + count |
| Simplicity | Very simple to implement |
| Accuracy | Lower than sliding window |
Implementation Example (PYTHON)
import time
class FixedWindowCounter:
def __init__(self, window_size, limit):
self.window_size = window_size
self.limit = limit
self.current_window = 0
self.count = 0
def try_allow(self):
now = time.monotonic()
window_id = int(now // self.window_size)
if window_id != self.current_window:
self.current_window = window_id
self.count = 0
if self.count >= self.limit:
return False # DENY
self.count += 1
return True # ALLOW