Skip to content
RED SAGSkills
mediumBackend Developer interview question

How would you add rate limiting to a public API that runs on many servers?

What the interviewer wants to hear

  • Decide what to limit by: API key, user, IP, and per endpoint cost
  • Choose an algorithm such as token bucket or sliding window
  • Shared state in Redis with atomic operations or Lua scripts
  • Return 429 with Retry-After and rate limit headers
  • Fail open or closed decision, and limits at the edge or gateway

Model answer

First I would decide the key: usually the API key or user id, with IP limits for anonymous traffic, and stricter limits on expensive endpoints like search or login. For the algorithm I prefer a token bucket, which allows short bursts but enforces an average rate, or a sliding window counter, which avoids the double burst at fixed window edges. Because there are many servers, the counters must be shared, so I would keep them in Redis and update them atomically with a Lua script or INCR plus EXPIRE. When a client is over the limit I return 429 with a Retry-After header, and on every response headers showing the limit and remaining quota. I would also decide what happens if Redis is down; for most APIs I fail open with a local in-memory limit. Coarse limits can also live at the API gateway or CDN to stop floods early.

Can you answer it under time pressure?

Take a timed medium round and get scored on every answer.

Start mock interview

More medium Backend Developer questions