25 - System Design: Distributed Rate Limiter (Stripe / Cloudflare)

Executive Summary: In distributed architectures handling hundreds of thousands of requests per second, rate limiting is the primary defense line against brute-force attacks, web scraping, and denial-of-service traffic spikes. Scaling a rate limiter across a multi-server API gateway fleet introduces two critical distributed challenges: Race Conditions (concurrent requests bypassing limits via read-then-write cycles) and Synchronization Latency (every API request requiring a cross-network database call). Production distributed rate limiters solve this by executing Atomic Redis Lua Scripts with lazy token-refill formulas and local in-memory token batching.


๐Ÿข Real-world case studies: how Google & Netflix approach this, in REAL_WORLD_EXAMPLES.md.

โฑ๏ธ Prerequisites: What You Need to Know First

To get the most out of this module, make sure you understand:


๐Ÿง’ The 30-Second Version (ELI5)

Imagine an amusement park with a popular roller coaster:


๐Ÿ• Everyday Analogy

Rate Limiter Component Rollercoaster Turnstile Analogy Technical Reality in Distributed Systems
Token Bucket Brass bucket holding up to 5 ride tokens Redis hash entry: {tokens: 5, last_refill: timestamp}
Turnstile Attendant Security guard checking tokens at the gate API Gateway middleware (Kong, Envoy, Nginx)
Token Refill Automated coin dispenser dropping 1 token every 12s Lazy O(1) timestamp delta formula on request arrival
Allowed Request Dropping a token and walking through the gate HTTP 200 OK forwarded to backend service
Throttled Request Bouncer stopping guest: "Bucket empty, wait 12s!" HTTP 429 Too Many Requests + Retry-After: 12
Race Condition Two guests grabbing the last brass token at the same millisecond Two gateway threads reading tokens=1 concurrently
Atomic Lua Script A steel trapdoor dispensing exactly 1 token to 1 hand Single-threaded atomic script inside Redis

๐Ÿงฉ The Problem It Solves

The Vulnerability of Unprotected APIs

Without a distributed rate limiter:

  1. Denial of Service (DoS): A runaway customer script or malicious attacker fires 100,000 requests/second against /api/checkout, exhausting database connection pools and taking down the platform for all paying customers.
  2. Credential Stuffing: Hackers test millions of stolen password combinations against /api/login without throttling.
  3. Third-Party API Cost Explosion: Uncontrolled traffic spikes trigger massive billing surges on downstream paid services (like Stripe or Twilio).

The Challenge of Distributed Multi-Server Gateways

If your API gateway runs on 20 independent server instances:


๐Ÿ–ผ๏ธ Picture It: Architecture & Flow

High-Resolution Distributed Rate Limiter Animation

Below is an animated trace of incoming client requests, atomic Redis Lua evaluation, token consumption, HTTP 429 throttling, and timer refills:

Distributed Rate Limiter


Global API Gateway & Redis Sharding Flowchart

flowchart TD
    subgraph Clients["Clients Tier"]
        C1["๐Ÿ‘ค Client 1 (Mobile)"]
        C2["๐Ÿค– Client 2 (Web Bot)"]
    end

    subgraph GatewayTier["Distributed API Gateway Fleet"]
        LB["โš–๏ธ L7 Load Balancer"]
        GW1["โšก API Gateway 1"]
        GW2["โšก API Gateway 2"]
        C1 --> LB
        C2 --> LB
        LB --> GW1
        LB --> GW2
    end

    subgraph RedisTier["Centralized Sharded Rate Limiting Tier"]
        Redis[("โšก Redis Cluster (Master / Replica)<br/>Key: rate_limit:user_id<br/>Script: Atomic Token Bucket Lua")]
        GW1 -->|1. EVALSHA token_bucket.lua| Redis
        GW2 -->|1. EVALSHA token_bucket.lua| Redis
    end

    subgraph BackendTier["Protected Core Microservices"]
        Service["๐Ÿข Core Application Database & APIs"]
        Redis -->|"Under Limit: Return (true, remaining)"| GW1
        GW1 -->|"2. HTTP 200 OK -> Forward"| Service
        Redis -.->|"Over Limit: Return (false, retry_after)"| GW2
        GW2 -.->|2. Return HTTP 429 Too Many Requests| C2
    end

    style Clients fill:#161b22,stroke:#58a6ff,stroke-width:2px;
    style GatewayTier fill:#161b22,stroke:#58a6ff,stroke-width:2px;
    style RedisTier fill:#161b22,stroke:#d29922,stroke-width:2px;
    style BackendTier fill:#161b22,stroke:#3fb950,stroke-width:2px;

๐Ÿ”ข Walk-Through with Real Numbers

Let's estimate traffic, memory footprint, and network latency for a global rate limiting tier:

1. Traffic & Memory Sizing

Memory = 10,000,000 ร— 100 bytes โ‰ˆ 1.0 Gigabyte of RAM

A single small 4 GB Redis instance holds the rate limits for 10 million active users!

2. Network Latency & Synchronization Cost

If every request from the 20 API Gateways makes a synchronous network round-trip to Redis:

The Local Token Batching Optimization:


โš™๏ธ How It Works Under the Hood

1. The Token Bucket Lazy Refill Algorithm (Zero Background Timers!)

A naive implementation runs background timer threads incrementing counters every second. At 10 million users, 10 million timer ticks per second would melt the CPU. The Production Solution: Lazy Refill on Request Arrival: When a request arrives at timestamp Tnow:

  1. Fetch previous state: (Ttokens, Tlast).
  2. Calculate time elapsed: ฮ” t = Tnow - Tlast.
  3. Add refilled tokens:
New Tokens = \min\big(Capacity, Ttokens + (ฮ” t ร— Refill Rate)\big)
  1. If New Tokens โ‰ฅ 1: deduct 1, update Tlast = Tnow, and allow the request! Complexity: Pure O(1) arithmetic. Zero background timer threads!

2. Eliminating Race Conditions with Atomic Redis Lua Scripts

Because Redis executes Lua scripts as a single atomic transaction without interruption, the read-modify-write cycle is completely immune to race conditions:

(The complete production Lua script is separated into rate_limiter.lua, and its atomic execution is simulated in demo.py.)


3. Standard HTTP Response Headers (RFC 6585)

When rate limiting is enforced, the API Gateway communicates status transparently via HTTP response headers:

Header Meaning Example Value
X-RateLimit-Limit Maximum number of allowed requests in current window 100
X-RateLimit-Remaining Number of remaining tokens available in current window 42
X-RateLimit-Reset Unix timestamp when the bucket will be completely refilled 1795372800
Retry-After Returned on 429: Number of seconds to sleep before retrying 3

โš–๏ธ Trade-Offs & Decision Matrix

Algorithm Bursts Allowed? Memory Usage Implementation Complexity Primary Drawback
Token Bucket Yes (up to capacity) O(1) per client (2 numbers) Low (with lazy refill) Burst traffic can temporarily spike backends
Leaky Bucket โŒ No (smooth constant rate) O(1) queue size Medium Drops requests if queue fills up
Fixed Window Yes (2x burst at boundary) Lowest (1 integer counter) Lowest Boundary spike allows 2x limit at window turn
Sliding Window Log โŒ No High (O(M) timestamps) High High memory overhead storing all request timestamps
Sliding Window Counter Smooth estimation Low (2 window counters) Medium Approximated count (assume uniform traffic)

๐Ÿšซ Common Mistakes & Production Pitfalls

  1. The Fixed Window Boundary Burst Bug:

    • Pitfall: Using a simple fixed window counter: INCR rate:user:12:00.
    • Result: A user sends 100 requests at 11:59:59 PM, and another 100 requests at 12:00:01 AM. Both windows show 100 requests (legal), but the system processed 200 requests in 2 seconds! Always use Token Bucket or Sliding Window Counters to prevent boundary doubling.
  2. Failing Open vs. Failing Closed During Redis Outages:

    • Pitfall: If the Redis rate limiter cluster crashes, every API call throws a 500 error (Fail Closed), causing a total platform outage.
    • Production Standard: Fail Open. If Redis is unreachable, log a critical alert, bypass rate limiting, and let traffic flow through to the backend while triggering emergency alerting. A degraded rate limiter is far better than a total platform blackout.
  3. Using IP Address Exclusively for Rate Limiting:

    • Pitfall: Rate limiting solely by client IP address (req.ip).
    • Result: Large corporate offices, university campuses, or cellular carriers route thousands of distinct users through a single public NAT gateway IP. Limiting by IP blocks an entire university when one student exceeds the limit! Use API Keys or Authenticated User IDs whenever possible.

๐Ÿ’ป Try It Yourself: Python Simulation

The companion simulation script demo.py implements:

  1. An O(1) Token Bucket algorithm with lazy mathematical refills.
  2. A simulated Redis Atomic Lua Script execution eliminating race conditions across multiple gateway workers.
  3. Rapid burst testing: verifying that exactly 5 burst requests pass and subsequent requests return HTTP 429 Too Many Requests.
  4. Automated token recovery verification after a 1.0-second refill sleep.

Run it locally in your terminal:

python demo.py

๐ŸŒ Where You'll See It in Production


๐ŸŽฏ Top Interview Questions & Deep Answers

Q1: "How do you prevent race conditions in a distributed rate limiter with 50 API Gateway servers?"

Interviewer Goal: Testing candidate's knowledge of atomic primitives and distributed concurrency.

Model Answer: "In a distributed gateway fleet, multiple servers concurrently receive requests for the same user. If servers use a naive GET count -> count++ -> SET count pattern:

The Solution is Redis Lua Scripting:

  1. We encapsulate the entire check, refill, and decrement logic inside a Lua script.
  2. Redis executes Lua scripts with single-threaded atomicity: while the script executes, no other command or script can run on that shard.
  3. The check and token decrement execute as an indivisible atomic operation in memory (<100 ฮผs). Zero race conditions are mathematically possible."

Q2: "What is the difference between Token Bucket and Leaky Bucket, and when would you choose one over the other?"

Interviewer Goal: Evaluating algorithm trade-offs and traffic shaping understanding.

Model Answer: "While both regulate throughput, they produce completely different traffic flow characteristics:

  1. Token Bucket:
    • Tokens accumulate up to a maximum bucket capacity. Requests consume tokens.
    • Allows bursts of traffic: If the bucket is full (capacity 20), an application can legally fire 20 requests in 1 millisecond.
    • Best for: General-purpose web APIs (Stripe, GitHub), where user traffic naturally occurs in small bursts (e.g. loading a dashboard).
  2. Leaky Bucket:
    • Requests enter a FIFO queue (the bucket) and leak out to the backend at a strictly constant, smooth rate.
    • If the queue overflows, incoming requests are dropped.
    • Best for: Traffic shaping in data ingestion pipelines, where downstream systems (like a database or third-party webhooks) require a smooth, constant flow without any sudden spikes."

Q3: "How do you handle rate limiting across multiple geographic data centers (e.g. US-East and EU-West)?"

Interviewer Goal: Testing global distributed state synchronization trade-offs.

Model Answer: "Synchronizing rate limits across global data centers presents a classic PACELC trade-off:

Production Solutions:

  1. Geo-Partitioned / Local Rate Limiting:
    • Allocate a portion of the user's quota to each region (e.g. 50% to US, 50% to EU). Local gateways enforce their local quota using local Redis clusters with sub-millisecond latency.
  2. Asynchronous Cross-Region Sync:
    • Gateways enforce limits locally in RAM and periodically publish usage deltas asynchronously across a WAN message bus (Kafka/CRDTs). While small temporary overages (<5%) can occur during high cross-region traffic bursts, latency remains under 1 ms.
  3. Session Affinity / Geo-DNS Routing:
    • Route all requests from a given API key or user to the same geographic region via DNS or BGP Anycast."

Q4: "Where should the Rate Limiter be placed in the architecture: Client, API Gateway, or Middleware?"

Interviewer Goal: Assessing architectural layering and defense-in-depth principles.

Model Answer: "The optimal placement is at the API Gateway / Edge Reverse Proxy Layer:

  1. Client-Side Rate Limiting:
    • Unreliable for security. Malicious attackers and compromised devices bypass client logic trivially. (Useful only as a polite SDK throttling mechanism to avoid unnecessary network calls).
  2. Application Middleware (Inside Core Microservices):
    • Too late in the stack. By the time a request hits application containers, network bandwidth, TLS handshakes, and gateway connection pools have already been consumed.
  3. API Gateway (The Standard):
    • Sits in front of all microservices (Envoy, Kong, Nginx).
    • Rejects throttled traffic at the perimeter before it touches internal networks, protecting backend databases, thread pools, and business logic from resource exhaustion."

Q5: "What is the Sliding Window Counter algorithm, and how does it balance accuracy with low memory?"

Interviewer Goal: Assessing understanding of hybrid rate limiting algorithms.

Model Answer: "The Sliding Window Counter is a memory-efficient hybrid of Fixed Window and Sliding Window Log:

Count = (Previous Window Count ร— (1 - P)) + Current Window Count

โœ… Check Yourself: Flash Quiz

1. Why does the Token Bucket algorithm require zero background timer threads in production?
Because tokens are refilled lazily on-demand when a request arrives. The server simply computes the elapsed time delta since the last request (ฮ” t = now - last_refill) and multiplies by the refill rate in O(1) constant time, eliminating the CPU overhead of background timers.
2. What HTTP status code should an API Gateway return when a client exceeds their rate limit, and what header tells them how long to wait?
Return HTTP 429 Too Many Requests along with the Retry-After header indicating the number of seconds the client must sleep before retrying.
3. Why is it dangerous to rely on client IP addresses as the sole rate limiting key?
Because thousands of distinct mobile users or corporate employees frequently share the exact same public IP address via NAT (Network Address Translation) or corporate proxies. Rate limiting by IP alone can inadvertently block an entire company or university when a single user misbehaves. Always rate limit by API Key, Session Token, or User ID when available.