Skip to content
Preptima
mediumCodingDesignMidSenior

Design a rate limiter that allows N requests per client per minute. Write the class.

A rate limiter machine coding solution should reject fixed-window boundary bursts, usually with a token bucket. It needs no per-request history and stores two numbers per client, but only if refill is computed lazily from a timestamp rather than by running a background thread. Use this rate limiting answer to show the decision, trade-off, and evidence rather than a memorised definition.

4 min readUpdated 2026-08-15

Answer

Short answer

For a rate limiter machine coding round, a token bucket is usually the best in-memory answer. A fixed window is simple but allows a client to send twice the intended rate across a boundary; a token bucket stores the current token count and last-refill timestamp per client, refills lazily on each request, and consumes atomically. Mention per-client eviction and the fact that this in-memory design is per process, not global across a fleet.

Was this answer helpful?

Likely follow-ups

  • Show me the request sequence that lets a client send twice the limit through a fixed window.
  • How does this change when there are twelve instances behind a load balancer?
  • A client is over its limit. What do you return, and what header do you set?
  • How would you support a burst allowance of 20 on a sustained rate of 5 per second?

Related questions