Design a Rate Limiter

MediumInfrastructureDistributed SystemsRedisAlgorithmsAPI DesignAbout 35 minutes

Design a distributed rate limiter that can be used as middleware for an API gateway. It should support per-user and per-endpoint limits, work across multiple server instances, and add minimal latency to each request.

How the interview goes

Five phases, and questions the interviewer is likely to ask in each.

  1. 1. Requirements

    • Should this be per-user, per-IP, per-endpoint, or all three?
    • What's the time window — per second, per minute, per hour?
    • Do different API tiers have different limits?
    Show a hint

    Consider whether this needs to work across multiple data centers.

  2. 2. High-level design

    • How do multiple API server instances share rate limit state?
    • Which algorithm are you using — token bucket, fixed window, sliding window?
    • Where in the request lifecycle does rate limiting happen?
    Show a hint

    Redis with atomic Lua scripts is a common approach for distributed counters.

  3. 3. Deep dive

    • How do you handle the race condition when two requests arrive simultaneously?
    • What's the trade-off between fixed window and sliding window counter?
    • How do you implement token bucket in Redis atomically?
    Show a hint

    The token bucket algorithm allows bursting; fixed window is simpler but has boundary effects.

  4. 4. Scaling

    • What happens if Redis goes down?
    • How do you handle clock skew between servers?
    • How would you scale to 1M requests per second?
    Show a hint

    Lua scripts in Redis provide atomicity for counter increments without locking.

  5. 5. Trade-offs

    • Fail open vs fail closed: what are the implications?
    • What's the trade-off between precision and performance in the sliding window?
    Show a hint

    Sliding window log is most accurate but expensive; sliding window counter is a good middle ground.

Practice this with an AI interviewer

Write your design doc with live diagrams while the interviewer asks one question at a time and pushes on your choices. Get a scored debrief with what to study next.

Start this interview