LearnThatStack Ace your next interview
API Design · question
Question 30 of 92

How do you implement rate limiting in APIs?

intermediate Pro
← All API Design questions

The full answer covers the core mechanics, when this pattern wins, and the trade-offs interviewers most often probe. It includes a code sample. Read it once at your own pace, then try to recall the structure from memory before the interview. Pro also includes this question's interview lens - the likely follow-up probes and what you can say in the room.

Full answer + code samples + AI explanations

Unlock to read the complete answer for this premium question.

Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

Rate limiting shares capacity fairly, and the algorithm decides how

A limiter protects a shared resource from any one caller, malicious or just retrying badly. The algorithm decides which bursts get through.

For this question · all 3 chapters

1/3 A fixed window lets two bursts in
rate limitA fixed window lets two bursts in100 per minute, 197 in two seconds0:000:301:001:302:0097 at 0:59sliding window: last 60 sLimiter429: 100 in last 60 slast 60 s1001of 100clock1:01100 timestamps per caller
rate limitA fixed window lets two bursts in0:001:002:0097 at 0:59sliding window: last 60 sLimiter429: 100 in last 60 slast 60 s1001of 100clock1:01100 timestamps per caller

A fixed window lets two bursts in

  1. A limit exists for four reasons: it stops abuse and holds back a client stuck in a retry loop. The limit also caps what one caller can cost you and keeps one tenant from taking the capacity the others need. This limiter allows 100 requests per clock minute, and at 0:00 its counter starts at 0. The limit is a promise about a shared resource, not a punishment.
  2. Most traffic looks like this: a client sends three requests in the first 40 seconds. The limiter counts them as 1, 2 and 3 and lets each one through. A client that behaves never reaches the limit at all. For that client, the counter is bookkeeping that the limiter never acts on.
  3. The limiter refuses no request here, because the window is the budget for this minute and this burst spends that budget. A burst of 97 requests arrives at 0:59, the last second of the minute. The counter reaches 100 of 100, so this minute's window is now full. But the minute is about to end.
  4. The reset at the window boundary is the whole flaw of a fixed window. At 1:00 the limiter sets its request counter from 100 back to 0. So the limiter keeps no record of the old window, not even the 97 requests from one second ago. A retry storm (a surge of retried requests) is exactly the traffic that triggers this flaw.
  5. Counting requests per clock window leaves a loophole at the window boundary. A second burst of 100 requests arrives at 1:01, just after the boundary. The new window's counter reaches 100 again. So 197 requests pass in two seconds, under a limit of 100 per minute. The total is nearly twice the limit. But the limiter never refused a single request.
  6. A sliding window closes the loophole and refuses the same second burst with 429. At 1:01, the last 60 seconds still include the 97 requests from 0:59 and the 3 from earlier, so the counter stays at 100. This fix is accurate but costs memory, because each caller needs a log of 100 timestamps instead of one integer. Most limiters use a compromise: a sliding counter that weights the previous window's count.
Chapter 1 · step 1 of 6

A limit exists for four reasons: it stops abuse and holds back a client stuck in a retry loop. The limit also caps what one caller can cost you and keeps one tenant from taking the capacity the others need. This limiter allows 100 requests per clock minute, and at 0:00 its counter starts at 0. The limit is a promise about a shared resource, not a punishment.

1 of 6 Use ← → or swipe See the concept

© LearnThatStack - diagrams may not be republished without permission.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

74 of 92 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime