Skip to content
We areTOP 1%

Design a rate limiter

MediumConceptWeb Services
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

Requirements: limit how many requests a client (identified by user ID, API key or IP) can make in a time window, return HTTP 429 when the limit is exceeded, add minimal latency, and work across many servers.

  • Algorithms: token bucket allows controlled bursts and is the most common; leaky bucket smooths output to a fixed rate; fixed window counters are simple but allow double bursts at window edges; sliding window log is exact but stores every timestamp; sliding window counter approximates it cheaply.
  • Placement: in an API gateway or middleware in front of the application servers.
  • Distributed counting: keep counters in Redis and update them atomically with INCR plus expiry, or with a Lua script.
  • Trade-off: a central store gives accurate global limits but adds a network hop; local counters are faster but only approximate.

More in Web Services

Web Services questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Design a URL shortener
MediumConcept
ConceptMedium Read the answer to Design a URL shortener
2Design a pastebin service
MediumConcept
ConceptMedium Read the answer to Design a pastebin service
3Design a rate limiter
MediumConcept
ConceptMedium Read the answer to Design a rate limiter
4Design a notification system
HardConcept
ConceptHard Read the answer to Design a notification system

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account