Skip to main content

Designing Rate Limiting and Throttling

1. Designing Rate Limiting Algorithms

AlgorithmProsCons
Fixed windowSimple, low memoryEdge bursts at boundaries
Sliding logAccurateMemory per request
Sliding window counterGood balanceApproximate
Token bucketAllows burstsTune burst + rate
Leaky bucketSmooths outputNo burst

2. Designing Fixed Window Rate Limiting

Example: Redis fixed window

key = "rl:{user}:{minute}" INCR key EXPIRE key 60 NX if value > LIMIT then deny

3. Designing Sliding Window Rate Limiting

VariantDetail
Sliding logZSET of timestamps; trim old
Weighted counterprevious*overlap + current
MemoryCounter ≪ log

4. Designing Distributed Rate Limiting

ApproachDetail
Central RedisAtomic INCR / Lua script
Local + syncEach node holds quota slice
GossipEventually consistent counters
Envoy global ratelimitgRPC service
Consistency vs latencyTrade-off explicitly

5. Designing Rate Limit Response Headers

HeaderMeaning
X-RateLimit-LimitQuota
X-RateLimit-RemainingRemaining
X-RateLimit-ResetEpoch when reset
Retry-AfterSeconds (with 429)
RFC 9239 standardRateLimit-Policy / RateLimit fields

6. Designing Per-User Rate Limiting

StrategyDetail
Key by user_idAuthenticated users
Tier-basedFree vs paid limits
Multiple windowsreq/sec, req/min, req/day combined

7. Designing Per-IP Rate Limiting

ConcernDetail
NAT / shared IPsSet higher limit; combine with cookie
IPv6Limit by /64 prefix
Trusted proxyUse X-Forwarded-For correctly

8. Designing API Quotas and Tiers

TierLimits
Free60 req/min, 10K/day
Pro1K req/min, 1M/day
EnterpriseCustom, SLA-backed
OveragePay-per-use vs hard cap

9. Designing Graceful Degradation Under Load

StrategyDetail
Shed non-criticalDisable analytics, recommendations
Serve staleFrom cache
Reduce featuresRead-only mode
Queue + 202Defer heavy work

10. Designing Backpressure Mechanisms

MechanismDetail
Bounded queuesReject when full (fail fast)
Reactive streamsDemand-driven
Concurrency limitsSemaphore per dependency
Adaptive concurrency (AIMD)Netflix concurrency-limits

11. Designing Adaptive Rate Limiting

SignalAction
Latency risingTighten limits
Error rate upThrottle harder
CPU / queue depthScale or shed
Per-user anomalyStep-down that user

12. Designing Rate Limit Bypass Strategies

Use CaseMechanism
Internal servicesAllow-list by mTLS identity
VIP customersHigher tier or no limit
Health checksExcluded paths
AuditAlways log bypass usage

Was this article helpful?