Your limit is 100 requests a minute. How did 200 get through?
Question
Your API allows 100 requests a minute per API key. The code below counts them per clock minute: a fixed window.
A client sends 100 requests at 12:00:59 and 100 more at 12:01:01. All 200 pass.
- Why did 200 get through in two seconds?
- Compare two fixes, a sliding log and a sliding window counter. How many of the second 100 does each let through?
- What should the API send back when it rejects a request?
LIMIT = 100 counts = {} # (api_key, minute) -> requests counted def allow(api_key, now): # now: seconds since 12:00:00 key = (api_key, int(now // 60)) if counts.get(key, 0) >= LIMIT: return False counts[key] = counts.get(key, 0) + 1 return True
Show hints (3)
- "WHEN IT RESETS"
Which minute does a request at 12:00:59 count in? And one at 12:01:01? What is the count at 12:01:00?
- "THE LAST 60 SECONDS"
The rule should mean "100 in any 60 seconds". At 12:01:01, how many requests came in the last 60 seconds?
- "TWO COUNTERS"
A sliding window counter keeps two counts: last minute's and this minute's. At 12:01:01, how much of last minute is still in the last 60 seconds?
Show checked explanation
The fixed window resets at 12:01:00, so the second 100 find an empty count and all 200 pass. A sliding log counts the last 60 seconds exactly and rejects all of the second 100. A sliding window counter estimates that count and lets 2 through. Reject with 429 Too Many Requests and a Retry-After header.
LIMIT, WINDOW = 100, 60 counts = {} # (api_key, minute) -> accepted requests def allow(api_key, now): # now: seconds since 12:00:00 minute, into = divmod(now, WINDOW) previous = counts.get((api_key, minute - 1), 0) current = counts.get((api_key, minute), 0) estimate = previous * (1 - into / WINDOW) + current if estimate >= LIMIT: return False counts[(api_key, minute)] = current + 1 return True for clock, now in [("12:00:59", 59), ("12:01:01", 61), ("12:01:30", 90)]: passed = sum(allow("k1", now) for _ in range(100)) print(f"{clock}: {passed} of 100 pass")
12:00:59: 100 of 100 pass 12:01:01: 2 of 100 pass 12:01:30: 48 of 100 pass
Code run on CPython 3.12 against the requests above, next to a sliding log and the starter. Checked by an editor other than the author.
Walkthrough
Why 200 pass. 12:00:59 falls in clock minute 0, and 12:01:01 in minute 1, whose count starts at 0. So each burst finds room for 100. Around its edge, a fixed window lets through up to twice the limit.
Both fixes count the last 60 seconds instead:
| method | keeps per key | second 100 at 12:01:01 | exact? |
|---|---|---|---|
| fixed window | 1 counter | 100 pass | no |
| sliding log | up to 100 times | 0 pass | yes |
| sliding window counter | 2 counters | 2 pass | estimate |
Sliding log. Keep the time of each accepted request. Before each decision, drop the times that are 60 seconds old or more, and count the rest. At 12:01:01 all 100 times from 12:00:59 are still inside, so every new request is rejected.
Sliding window counter. This is the code above. At 12:01:01 you are 1 second into the minute, so 59 of the last 60 seconds belong to last minute. The estimate is 100 × 59/60 plus this minute's count, about 98.3 at first. Two requests fit under 100, and the third is rejected. The estimate assumes last minute's requests were spread evenly. Here they all came at 12:00:59, so at 12:01:30 it counts only 50 of them and lets 48 more through.
What to send back. HTTP 429 Too Many Requests, with a Retry-After header that says how many seconds to wait. With the log, the 100 requests from 12:00:59 leave the window at 12:01:59. So at 12:01:01 the answer is Retry-After: 58.
Common mistakes
- Treating the sliding window counter as exact. Here it lets 150 requests through from 12:00:59 to 12:01:30, in 31 seconds.
- Answering 503 Service Unavailable to a client over its limit. 503 means the server is overloaded or down for maintenance. 429 means this client sent too many requests.
- Retrying at once after a 429. A tight retry loop turns one burst into many. Wait as long as
Retry-Aftersays. Without it, back off with growing, slightly random waits.
Follow-up questions
- "Your API runs on 5 servers. Where do the counts live?" In one shared store, such as Redis, keyed by API key and minute. Five local counters would allow up to 500 a minute. For a fixed window,
INCRadds 1 and returns the new count atomically. For the sliding counter, read both counts, decide and add 1 in oneEVALcall. Let keys expire after two minutes. - "Which one would you pick for a public API?" Usually the sliding window counter or a token bucket: two numbers per key, good enough for most traffic. Pick the log when the limit must hold in every 60 seconds, as for calls you pay a partner for. It stores one time per allowed request: 10,000 for a limit of 10,000 an hour.
What a strong answer covers
Shows the edge burst with real times and counts the last 60 seconds instead. Weighs the log's exact count and memory against the counter's two numbers and its error. Returns 429 with Retry-After and shares the counts across servers.
Open in interactive practice deck →