Lesson 2 · Minutes 10-20

Rate Limiter: First Design

Problem

Put the count where the request already is. From minute ten to minute twenty there is no new system on the board: each API server keeps its own buckets in memory, and the check is a little arithmetic that runs before the handler does. You walk two calls through it, defend it, and then show the interviewer the multiplication that breaks it before they show you.

A shared store would be the obvious thing to draw, and it is too early for it: the interviewer could not tell which requirement forced it. Four pushes are coming, collected in the quiz after this lesson, and Scaling It answers them in that order.

A Bucket in Every Server

Clients reach twenty API servers through a load balancer, which spreads requests among them with no regard for whose they are. Each server holds a map from API key to token bucket in its own memory. The check runs once the key is known and before any handler starts, so a refused request has cost one lookup and some arithmetic.

A bucket holds at most 100 tokens and refills at 100 a minute, and it stores only two numbers: the tokens it had and the time of its last refill. Each check works out what the elapsed time has earned, adds that up to the capacity, and takes one. No timer runs anywhere.

The burst of 100 pointed you at this counting rule, and you have compared it with nothing, so say that before the interviewer does: "Treat the token bucket as the counting rule for now. I'll come back to why not a window."

The first design: a bucket in every server
Clientsends its API key Load balancerany server Handlerthe real work API servers20, a bucket per key request admitted 429 read

One Call Admitted, One Refused

Take a client whose bucket on this server held 40 tokens six seconds ago. At 100 a minute, six seconds has earned 10 tokens, so the bucket now holds 50. The check takes one, stores 49 and the current time, and the handler runs.

Now the same client has spent everything, the last token only a moment ago. The check finds less than one token, takes nothing, and the handler never runs. The server answers with a 429 and the wait for the next token. At 100 a minute, a token arrives every 0.6 seconds. Retry-After counts whole seconds, so the header says 1.

Right for One Server

The case for this design comes first, and it is a real one. On any one server it checks every request, respects plans by reading capacity and refill from the key's rule, and answers with a 429. Each server loads the few plan rules when it starts, and learns a key's plan from the lookup that authenticates it. All of this takes no hop, a network call on the way to the answer, and no second system to run.

It is exactly right where a limit per server is what you want, which is guarding each server from being swamped, and for a small API on one or two servers. Tokens are worked out on demand, so 2 million keys need a subtraction and no timers.

Twenty Times the Plan

Three limits get named now and fixed later. The promised limit is multiplied by the number of servers, because each one counts alone. A restart hands out full buckets, because the map dies with the process. And each server trusts its own clock, so one that jumps forward earns tokens nobody granted.

The first of those is the one to volunteer, with its number and how you would see it happen: "One server seeing all of a client's calls is the case this fits. At twenty servers, a limit of a hundred a minute lets through two thousand a minute, and I'd see that as one client running at twenty times its plan. So the count moves."

Interview Pitfalls
  • Reaching for the shared store at once. The interviewer cannot tell which requirement forced it, and you have no limit left to volunteer.
  • A timer per bucket to refill it. That is 2 million timers doing what a subtraction does, and the interviewer asks what they cost.
  • Putting the check after the handler, so that a refusal has already spent what the limit protects.
  • Stopping without the multiplication: twenty servers, two thousand a minute. The interviewer has to find it for you, and that is the mark they write down.
What Interviewers Listen For
  • Tokens counted aloud through one admitted call and one refused call.
  • A limit per server defended as the right tool for guarding one server, before the fleet comes up.
  • Twenty times the plan, offered as the first limit before anyone pushes.

Knowledge Check

"One client's calls spread across all twenty of your API servers. Whose bucket is counting them?"

  • "Each server's own, so give every server a twentieth of the client's limit to enforce."
  • "One server's, by hashing the key at the load balancer so a client always lands there."
  • "No single one today, so move the buckets into one store that all twenty servers ask."
  • "All of them, with each server telling the other nineteen its counts once a second."

"That store is a network hop on every request. What does it cost you, and can you pay it?"

  • "One to two milliseconds across zones, inside our 5 ms budget, so keep it in the region."
  • "One to two milliseconds is too much on every call, so run the check after answering."
  • "About 70 ms to a single home region, paid so that all regions can share a single count."
  • "Nothing at all, if each server refreshes its copy of the buckets from it once a minute."

"Two servers read the same bucket, both see one token left, and both admit. The hundredth request got in twice. What now?"

  • "Take a lock on the client's key in the store around every read and write of its bucket."
  • "Make the check and the take a single atomic step on the store node that owns the key."
  • "Stamp both writes with the time and let the later write win that last token outright."
  • "Replicate each bucket synchronously, so that both servers always read the latest count."

"It is peak, and the counter store has just gone down. Every request is waiting on it. What happens next?"

  • "Refuse every request until it returns, because a request we cannot count is not allowed."
  • "Let every request through until it returns, because the API matters more than its limits."
  • "Hold requests in a queue on each server until the store is back and can count them all."
  • "Time out fast, fall back to a local bucket per server, and set open or closed per route."

You got correct