Dev Skills

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.

  1. 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?
  2. 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?
  3. 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?
  4. 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?
  5. 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?
Prefer to read first? The worked solution, end to end →

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.

Loading your progress…

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.

Before you leave
Try this problem again in a week without reading anything. Comparing what you write then with what you wrote today shows you what stuck.
Stuck, or new to this?Read the full worked solutionAll five stages end to end, with the diagrams and the costs, written out the way you would say it. Best read after you have had a go.Read it →

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.