Sub topic 1
Why Services Need Rate Limiters
Prerequisites
This chapter assumes you know the basics of HTTP requests and responses. It also assumes you can read short Python snippets, and that you have a general idea of how services run behind a load balancer. Later chapters use a key-value store such as Redis, but this chapter does not.
What a rate limiter does
A rate limiter caps how many requests a client may make to a service in a given period. When a client goes over the cap, the service rejects or delays the extra requests instead of processing them. Rejection is usually an HTTP 429 response, which later chapters cover in detail.
The limiter does not make the service faster. It decides which work the service accepts, so that the work it does accept finishes reliably.
The goals a limiter serves
Teams add rate limiters for several different reasons. Each reason leads to different design choices later.
- Protect capacity. Every service has a ceiling on the load it can handle. Without a limit, a traffic spike can exhaust threads, connections, or database capacity and slow down every user.
- Keep sharing fair. One heavy client should not starve the others. A per-client limit keeps a noisy neighbor from taking the whole capacity.
- Contain bugs and abuse. A client stuck in a retry loop can send thousands of requests per second. Credential stuffing and scraping produce similar floods. A limit caps the damage from both.
- Control cost. Some requests are expensive, such as calls to a paid downstream API or a large model. A limit puts a ceiling on spend.
- Enforce a product plan. A free tier may allow 1,000 requests per day and a paid tier 100,000. The limiter enforces that quota.
A request flow with a limiter
The diagram shows where the limiter sits in the path of a request. It checks the request before any expensive work happens.
What a limiter is not
A rate limiter is not a full defense against attacks. A large distributed denial of service attack can saturate your network before any request reaches your limiter. It is also not a replacement for capacity planning. If normal traffic exceeds what you can serve, limits only decide who gets turned away.
The simplest possible limiter
Here is a deliberately naive limiter. It allows 3 requests in total per client and then blocks that client permanently.
counts = {}
LIMIT = 3
def allow(client_id):
counts[client_id] = counts.get(client_id, 0) + 1
return counts[client_id] <= LIMIT
print([allow("alice") for _ in range(5)]) # True, True, True, False, FalseThis shows the core idea: keep a counter per client and compare it with a limit. It fails in practice for three reasons. The counter never resets, so a client is blocked forever. The dictionary lives in one process, so several servers behind a load balancer would each keep their own count. The dictionary also grows with every new client and is never cleaned up. The rest of this tutorial fixes these problems in turn.
Summary of the design pressures
| Pressure | Question it raises |
|---|---|
| Protect capacity | How high can the limit safely be? |
| Fairness | Who is the "client" that gets a limit? |
| Bursts | Should short spikes be allowed? |
| Many servers | Where is the shared count stored? |
| Failure | What happens if the counter store is down? |
Exercise
Name two reasons the naive limiter above would not work for a service running on several servers, and say which of the five goals above a per-client limit mainly serves.
Show answer
Each server keeps its own in-memory counts, so a client could send up to the limit to every server, and the counter never resets, so clients stay blocked forever. A per-client limit mainly serves fairness, since it stops one client from taking the shared capacity. It also helps protect capacity.