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

Rate Limiting

InfrastructureTraffic ManagementđŸŸĸ Free Lesson

Advertisement

Infrastructure

Rate Limiting

Rate limiting controls the rate of requests a client can send to a service. It protects against abuse, ensures fair resource usage, and maintains system stability under load.

  • Protection — Prevent overload and denial-of-service attacks
  • Fairness — Ensure equitable resource allocation
  • Stability — Maintain predictable performance under load

Rate limiting is the first line of defense for any production service.

Why Rate Limit?

Uncontrolled traffic can cascade failures through a system. Rate limiting provides a controlled degradation path.

Types of Limits

Limit TypeDescriptionExample
Per-clientLimit per individual user/IP100 requests/minute per user
GlobalSystem-wide limit10,000 requests/second total
Per-endpointLimit per API endpoint10 writes/second per user
TieredDifferent limits per planFree: 100/min, Pro: 1000/min

Token Bucket

The most widely used rate limiting algorithm.

Token Bucket AlgorithmBucket (C=10)7/10 tokens used+r/secrequestAllowRejectHTTP 429

Sliding Window Log

Tracks exact timestamps of each request in a sorted set.

Sliding Window Counter

A hybrid approach combining fixed windows with interpolation.

Fixed Window

The simplest approach: count requests in fixed time periods.

Distributed Rate Limiting

Rate limiting across multiple service instances.

Distributed Rate Limiting with RedisInstance 1Instance 2Instance NRedisINCR user:123EXPIRE 60sAtomic opsallow/denyHTTP 200 / 429

Distributed Challenges

ChallengeSolution
Race conditionsUse atomic operations (Redis INCR with EXPIRE)
Network latencyLocal rate limiting with periodic sync
Redis failureFallback to local limiting or fail open
Clock skewUse logical timestamps or server-side time

Algorithm Comparison

AlgorithmAccuracyMemoryBurst HandlingComplexity
Token BucketHighO(1)Allows bursts up to CLow
Sliding Window LogExactO(n) per clientNo burstsMedium
Sliding Window CounterApproximateO(1)SmoothLow
Fixed WindowLowO(1)Boundary burstsVery Low
Leaky BucketHighO(1)Smooth outputLow

Practice Exercises

  1. Design: Design a rate limiting system for an API gateway handling 1M requests/second with per-user limits of 100 requests/minute. Include Redis architecture and failover.

  2. Analysis: Compare token bucket and sliding window counter for a video streaming service that allows 10-second bursts of high bitrate.

  3. Distributed: Implement distributed rate limiting using Redis with a Lua script. Handle the case where Redis is unavailable.

  4. Edge Case: A user has a rate limit of 10 requests/minute. They make 10 requests at 12:00:59 and 10 more at 12:01:01. How does each algorithm handle this?


What to Learn Next

-> Proxy and Reverse Proxy Forward proxy, Nginx, HAProxy, and SSL termination.

-> API Design REST, GraphQL, gRPC, and API gateway patterns.

-> CDN Edge caching, DNS routing, and content distribution.

-> Load Balancing Distribution algorithms and L4 vs L7 load balancing.

-> Security Patterns Authentication, authorization, encryption, and mTLS.

-> Observability Logging, metrics, tracing, and monitoring.

Need Expert System Design Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement