Design a Rate Limiter
🚦 What is a rate limiter? It is the piece of code that decides whether a request may go through right now. Every client gets a budget, such as 100 requests per minute. A request inside the budget is allowed; one over it is turned away, usually with an HTTP
429 Too Many Requests. It protects a service from one noisy client using up capacity that everyone shares.
Rate limiters show up in interviews in two forms. In a system design round, the question is where the counters live across many servers. In a low-level design round, which is this post, the question is the code inside one process: the classes, the algorithms and the locking. The design here is also the building block the distributed version is made of, and we connect the two at the end.
Requirements
"Design a rate limiter for our API. Each client should be limited to a number of requests per time period."
The prompt hides three big decisions: which algorithm, what "client" means, and what happens when many threads ask at once. The questions below settle them.
Clarifying Questions
You: Which algorithm should it use: a simple counter per minute, or something smoother?
Interviewer: We are not sure yet. Walk me through the options. We want to pick one per deployment and switch without rewriting the limiter.
You: What is a client? An API key, a user, an IP address?
Interviewer: Any string we pass in. Each client has its own budget; one client hitting its limit must not affect another.
You: Is the limit the same for everyone?
Interviewer: There is a default, but some clients get their own limit. Paying customers get more, and we sometimes block an abusive client completely.
You: When a request is rejected, does it still count against the client?
Interviewer: No. Only allowed requests use up the budget.
You: Should the limiter read the clock itself?
Interviewer: Pass the time in with each call. It makes the logic testable.
You: Will several request threads call it at the same time?
Interviewer: Yes, this runs inside a busy web server. It has to be correct under concurrency.
You: Do callers need to know how much budget is left?
Interviewer: Yes, we want to send it back in a response header.
The fourth answer is easy to skip and changes real code. If rejected requests counted, a client that keeps retrying in a tight loop would lock itself out forever; with our rule it gets back in as soon as its old requests age out.
💡 Interview tip: Ask "per what?" early. A limit is always per some key (client, user, IP, endpoint). Once you know the key is just a string, the design becomes "one small limiter per key", and the rest follows.
Final Requirements
allow(client_id, timestamp)returns whether this request may go through, and counts it if so.- Every client has its own independent budget of
limitrequests perperiodtime units. - The algorithm (fixed window, sliding window or token bucket) is chosen when the limiter is created, and adding another one must not change the rest of the code.
- A default limit applies to all clients;
set_limit(client_id, limit)gives one client its own limit. A limit of 0 blocks the client. - Rejected requests are not counted.
remaining(client_id, timestamp)returns how many more requests the client could make right now, without using any.- Time is passed in with every call and never goes backwards.
- It must be safe to call from many threads at once.
Out of scope
- Sharing limits across several servers (we discuss it in Extensibility).
- The HTTP layer itself: status codes, headers and retries are the caller's job.
- Different limits per endpoint, and weighted requests that cost more than one unit.
- Persisting counters across restarts.