Skip to the guide

Rate limiting algorithms

IntermediateConcept3 pages

A rate limiter decides, for each request, whether this caller has sent too many lately. Interviewers ask which algorithm you'd pick, where the counters live, and what happens at the edge of a time window.

Tokens at an arcade

An arcade gives each child a cup that holds at most 10 tokens, and every cup stands on one shelf behind the counter. Whichever door a child comes in by, the staff there check that child's cup before the child reaches the games. An attendant walks round once a minute and drops a token into every cup that isn't full, and each game costs a token. A child who arrives with a full cup can play 10 games straight away and one a minute after that, while an empty cup means "wait".

  • a child → a caller: an API key, a user, an IP address
  • the child's cup → that caller's bucket
  • the staff at each door → the gateway instances, and the shelf they all check → Redis
  • the attendant's round → the refill rate, the long-run limit
  • the size of the cup → the burst allowed after a quiet spell
  • a game → a request reaching the Orders API; an empty cup → 429 Too Many Requests