Rate Limiting Algorithms
Control how many requests a client can make within a time window. Each algorithm makes different trade-offs between accuracy, memory, and burst handling.
// concept
Rate limiting protects your services from being overwhelmed by too many requests. It enforces a maximum number of operations within a given time window. The challenge is choosing the right algorithm — each has different trade-offs in accuracy, memory usage, and how it handles request bursts.
Fixed Window Counter
O(1)Divide time into fixed windows with a counter per window. Simple but has the boundary burst problem.
Sliding Window Log
O(n)Store every request timestamp and filter stale entries. 100% accurate but memory intensive.
Sliding Window Counter
O(1)Hybrid: weighted counters from current + previous windows. Memory efficient and fairly accurate.
Token Bucket
O(1)Tokens refill at a fixed rate. Each request consumes a token. Allows controlled bursting.
Leaky Bucket
O(1)Bucket fills with requests and drains at a constant rate. Smooths bursty traffic.