Rate Limiter: Scope
Say "rate limiter" to most candidates and "token bucket" comes back before the sentence has ended. Minutes zero to ten exist to hold that name back. An algorithm answers a question nobody has asked yet: what the limit is for, who counts as one client, and how many milliseconds you may spend finding out. By minute ten those are settled, along with one rate, one call and two small entities.
Read the question again for what it leaves out. "Limits how many requests" says nothing about how exact the count must be, and "rejects the rest" says nothing about what happens when the limiter itself is broken. Those two gaps decide more of this design than the choice of algorithm does.
What the Limit Is For
Ask these six, and take each answer as a constraint. The fourth is the one candidates skip, and it matters most.
- Who is a client: an API key, a user, an address? This sets what you count by and how many counts there are. Agreed: the API key, with 2 million active. A scanner using one key is stopped here, the case Chapter 2's When the Cache Hurts left for this chapter. Many addresses with no key at all is a different problem, and it returns as a curveball.
- How much traffic, on how many servers? This sets the rate of checks. Agreed: 2,000 requests per key a day, on twenty API servers.
- What does a limit look like, and is a burst allowed? This picks the algorithm. Agreed: 100 a minute by default, with bursts of up to 100, and higher plans get higher numbers.
- Is this limit protecting the API, or do we bill on it? Protection can tolerate a small overshoot, meaning some requests admitted past the limit. A billed number needs a metering pipeline, which is another system. Agreed: protection and fairness, with the overshoot bounded and stated.
- How much delay may the limiter add? This sets where the count may live. Agreed: 5 milliseconds at the worst.
- A library in each API server, or a service they call? A separate service is a second hop and a second fleet. A gateway in front of the servers is the same check one hop earlier, and the move when the gateway already checks the keys. Agreed: a library. A service of its own becomes the move if many teams write servers in many languages.
That fourth question belongs ahead of any talk of counting, and you can give the reason as you ask it: "Before I pick an algorithm: is this limit protecting the API, or do we bill on it? A billed number needs a different level of exactness."
Promises and Exclusions
Three requirements are functional. Every request is checked by key before any work is done on it. Limits differ by plan. A refusal is a 429, the HTTP status for too many requests, and it says when to retry.
Three more are non-functional, and they shape the design harder. The check adds at most 5 milliseconds. The count is shared across the fleet, with its overshoot bounded and stated. And the limiter's failure never takes the whole API down with it.
Then rule three things out by name, so the interviewer can pull one back in now and not at minute twenty-five. Floods from many addresses are the job of the edge, the network layer in front of your servers. Billing and metering are another system. And one service's defence of its own capacity is Backend Deep Dive's lesson on rate limiting and backpressure.
Eighty Thousand Checks
Every request is one check, so the limiter's load is the API's traffic. Two million keys at 2,000 requests a day is 4 billion requests a day. Take the day as about 100,000 seconds, and that comes to 40,000 a second. Assume a peak of twice the average, and it is 80,000. Over twenty servers, that is 4,000 checks a second on each at peak. The limiter starts from those numbers.
One client hardly registers. A hundred a minute is under two a second. No key is the load; the fleet is, which makes this a problem of shared counting and not of fast counting.
Memory is the sum candidates linger on. Assume about 100 bytes to hold one key's count. Two million keys is 200 MB, a fifth of a percent of a 100 GB cache node. It decides nothing, and you gain time by saying so: "Memory is 200 megabytes, so I'm dropping it. Eighty thousand checks a second is what I'm designing for."
One more sum, for a store you are not drawing yet. If the count ever has to live in one shared place, that place is an in-memory node, and one node does about 100,000 simple operations a second. Count a check as one operation, which flatters it, because a check is really a small script that reads, adds and writes. Assume each node runs at half its limit, which is 50,000. Eighty thousand over 50,000 is under two, so two nodes. The limiter is small. If it comes to a store, the cost will be the network call and not the hardware.
The estimator below runs the same sums and lets you move them. It calls a check a write, because every check changes a count on its way through, and its decision line is the two nodes at 50,000 each.
The Call and What Sits Behind It
Bursts of up to 100 point at a token bucket, and now the name has something to rest on. Each key has a bucket of tokens, a request spends one, and tokens flow back at a set rate. That gives two entities. A bucket holds a key, its tokens and the time it last refilled. A rule holds a plan, its capacity and its refill rate.
The limiter is a library inside each server, so there is one call to design: check this key at a cost of one. It answers allowed or not, the tokens left, and the seconds until a token is due. The cost is a parameter because a call may need to take more than one token, and the deep dives have a use for that. What the client sees of all this is a refusal: a 429 with a Retry-After header, the wait in whole seconds.
check(key, cost=1) -> allowed, tokens_left, retry_after_seconds 429 Too Many Requests Retry-After: 1 bucket: key, tokens, last_refill rule: plan, capacity, refill_rate
- Naming an algorithm in the first minute, before anyone has said what the limit is for. When the interviewer asks why that one, the choice has nothing to rest on.
- Assuming a client means an address, then finding a whole office behind one. The interviewer asks what everyone in that office is supposed to do, and the design has no answer.
- Sizing memory at length while the check rate goes unsaid. The minutes go on 200 MB, and the number that sizes the store is still missing.
- Saying "accurate" without asking what a billed number would need. The interviewer cannot tell which of two systems you think you are building.
- What the limit protects, asked before how it is counted.
- Agreed traffic turned into 80,000 checks a second and two nodes, out loud.
- The memory sum crossed out on purpose, with why it changes nothing.
Knowledge Check
Only one question fits before you choose a counting rule. Which answer moves this design the most?
- Which status code a refused client should get back from the API
- How many bytes each server has to keep for one client's count
- Whether the limit protects the API or is a number you bill on
- How many requests one client can send in a second at its fastest
Two million keys make 2,000 requests a day each, with a peak of twice the average. How many checks a second do you design for?
- About 4,000, shared out across twenty servers
- About 40,000, over a day of 100,000 seconds
- About 4 billion, because each request is a check
- About 80,000, twice the average of 40,000
One of these sums gets said and crossed out. Which one?
- The 200 MB of memory for 2 million keys at 100 bytes
- The 80,000 checks a second at twice the average
- The two nodes that hold 80,000 checks at half load
- The 4,000 checks a second on each of twenty servers
Which of these do you rule out of scope out loud?
- Keeping the limiter's own failure from taking down the API
- Billing and metering, which need an exact count of their own
- Checking every request before any work has been done on it
- Telling a refused client how long it should wait before retrying
You got correct