KatInterview

Building a Global Rate Limiting System

System DesignModel answer
MediumModel answerSystem Designdistributed-systemsrate-limitingscalability

Last updated

Design a low-latency rate limiting system that enforces usage quotas across distributed API gateways while maintaining availability and handling failures gracefully.

Problem Context

You're building a rate limiting system for a globally distributed API platform serving machine learning inference requests. The system must protect backend resources while enforcing per-customer usage policies across multiple regions without introducing significant latency.

Your Task

Design a rate limiting solution that operates within these parameters:

What the System Must Do

  • Limit requests based on different identifiers: API keys, user IDs, and source IP addresses
  • Support multiple time windows (per-second, per-minute quotas)
  • Provide standard feedback through HTTP headers (X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset)
  • Return HTTP 429 status with retry information when thresholds are exceeded
  • Allow configurable behaviors: reject immediately, queue briefly, or fail open/closed during outages

Performance & Scale Targets

  • Latency: Add less than 5ms overhead at p99
  • Availability: System failures must not block API traffic
  • Throughput: Support 100,000 requests/second globally
  • Scale: Manage 5 million active rate limit policies
  • Geography: Three regions (US East, US West, Europe)

Expected Design Elements

Your design should address:

  1. Algorithm choice: Evaluate different rate limiting algorithms for memory efficiency and precision
  2. Storage architecture: Select appropriate datastores and explain how atomic updates work
  3. Cross-region coordination: Show how global limits work without cross-region latency on each request
  4. Fault tolerance: Describe behavior during datastore outages, network splits, and traffic surges

Hints

Hint 1

Consider algorithms that work with constant memory per limit rule rather than storing individual request timestamps

Hint 2

Think about how to partition quota allocation across regions so local checks remain fast

Hint 3

Design for the common case where the datastore is healthy, but have a fallback that maintains some protection when it's not