Design a rate limiter
Design a rate limiter for a public API served from many machines across several regions. Each customer has a plan with a request quota. The limiter must not become the reason the API is slow or down.
About this problem
“Design a rate limiter” is a core-level problem in the limits and abuse family. Deciding whether to say no, cheaply, on every request.
The key challenge: No time slack at all, because the decision sits in front of every request. But a lot of accuracy slack, and the design is built on that trade.
Time budgets: Decision: ~1 ms. Accuracy: generous.
The worked solution for Rate limiter goes through why those budgets shape the design, stage by stage.
Before you start
You will work this in five timed stages. Here is what each one asks you to think about. None of it is the answer; the answers unlock in the workspace below once you have written your own.
- Stage 1 · Requirements · 5 min
- Is the limit global across regions, or per region? That is the question.
- How exact does the limit need to be? Ask, do not assume silently.
- What is the limiter protecting — capacity, or fairness, or abuse?
- Stage 2 · Actors and lifecycle · 6 min
- The system decides things silently here. What are they?
- What happens when the counter store is unreachable?
- What does a rejected client do next?
- Stage 3 · Numbers · 6 min
- How many counter operations per second? Compare to the request rate.
- How much state does one customer's counter need?
- What does a cross-region round trip cost against your budget?
- Stage 4 · High-level design · 12 min
- Which algorithm, and what specifically does it get you?
- Where does the counter live — locally, shared, or both?
- How do many servers agree without talking on every request?
- Stage 5 · Deep dive and tradeoffs · 14 min
- What exactly happens when the shared store dies?
- One customer is 90% of your traffic. What breaks?
- How do you avoid punishing a customer during their busiest hour?
Work it through
Five stages, gated. You write your answer for each stage before its model answer unlocks. That is the core of the method.
Done all five? The diagrams, numbers, trade-offs and follow-ups for this problem are on the worked solution page, to check your own design against.
How it is really built
Write your own answer first. Then read how teams who run this at scale describe it. Their constraints were real, and the write-ups often say what their choices cost.
- Using load shedding to avoid overload ↗The Amazon Builders' Library on rejecting work early, which is what a limiter does.
- Google SRE Book — Handling Overload ↗Graceful degradation when the limit is reached.