System Design · step by stepDesign a Rate Limiter
Step 1 / 9
RUN IT YOURSELF

The token bucket, in Python & TypeScript

The most common rate limiter is a token bucket. Here it is in both languages, running live in your browser. Switch tabs, read the comments, edit the capacity/refill, and hit Run.

HOW TO READ THE CODE — 4 IDEAS
  1. A bucket holds up to capacity tokens and refills at a steady rate.
  2. Before each check, add tokens for the time elapsed since last time (step 1).
  3. A request spends one token; if one is available it passes (step 2).
  4. An empty bucket rejects the request — that is the rate limit biting (step 3). Capacity sets the burst size.
CPython · WebAssembly
built to be throttled, not memorized — make the calls, drop Redis, run the gauntlet.
Finished this one? 0 / 28 System Designs done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More System Designs