Design an API Rate Limiter
medium 45 minDecide, in about a millisecond and on every request, whether this caller may go on.
Video lesson · Chapter 1 of 5
One caller asks for the last token
An API rate limiter in 90 seconds
1:25Reel coming soon
Theory
Read or listen first. About 1 min.
What it is
An API rate limiter decides how many requests one caller may make in a stretch of time. Requests over the limit get 429 Too Many Requests and a hint about when to come back.
It sits on the path of every request, so it has two jobs that pull apart: be right about a count that many machines share, and answer so fast that nobody notices it is there.
Why it exists
Without limits, one broken script can use the capacity that everyone pays for. Limits keep use fair, slow down abuse and cap the bill of expensive calls.
They also make plans possible. A free caller gets 100 requests a minute and a premium caller 1,000, enforced in one place rather than in every service.
A short history
1999
RFC 2697 describes a traffic meter built from two token buckets, the idea API limiters reuse.
April 2012
RFC 6585 defines the status code 429 Too Many Requests.
2017
Cloudflare describes counting with two windows and a weight, the sliding window counter.
June 2022
RFC 9110 restates HTTP, including the Retry-After field that tells a client how long to wait.
Words to know
- Key
- What we count: a user, an API key, an address, or a mix such as user plus endpoint.
- Token bucket
- A store of permits that refills at a steady rate up to a maximum.
- Burst
- How many requests may arrive at once: the size of the bucket.
- Lease
- A handful of tokens taken from the shared bucket and spent locally.
- Fail open, fail closed
- Allow everything, or refuse everything, when the limiter cannot decide.
- Hot key
- One caller with so much traffic that a single shard struggles.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Identify. Read the caller from the verified token and pick the rules that match
Understand it
One farm, one card for each visitor, and a plan for the day the hut is closed.
Scope
What are we building?
A service that every API request passes. It picks the rules for the caller, checks a shared count and lets the request through or refuses it. The general ideas have their own pages: rate limiting as a practice, the algorithms and the shared limiter. Here we put them together at a stated size.
Decide
Allow or refuse each request, with a p99 of about a millisecond.
Explain
Headers with the limit, what is left and when to return.
Change
Owners edit rules and they apply within seconds, with no restart.
Survive
A lost shard weakens the limit. It never takes the API down.
Similar systems
| System | What it is |
|---|---|
| AWS API Gateway | Managed gateway with token bucket throttling |
| Cloudflare rate limiting | Rules applied at the edge |
| Kong | Gateway with rate limiting plugins |
| NGINX | Web server with a request limiting module |
| Redis | Store on which many teams build their own |
| Stripe | Payment API that has published how it limits requests |
The finished system
Where the design ends up
This is the last version. The Design tab starts with one server and one database.
One region of the system
Where to read what
| Question | Place |
|---|---|
| How do I open this interview? | Approach |
| How big is it? | Numbers |
| How does it grow? | Design |
| Hot callers, failures, regions? | Deep dives |
| Can I practise it? | Mock |
Summary
The whole topic in one minute.
A rate limiter keeps one small number per caller and asks one question per request. Counting atomically in a shared, sharded memory store makes the answer the same on every gateway.
The rest of the design protects that call: rules live in memory, hot callers use leased tokens, refusals are remembered locally, each region holds a share of the limit, and a bounded local bucket takes over when a shard is gone.
- Reading the count and changing it must be one atomic step.
- A bucket of size B that refills at r a second admits up to B + 60r in a minute.
- Memory is small here. Operations per second decide the number of shards.
- Leased tokens are taken before they are spent, so they cannot exceed the limit.
- Choose the failure behaviour for each rule before the failure.