Home
ArenaGraphSignalTopics
/Distributed Systems Architecture
Chapter 10 • Module 2 5 min breakdown +15 XP Module

Rate Limiting and Traffic Shedding at Scale

From Track:Distributed Systems ArchitectureDistributed Systems & Consensus
Interactive Arena Lab: Build a Distributed Sliding Window Rate Limiter with Lua Scripts

Verify your implementation with live deterministic test suites & earn arena points.

Launch Arena ➔

In a distributed microservices ecosystem, no backend service has infinite capacity.

Without protective boundaries, unexpected traffic spikes, runaway client retry loops, scraping bots, and DDoS attacks will saturate database connection pools and CPU threads, triggering cascading collapse across the entire platform.

To protect system stability, high-scale platforms (Stripe, Cloudflare, Netflix, AWS) implement two complementary layers of defense:

  1. Distributed Rate Limiting: Restricting the rate of requests per client/API key.
  2. Adaptive Load Shedding: Proactively dropping low-priority background work when servers are under severe CPU/memory strain to protect mission-critical transactions.
Interactive Blueprint
Rendering diagram...

1. Rate Limiting Algorithms Compared

text
Loading code editor...

2. The Sliding Window Counter Algorithm

To avoid the boundary burst of fixed windows without paying the high memory penalty of sorted set logs, modern gateways use the Weighted Sliding Window Counter:

text
Loading code editor...

If , the request is allowed. If , it is rejected with HTTP 429 Too Many Requests.


3. Distributed Atomic Execution with Redis Lua Scripts

When running 100 API gateway pods in Kubernetes, tracking limits in local process memory is useless because requests from the same user land on different pods.

To execute rate limiting atomically across servers in without distributed locks, we use an Atomic Redis Lua Script:

lua
Loading code editor...

4. Adaptive Load Shedding: Protecting Saturated Systems

When a massive surge occurs, Rate Limiting alone cannot save your system if the total volume of legitimate users exceeds capacity.

Load Shedding monitors the health of the host machine and proactively rejects low-priority work before the server runs out of memory or CPU threads:

Interactive Blueprint
Rendering diagram...

5. Fail-Open vs Fail-Closed Design

What happens if the Redis rate-limiting cluster itself goes down?

text
Loading code editor...

6. Production Failure Postmortem: The Fail-Closed Redis Gateway Outage

Incident Overview:

In 2020, a top payment gateway experienced a 22-minute total global outage when a single Redis cluster hosting rate limiter keys suffered memory fragmentation and crashed.

What Happened:

  1. The API Gateway was configured with a Fail-Closed policy: if (redisError) throw new RateLimitUnavailableException(500).
  2. When Redis crashed during a routine memory defragmentation cycle, the gateway began rejecting 100% of all incoming merchant payment transactions globally.
  3. Over \4,!000,!000$ in transactions failed in 22 minutes, even though the primary PostgreSQL database and Stripe upstream integrations were completely healthy and idle at 2% CPU utilization.
Interactive Blueprint
Rendering diagram...

Remediation:

  • Converted all public API gateway rate limiters to Fail-Open with a 3-second circuit breaker.
  • Added a local in-memory fallback token bucket on each pod to catch extreme bursts during Redis downtime.

7. Landmark Capstone #7: Build a Distributed Sliding Window Rate Limiter ⚔️

Celebrate the grand finale of Track 1 by implementing an industrial-strength Distributed Sliding Window Rate Limiter:

👉 Launch Capstone: Build a Distributed Sliding Window Rate Limiter

  • Supported Languages: TypeScript & Python 3
  • Challenge Focus:
    • Implement a microsecond-accurate Sliding Window Log / Counter.
    • Enforce atomic capacity checks and expiration window management.
    • Return deterministic X-RateLimit-Limit, X-RateLimit-Remaining, and Retry-After headers.
    • Support multi-tier priority load shedding under simulated queue pressure.

8. Chapter Summary & Rate Limiting Rules

text
Loading code editor...
Milestone Verification

Ready for the next lesson?

Mark this module complete to record verified progress and earn +15 XP toward your architect profile.