Quick revision
The short answer
Clients that hit the 600-per-minute limit all wait for the same reset and retry at the same instant, so traffic synchronises at the top of every minute; a fixed window also lets a client send up to twice the limit across a boundary. Replace it with a sliding window counter, which weights the previous window's count by how much of it still overlaps, or with a token bucket, and return 429 with Retry-After and rate limit headers so clients can pace themselves and add jitter. Per-key window start times also break the synchronisation.
What to remember
In this answer, we traced the spikes at the top of every minute to the fixed window: clients that hit the limit all wait for the same clock reset and retry together, and the abrupt reset also allows twice the limit across a boundary. We replaced the counter with a sliding window counter or a token bucket, returned 429 with Retry-After and rate limit headers, asked clients to add jitter, and noted that per-key window starts break the alignment even before clients change.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Clients that hit the 600-per-minute limit all wait for the same reset and retry at the same instant, so traffic synchronises at the top of every minute; a fixed window also lets a client send up to twice the limit across a boundary. Replace it with a sliding window counter, which weights the previous window's count by how much of it still overlaps, or with a token bucket, and return 429 with Retry-After and rate limit headers so clients can pace themselves and add jitter. Per-key window start times also break the synchronisation.
Why this question is asked
Fixed-window counters are the first rate limiter most teams write: one counter per key per minute, reset on the minute. They are cheap and easy to explain, which is why they are everywhere. Their weakness shows up in graphs like the ones in this scenario, spikes at the top of each minute and a quiet remainder, and interviewers use it to check whether we can read the symptom back to the algorithm. In this answer, we will recap how a fixed window works, explain the synchronised retries and the boundary burst, replace the algorithm with a sliding window counter or a token bucket, and improve the responses so clients stop stampeding.
1A Quick Recap of the Fixed Window Counter
A fixed window counter divides time into windows aligned to the clock, here 12:00:00 to 12:00:59, 12:01:00 to 12:01:59, and so on. For each API key it keeps a counter for the current window. Each request increments the counter; while the counter is at most 600 the request passes, and after that the key gets 429 until the window ends. At the start of the next minute every counter starts again from zero. In Redis this is a single INCR on a key such as rl:key42:202611131200 with a 60-second expiry.
2Why Every Minute Starts With a Stampede
Think about a busy client. It sends steadily, uses up its 600 requests by, say, 12:00:40, and then gets 429 for the remaining 20 seconds. What does a typical client do with a 429 and no guidance? It retries in a loop, or it waits until 'the next minute'. Either way, its pent-up work goes out the moment the counter resets at 12:01:00.
Now multiply that by every client that hit its limit. All their windows reset at exactly the same instant, because the windows are aligned to the clock for everyone. So at 12:01:00 a large share of clients fire their backlog at once: the spike. Many of them burn through 600 requests within two seconds and get 429 again: the wave of rejections. Then they wait for 12:02:00, and the rest of the minute is quiet. The limiter has turned smooth demand into a synchronised pulse, which is harder on the backend than the same traffic spread evenly.
3The Boundary Burst: Twice the Limit in Two Seconds
The fixed window has a second flaw. Because counters reset abruptly, a client can send 600 requests in the last second of one window and 600 more in the first second of the next. That is 1,200 requests in about two seconds, twice the intended rate for any 60-second period that straddles the boundary. The limit promises 600 per minute but only enforces 600 per clock minute.
| Time | Window | Requests sent | Result |
|---|---|---|---|
| 12:00:59 | 12:00 | 600 (counter 0 -> 600) | all pass |
| 12:01:00 | 12:01 | 600 (counter reset to 0 -> 600) | all pass |
| 12:00:59 to 12:01:00 | any 60 s view | 1,200 | twice the limit |
4Fix 1: A Sliding Window Counter
The sliding window counter keeps the cheapness of fixed windows but smooths the boundary. It stores the counts of the current window and the previous one. To estimate how many requests happened in the last 60 seconds, it assumes the previous window's requests were spread evenly, and counts only the part of the previous window that still overlaps the sliding 60 seconds.
Suppose we are 15 seconds into the current minute. The previous minute had 500 requests and the current one has 200 so far. The last 60 seconds include the final 45 seconds of the previous minute, which is 75 percent of it, so the estimate is 200 + 500 x 0.75 = 575. That is under 600, so the request passes. There is no reset moment any more: allowance returns gradually as the previous window slides out, so clients are not all released at the same instant.
A token bucket, which refills continuously, solves both problems too: there is no reset instant to synchronise on, and the burst is bounded by the bucket's capacity, which we choose explicitly. Either algorithm is a big improvement; the token bucket is a good choice when we also want to state a burst size.
5Fix 2: Better 429 Responses, and Breaking the Alignment
The algorithm is half of the problem; the other half is that clients do not know when to come back. Every 429 should carry Retry-After with the number of seconds until the request would succeed, and every response, not just 429s, should carry rate limit headers with the limit, the remaining allowance and the time until it refills. The IETF RateLimit header fields draft defines a standard form; many APIs use X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset. Clients that can see 'remaining: 40' slow down before they hit the wall.
Our documentation and SDKs should tell clients to add jitter, a random delay, to their retries, for example Retry-After plus a random 0 to 5 seconds, so that thousands of clients told to wait do not all return in the same millisecond. If we keep a window-based scheme for some reason, we can also start each key's window at its first request instead of at the clock minute. Different keys then reset at different times, and the synchronisation disappears even before clients change.
- Estimate usage over the last 60 ssliding window or token bucket
- Under the limit?YesPass, add RateLimit-Remaining headerNoReturn 429 with Retry-After
- Client waits Retry-After plus jitterrandom 0 to 5 s spreads the retries
3 steps in order. Play them, or step through them one at a time.
6Follow-up questions
How large is the error of the sliding window counter compared with an exact sliding log?
The sliding window counter assumes the previous window's requests were evenly spread. If they were all at the very end of that window, the estimate undercounts; if they were all at the start, it overcounts. In the worst case the error is bounded by the previous window's count times the overlap fraction, so in theory it can admit somewhat more than the limit in an unlucky 60 seconds. In practice, with real traffic that is fairly smooth, measurements reported by large operators put the error at a fraction of a percent. An exact sliding log, which stores a timestamp per request, has no error but costs memory proportional to the limit per key, 600 entries here, which is why the counter is the common compromise.
Would you fail open or closed if the counter store is down?
For a limit that protects fairness between customers, fail open with a local fallback limit per node, because blocking all traffic would cause a bigger outage than the limiter prevents. For limits that protect billing or abuse-prone endpoints, failing closed may be right.
7Conclusion
In this answer, we traced the spikes at the top of every minute to the fixed window: clients that hit the limit all wait for the same clock reset and retry together, and the abrupt reset also allows twice the limit across a boundary. We replaced the counter with a sliding window counter or a token bucket, returned 429 with Retry-After and rate limit headers, asked clients to add jitter, and noted that per-key window starts break the alignment even before clients change.
Open with
The graphs are the fixed window synchronising our clients. Every key's counter resets at the same moment, the top of the clock minute. Busy clients use their 600 requests early in the minute, get 429s, and then either retry in a loop or wait for the reset.
Then, if they want more
So at second zero all of them fire their backlog together, many burn through their allowance in a couple of seconds and get rejected again, and the rest of the minute is quiet.
The fixed window has a second problem: a client can send 600 requests in the last second of one minute and 600 more in the first second of the next, so twice the limit in two seconds.
I would change the algorithm to a sliding window counter, which adds the current window's count to the previous window's count weighted by how much of it still overlaps the last sixty seconds, or to a token bucket. Both release allowance gradually, so there is no single moment to synchronise on.
Then I would fix the responses: 429 with Retry-After, and rate limit headers with the remaining allowance on every response, so well-behaved clients pace themselves, and I would tell clients to add random jitter to retries. If we kept windows, starting each key's window at its first request would also break the alignment.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is going on, and how would you change the algorithm and the responses?": https://scrivio-interview-library.netlify.app/architecture/api-design-at-scale/7c86dc1a13/