Full answer
Fixed window counter
- Key = client + floor(now / window). Increment, compare with the limit. Memory: one number per client. Weakness: the boundary burst. With a limit of 5 per 10 seconds, 5 requests at second 9.9 and 5 more at second 10.1 are all allowed (10 requests in 0.2 seconds).
Sliding window log
- Keep the timestamp of every accepted request. On a new request drop timestamps older than the window, count the rest, allow if below the limit. Exact, but memory grows with the limit (O(limit) per client).
Sliding window counter
- Keep counts for the current and previous windows. Estimated count = previous * (fraction of the previous window still inside the sliding window) + current. Constant memory, small error, and very popular.
Other points
- Choose the key: user, API key, IP address (careful with shared NATs), or a combination.
- Return 429 with Retry-After and rate limit headers. Do the limiting early, before expensive work.
- Use a shared store (Redis) when you have several servers, and make the check atomic.
javascript
const LIMIT = 5, WINDOW = 10; // 5 requests per 10 seconds
function fixedWindow() {
const counts = new Map();
return (now) => { const slot = Math.floor(now / WINDOW); const n = (counts.get(slot) ?? 0) + 1; counts.set(slot, n); return n <= LIMIT; };
}
function slidingLog() {
let log = [];
return (now) => { log = log.filter((t) => t > now - WINDOW); if (log.length >= LIMIT) return false; log.push(now); return true; };
}
function slidingCounter() {
const counts = new Map();
return (now) => {
const slot = Math.floor(now / WINDOW), elapsed = (now % WINDOW) / WINDOW;
const estimate = (counts.get(slot - 1) ?? 0) * (1 - elapsed) + (counts.get(slot) ?? 0);
if (estimate + 1 > LIMIT) return false;
counts.set(slot, (counts.get(slot) ?? 0) + 1);
return true;
};
}
// Five requests just before the boundary at t = 10 s, five more just after it.
const times = [9.5, 9.6, 9.7, 9.8, 9.9, 10.1, 10.2, 10.3, 10.4, 10.5];
for (const [name, make] of [['fixed window', fixedWindow], ['sliding log', slidingLog], ['sliding counter', slidingCounter]]) {
const allow = make();
const result = times.map((t) => allow(t));
console.log(name.padEnd(16), result.map((r) => (r ? 'ok ' : 'no ')).join(''), '| allowed in 1 second:', result.filter(Boolean).length);
}
console.log('limit is', LIMIT, 'per', WINDOW, 'seconds, so at most', LIMIT, 'should pass in these 1.0 seconds');Plain functions with explicit timestamps, so the result is exact and repeatable. Real systems read the clock and keep the state in Redis or a gateway. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): fixed window ok ok ok ok ok ok ok ok ok ok | allowed in 1 second: 10 sliding log ok ok ok ok ok no no no no no | allowed in 1 second: 5 sliding counter ok ok ok ok ok no no no no no | allowed in 1 second: 5 limit is 5 per 10 seconds, so at most 5 should pass in these 1.0 seconds
Key points
- Fixed windows are simple and allow double bursts at the boundary.
- A sliding log is exact but stores every timestamp.
- A sliding window counter approximates with two numbers.
- Return 429 with Retry-After.
- Use a shared, atomic store for many servers.
Common mistakes
- Using a fixed window and being surprised by double bursts.
- Limiting only by IP address behind a shared proxy.
- Keeping unlimited counters in memory for every client.
- Limiting after the expensive work is done.
- Not returning Retry-After.
Follow-ups
- How does a sliding window counter estimate the count?
- Where do you apply rate limits (gateway, application, both)?
- How do you limit by user and by IP together?
Tips
- Describe the boundary burst with numbers.
- Compare memory use.
- Say that 429 and Retry-After are part of the contract.