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.
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
- Build an in-memory pub-sub with topics, many subscribers per topic and at-least-once delivery. How does a message get redelivered?hardAlso on machine-coding and concurrency6 min
- Design an in-memory job scheduler that runs one-off, delayed and recurring jobs. Write the core classes.hardAlso on machine-coding and concurrency6 min
- Design a movie ticket booking system with shows, seats and a temporary hold that expires if the user does not pay. Write the classes.hardAlso on machine-coding and concurrency6 min
- One tenant bursts to ten times their normal traffic and every other customer's latency doubles. Your global rate limit was never hit. How would you design for fairness instead?hardAlso on rate-limiting and token-bucket5 min