Fixed window counts requests in each clock window, for example each minute. It is cheap. But a client can send the full limit at the end of one window, then the full limit again at the start of the next one. This doubles the rate around the boundary.
Sliding window log stores a timestamp for every request. It counts the timestamps inside the last window. It is exact, but its memory grows with the limit.
Sliding window counter keeps counts for the current window and the previous window. It weights the previous count by how much of that window still overlaps the sliding window. For example, 30 s into the current minute, it counts half of the previous minute. It is a cheap and close approximation.
Token bucket refills tokens at a steady rate, up to a maximum capacity. Each request spends one token. It enforces an average rate, but allows bursts up to the capacity. It is a common choice for APIs.
Leaky bucket puts requests in a queue and processes them at a constant rate. This makes the output smooth. Bursts must wait in the queue, or they are dropped.
- A fixed window is cheap, but allows a double burst at the window boundaries.
- A sliding window log is exact, but uses a lot of memory.
- A sliding window counter is a cheap, weighted approximation.
- A token bucket enforces an average rate and still allows bursts.
- A leaky bucket sends requests out at a constant rate, which smooths traffic.