core practice · implementation task

Implement a token-bucket rate limiter

Implement a token bucket: tokens refill at a fixed rate, each request spends one, an empty bucket returns 429 with Retry-After.

The mechanism

Rate limiting protects a shared resource from any one caller. Token bucket is the workhorse: tokens refill at a steady rate, each request spends one, an empty bucket means 429 — allowing bursts up to the bucket size.

Study Rate Limiting

Expected evidence

Allows bursts up to bucket size, then throttles to the refill rate.

Try this drill in the practice workspace →

Hints when you need them
  • Compute refill lazily from elapsed time.
  • Set Retry-After from time-to-next-token.

Watch for these mistakes

  • Fixed-window counters that allow 2x burst at the boundary
  • Limiting per-server instead of globally in a cluster
  • No Retry-After header on 429 responses

Check the sources

Browse all practice briefs