Rate limiting, step by step
Rate limiting decides how many requests each client may send in a given time. Follow an API as traffic grows and see why each piece is needed.
3 steps · about 5 minutes · free, no sign-up
Step 1: One greedy client
One buggy script sends 5,000 requests a second.
One client floods the API and everyone else gets slow answers. What do you do?
- Buy a bigger API server (Helps for a while): The server copes for a while. Then the script just sends more, and the flood returns.
- Limit requests per client (Right fix): Each client gets a fixed allowance. The greedy script gets 429 errors, and everyone else is fine. Rate limiting: A rate limit caps how many requests one client can make in a time window. Extra requests get a 429 "Too Many Requests" reply.
- Ask clients nicely to slow down (Not this one): A buggy script doesn't read your emails. Nothing enforces the rule, so the flood continues.
Step 2: Bursts at the limit
Real users click in bursts. The strict limit blocks them.
A page load fires 10 requests at once, and the limit is 5 per second. Honest users get blocked. Fix?
- Remove the limit (Not this one): Bursts work again, but so does the greedy script. You are back to step one.
- Use a token bucket (Right fix): Tokens refill steadily and can pile up to a cap. Short bursts spend saved tokens, while a long flood runs dry. Token bucket: A bucket fills with tokens at a steady rate, up to a maximum. Each request spends one token, so bursts are allowed but the average rate is capped.
- Raise the limit to 1,000 per second (Helps for a while): Bursts fit, but a greedy client can now send 200 times more. The limit no longer protects you.
Step 3: Many API servers
You scaled to many servers. Each counts on its own.
Each server keeps its own count, so a client gets the full limit on every server. The limit is really 5x higher. Fix?
- Divide the limit by the number of servers (Helps for a while): It works if traffic spreads evenly. But servers come and go, and uneven traffic blocks honest users.
- Keep counters in one shared fast store (Right fix): Every server checks the same counter, so a client's total is counted once, no matter which server it hits. Distributed rate limiting: Servers share one fast in-memory store, like Redis, to count requests per client. The limit then holds across the whole fleet.
- Block clients permanently after one mistake (Not this one): Honest users get locked out forever and the real problem, uncoordinated counts, stays.
What you built
- Rate limit: Stops one client from using everything.
- Token bucket: Allows short bursts while capping the average rate.
- Shared counters: Make the limit hold across many servers.
- API gateway: One front door where limits are enforced before servers do work.
| Problem | Fix | Trade-off |
|---|---|---|
| One client floods the API | Per-client rate limit | Needs a way to identify clients |
| Honest bursts get blocked | Token bucket | Must tune rate and bucket size |
| Many servers count separately | Shared counter store | Extra network hop; the store must stay fast and available |
- Lesson: microservices and API gateways
- Lesson: from 1 user to 1 million
- Practise: design a rate limiter with an AI interviewer
Frequently asked questions
What is rate limiting?
It caps how many requests a client can make in a time window, so no one can overload a service.
What does HTTP 429 mean?
"Too Many Requests". The client went over its limit and should retry later, often after the time in a Retry-After header.
Token bucket or fixed window?
A fixed window is simpler but lets double bursts happen at window edges. A token bucket allows controlled bursts and is smoother.
Why not limit inside each API server?
With many servers, each counts separately, so the real limit multiplies. A shared store or gateway keeps one count.