Sub topic 2
Clarifying requirements: what to limit, how strictly, and for whom
This chapter assumes you know basic HTTP request and response concepts and can read Python. It also assumes you know roughly what a key-value store such as Redis does. Before choosing an algorithm, you need to pin down what the limiter must do. Most rate limiter bugs trace back to requirements nobody wrote down.
Three questions to answer first
Every limiter design starts with three questions.
- What are you limiting? Requests per second is the usual answer. Sometimes the real cost is elsewhere: bytes uploaded, expensive search queries, or login attempts. A limit counts units of cost, so decide what one unit is.
- For whom? The limit applies to a key. The key might be a user, an API key, an IP address, or an endpoint. The next chapters cover this choice in detail. For now, note that the key decides who is affected when a limit triggers.
- How strictly? Some limits protect against abuse and can be approximate. Others enforce a paid quota and must be exact.
Writing a limit policy
A limit policy is a short, testable statement. A good one names the key, the rate, the burst allowance, and the window. For example:
- Each API key may make 100 requests per minute to
/search. - Each API key may burst up to 20 requests at once.
- Each IP address may attempt 5 logins per 15 minutes.
Vague goals such as "stop abusive clients" cannot be tested. The policy lines above can.
Functional and non-functional requirements
Functional requirements describe the behavior clients see. Non-functional requirements describe how the limiter must run.
| Kind | Question | Example answer |
|---|---|---|
| Functional | Are bursts allowed? | Yes, up to 20 requests |
| Functional | What does a rejected client see? | HTTP 429 with retry information |
| Non-functional | How much latency can the check add? | Under 2 ms at p99 |
| Non-functional | How accurate must counts be? | Within 5% of the limit |
| Non-functional | What happens if the limiter breaks? | Allow traffic (fail open) |
The last row matters more than it looks. A limiter sits on the path of every request, so its own failure can cause an outage. Decide the failure behavior up front.
How requirements steer the design
Each answer narrows your choices later in the tutorial.
- "Bursts are fine" points toward a token bucket.
- "Exact quotas for billing" points toward a sliding window log or an atomic counter.
- "Many servers share one limit" requires shared state, which adds atomicity and clock concerns.
- "Tiny memory per client" argues against storing a timestamp for every request.
Common mistakes
Two other mistakes are common. The first is choosing one global limit when different endpoints have very different costs. The second is leaving out the response contract, so clients cannot tell when to retry.
Exercise
A public API must stop one customer from sending more than 1,000 requests per hour, because each customer pays for that quota. Which requirement answer is most important here, and why?
Show answer
How strictly. The quota is paid for, so counts must be accurate and the limit enforced exactly. This favors an exact method over a cheap approximation, even if it costs more memory.