Skip to content

Rate limiting: recap and quiz

  1. 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.
  2. 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.
  3. 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.
  4. Sliding windows. The log is exact and stores a timestamp for every accepted request, and the counter stores two numbers and estimates the window.
  5. 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.
  6. 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.
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

Each question points back to the lesson that answers it.

  1. What is the limit protecting? One client’s fairness, the service’s capacity, a bill, or a dependency. (Where to enforce)
  2. What is the key? User, API key, address or path, and where in the request path it is known. (Where to enforce)
  3. Should a quiet client be able to burst? Then the size of the bucket is the burst you accept. (Token bucket)
  4. Does the service behind need a constant rate? Then hold the burst back in a short queue. (Leaky bucket)
  5. Is it a quota or a protection? A quota can use a fixed window, a protection from spikes cannot. (Fixed window)
  6. How strict must the window be, and how much memory can you spend? (Sliding windows)
  7. How many servers count, and do they share the count? Say what happens when the shared store cannot be reached. (Distributed limits)
  8. 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)
  9. What protects the service when every client is within its limit? (Where to enforce, and overload and load shedding)

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.

Question 1 of 10

A token bucket holds 20 tokens, is refilled at 5 tokens a second and starts full. 50 requests arrive in the same instant.

How many are accepted?

Enter a number and choose its unit.

requests
Question 2 of 10

The same bucket, 20 tokens refilled at 5 tokens a second, has just been emptied.

How many seconds until it is full again?

Enter a number and choose its unit.

seconds
Question 3 of 10

A leaky bucket works as a queue of 60 places that drains at 5 requests a second. A burst of 60 requests arrives in one second.

What happens?

Choose one answer.

Question 4 of 10

A colleague says the system uses a leaky bucket, and means a counter that rises with each request and drains at a fixed rate, rejecting a request that would make it overflow. Nothing is queued.

How does it behave next to a token bucket with the same rate and size?

Choose one answer.

Question 5 of 10

A sliding window log protects an endpoint with a window of 10 seconds. One client sends 1,000 accepted requests a second, steadily.

How many timestamps does the log hold for that client?

Enter a number and choose its unit.

timestamps
Question 6 of 10

A sliding window counter uses a window of 60 seconds. The last window counted 80 requests. The current window has counted 10, and it is 15 seconds old.

What is its estimate of the requests in the last 60 seconds?

Enter a number and choose its unit.

requests
Question 7 of 10

Four gateways each enforce a limit of 100 requests a minute for a client, each with its own counter. A balancer spreads the client's requests across them.

What is the most that client can get through in a minute?

Enter a number and choose its unit.

requests
Question 8 of 10

Three gateways share one counter, but each reads it a few seconds late. A client's requests arrive together.

Why is the limit overshot?

Choose one answer.

Question 9 of 10

An API has 1,000 clients, each limited to 10 requests a second at the gateway. The service behind it can handle 2,000 requests a second.

What protects the service if many clients use their whole allowance at once?

Choose one answer.

Question 10 of 10

A client is over its limit.

Which response best tells it what happened and what to do?

Choose one answer.

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.