🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Design a Rate Limiter

System Design ProblemsRate LimitingđŸŸĸ Free Lesson

Advertisement

System Design Problems

Design a Rate Limiter

A rate limiter controls the rate of requests a client can send to an API. It protects services from abuse, ensures fair resource usage, and prevents cascading failures. Systems like Cloudflare, AWS API Gateway, and Kong implement distributed rate limiting at massive scale.

  • Protection — Prevent DoS attacks and API abuse
  • Fairness — Ensure equitable resource allocation across clients
  • Throttling — Gracefully degrade service under overload

A rate limiter sits between the client and the server, deciding whether to allow or reject each request based on the client's recent request history.

Requirements

Functional Requirements

  • Limit requests per client per time window
  • Support different rate limits per API endpoint
  • Support different limits per client tier (free, pro, enterprise)
  • Return HTTP 429 (Too Many Requests) when limit exceeded
  • Provide rate limit headers in responses
  • Support distributed rate limiting across multiple servers

Non-Functional Requirements

  • Latency: Rate limit check in < 1ms
  • Accuracy: Allow slight over-limit (within 1%) rather than blocking valid requests
  • Availability: Rate limiter failure should fail open (allow requests)
  • Scalability: Handle millions of clients and thousands of API endpoints

API Design

Architecture Diagram
GET /api/v1/rate-limit/status
Response: {
  "client_id": "user_123",
  "limit": 1000,
  "remaining": 742,
  "reset_at": "2026-06-20T11:00:00Z",
  "retry_after": 0
}

// Response headers
X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 742
X-RateLimit-Reset: 1687267200
Retry-After: 30  // Seconds until next request allowed

Rate Limiting Algorithms

Token BucketBucket of TokensAdd tokens at fixed rateRemove 1 per requestBurst-friendlySliding Window LogLog of Request TimestampsCount in windowEvict old entriesMemory-intensiveFixed WindowCounter per WindowSimple implementationReset at boundaryBoundary spike issueSliding WindowCounterWeighted AverageSmooth boundaryLow memoryBest balance ✓

Token Bucket

Sliding Window Counter

Algorithm Comparison

AlgorithmMemoryAccuracyBurst HandlingComplexity
Token BucketO(1)GoodAllows burstsSimple
Sliding Window LogO(n)ExactNo burstsComplex
Fixed WindowO(1)Boundary issuesAllows burstsSimplest
Sliding Window CounterO(1)GoodSmoothModerate

Distributed Rate Limiting

Single vs Distributed

For single-server rate limiting, an in-memory counter suffices. For distributed systems, use Redis:

ClientServer 1Server 2RedisAtomic CountersAPIDistributed rate limiting with Redis

Race Condition Handling

In distributed systems, race conditions can allow more requests than the limit:

Practice Exercises

  1. Design: How would you implement rate limiting that accounts for different API endpoint costs (e.g., a search query costs 5 units, a simple GET costs 1 unit)?

  2. Distributed: If you have 10 API servers and a rate limit of 1000 requests/minute per client, what is the worst-case over-limit due to race conditions? How would you mitigate it?

  3. Trade-offs: Compare token bucket and sliding window counter for an API that needs to support occasional bursts (10x normal rate for 5 seconds).

  4. Edge Case: How would you handle rate limiting for a client that uses multiple IP addresses (e.g., behind a NAT)? Design a client identification strategy.


What to Learn Next

-> Rate Limiting Deep dive into rate limiting algorithms and distributed implementation.

-> Load Balancing Distributing requests across multiple API servers.

-> API Design REST conventions, error handling, and HTTP status codes.

-> Caching Strategies Redis caching patterns for distributed counters.

-> Proxy and Reverse Proxy Rate limiting at the proxy layer (nginx, Envoy).

-> Microservices Service mesh rate limiting and circuit breaking.

Need Expert System Design Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement