Rate limiting: recap and quiz
The module in six sentences
Section titled “The module in six sentences”- The token bucket. A bucket refilled at the rate and holding the burst: the rate is the sustained speed, the size is what a quiet client can spend at once.
- The leaky bucket. As a meter it is a token bucket seen from the other side; as a queue it releases requests at a constant rate and turns the burst into waiting.
- Fixed windows. One count for each block of time, reset at the boundary, so a burst on each side of a boundary passes twice the limit.
- Sliding windows. The log is exact and stores a timestamp for every accepted request, and the counter stores two numbers and estimates the window.
- Distributed limits. Counters on each gateway multiply the limit by the number of gateways, a shared counter holds it, and a shared counter read late overshoots it.
- Where to enforce. The gateway knows who is asking and the service knows what it costs, a limit for each client does not bound the load on the service, and a refused client should get a 429.
How each one goes wrong
Section titled “How each one goes wrong”| Idea | What breaks | What you see | What helps |
|---|---|---|---|
| Token bucket | A bucket bigger than the service can take at once | A burst the size of the bucket reaches the service | A smaller bucket, or a queue in front |
| Leaky bucket | A queue longer than clients will wait | Requests that time out in line | A short queue and a fast refusal |
| Fixed window | A burst on each side of a boundary | Twice the limit in a few seconds | A sliding window, or accept it for a quota |
| Sliding window | The log’s memory, or the counter’s error | Large state per client, or a few requests over the limit | The counter for scale, the log for strictness |
| Distributed | Local counters, or a late read | The limit multiplied, or overshot | A shared counter read fresh, a local limit in front |
| Enforcement | Only per-client limits | The service overloaded with every client in its limit | A limit for the service, a concurrency limit, load shedding |
A checklist for a rate limit
Section titled “A checklist for a rate limit”Each question points back to the lesson that answers it.
- What is the limit protecting? One client’s fairness, the service’s capacity, a bill, or a dependency. (Where to enforce)
- What is the key? User, API key, address or path, and where in the request path it is known. (Where to enforce)
- Should a quiet client be able to burst? Then the size of the bucket is the burst you accept. (Token bucket)
- Does the service behind need a constant rate? Then hold the burst back in a short queue. (Leaky bucket)
- Is it a quota or a protection? A quota can use a fixed window, a protection from spikes cannot. (Fixed window)
- How strict must the window be, and how much memory can you spend? (Sliding windows)
- How many servers count, and do they share the count? Say what happens when the shared store cannot be reached. (Distributed limits)
- What does a refused client get? A 429 with a retry delay, and the RateLimit headers only as a hint, since they are a draft. (Where to enforce)
- What protects the service when every client is within its limit? (Where to enforce, and overload and load shedding)
Check your understanding
Section titled “Check your understanding”Ten questions cover the whole module. Every option is explained, whether it is right or wrong.
Rate limiting: module quiz
This quiz needs JavaScript to check your answers.
Where this goes next
Section titled “Where this goes next”A rate limit decides what to refuse. The next module, observability, is about seeing what a system is doing, which is how you find out that a limit is too tight for honest clients or too loose for a dangerous one. Ask this module’s questions when you meet it: what is counted, who counts it, and what a refused client is told.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.