Skip to main content
Abstract Algorithms Logo
Abstract Algorithms
← All ProblemsΒ·system designMedium~30 min

Design a Distributed Rate Limiter

Design a rate limiter for a public API that enforces per-user and per-IP request limits at scale. The service must handle high throughput, be deployable across multiple data center regions, and gracefully degrade when the rate-limiting backend is unavailable.

Requirements

  • βœ“Enforce per-user and per-IP rate limits (e.g., 1,000 requests/minute per user).
  • βœ“Return a clear 429 response with retry-after headers when limits are exceeded.
  • βœ“Support multiple limit windows (per-second, per-minute, per-hour).
  • βœ“The limiter must work across multiple API server instances.
  • βœ“Degrade gracefully: if the rate-limit store is unreachable, choose fail-open or fail-closed based on product policy.

Constraints & Scale

  • β€”API receives ~500,000 requests/second at peak.
  • β€”Rate limit decisions must add ≀ 2 ms to request latency (p99).
  • β€”The limiter must be consistent across 10+ API nodes in a region.
  • β€”Configurable limits: different limits per API key tier (free, pro, enterprise).
  • β€”No shared memory between API processes β€” distributed counter required.
Understand
Guided mode

Identify the key requirements and constraints. What algorithms are candidates for rate limiting? What does 'distributed' add to the problem compared to a single-server limiter?

Context & Constraints β€” Human Reviewed

Core tension: single-server counters don't work across nodes; distributed counters add network round-trips. The 2 ms latency budget is tight β€” a Redis call is typically 0.5–1 ms in the same data center. Algorithm choice (token bucket, fixed window, sliding window log, sliding window counter) affects accuracy, memory, and implementation complexity.

Trade-offs

  • βš–Fixed window: simple, low memory, but allows 2Γ— burst at window boundaries.
  • βš–Sliding window log: accurate, but memory grows with request count per user.
  • βš–Sliding window counter (hybrid): accurate within ~0.5% error, O(1) memory β€” best for most production systems.
  • βš–Token bucket: handles bursts naturally; refill rate maps to sustained limit.

Concepts used in this stage