Skip to content

Rate Limiting

Every service has a rate it can sustain, and some clients will send more: by a bug, by greed, or on purpose. A rate limit is a rule that refuses what is above the allowed rate, so that one client cannot use up what the others need. The rule is simple to say and has more than one way to be counted, and each way lets a different thing through. This module is about those counting methods, what each one stores and what it gets wrong, and about the two questions that appear when a service has more than one server: who is counting, and where the check belongs.

You will work with a live rate limiter lab that runs five limiters on the same traffic, with a sorting exercise, a quiz and a C# example of a token bucket, and every lesson has a moment where you change the bucket, the window or the number of gateways and read what gets through.

  1. The token bucket: a rate and a burst (18 min). What a rate limit refuses and what a client is told, and how the size of a bucket decides the burst that gets through.
  2. The leaky bucket: holding the burst back (16 min). The meter and the queue, and why a queue gives the service a constant rate at the price of waiting.
  3. Fixed windows and the boundary burst (16 min). The cheapest counter, and the burst on each side of a boundary that passes twice the limit.
  4. Sliding windows: exact, or estimated (22 min). The log that keeps every request, the counter that keeps two numbers, and the memory each one costs.
  5. Distributed limits: who is counting? (22 min). Counters on each gateway, one shared counter, and a shared counter that is read late.
  6. Where to enforce a limit: gateway or service (20 min). What the edge, the gateway, the service and the client each know, how to choose a key, and what to tell a refused client.
  7. Recap and quiz (12 min). The module in one page, a design checklist, and a quiz.
  • Describe a token bucket by its rate and its size, and say how much of a burst it lets through.
  • Say what a leaky bucket does as a meter and as a queue, and which one changes what the service behind sees.
  • Show how a fixed window lets twice its limit through, and what the sliding window log and the sliding window counter do instead.
  • Say how many requests a limit really allows when several gateways count on their own, with a shared counter, and with a shared counter that is read late.
  • Choose where to enforce a limit and by which key, and say what a refused client should be told.

All the numbers in the lab are illustrative. They are for comparing algorithms, not for forecasting any real product.

Built by GhaziAlibi.

© ShardCraft. All rights reserved.