Skip to content
Veltoryx Technologies
ProLesson 10.2Advanced8 min readUpdated 31 October 2026

Designing a Distributed Rate Limiter

Build an enterprise rate limiter: Token Bucket, sliding window counters, Redis atomic Lua scripts, and fail-open resilience.

Every production web service exposed to the public Internet faces traffic instability:

  • Malicious actors launch credential-stuffing attacks or scraping bots that execute thousands of requests per second.
  • Buggy client mobile applications enter infinite retry loops, flooding backend microservices.
  • Flash sales or marketing campaigns create sudden traffic surges that overwhelm database connection pools.

Without defense, a single abusive customer or rogue script can exhaust server CPU, saturate memory, and trigger cascading outages for every other user sharing the infrastructure.

A Distributed Rate Limiter protects backend services by throttling traffic based on client identity (e.g., API key, user ID, or IP address). When a client exceeds their permitted quota, the rate limiter intercepts the request at the edge, rejecting it with HTTP 429 Too Many Requests before it can consume valuable backend resources.

01Core concepts and intuition

Designing a rate limiter for a single server is straightforward: you can use an in-memory counter or Google Guava's RateLimiter.

However, in a high-scale distributed environment with hundreds of API gateways, multiple challenges arise:

  • Distributed State: A client may send Request 1 to Gateway A in US-East and Request 2 to Gateway B in US-East. Both gateways must share a unified rate-limiting counter.
  • Race Conditions: If two requests arrive at different gateways at the exact same millisecond, both gateways might read the counter as 99, decide the limit of 100 has not been reached, increment to 100, and allow both through.
  • Low Latency: The rate check sits directly in the critical request path. If the rate limiter takes 30ms to decide, every user request in the company slows down by 30ms.

Rate limiting algorithms compared

There are four primary algorithmic models:

  1. Token Bucket:
    • A bucket has a fixed capacity (e.g., 100 tokens). Tokens are continuously added at a constant refill rate (e.g., 10 tokens per second).
    • Each incoming request consumes 1 token. If the bucket is empty, the request is dropped.
    • Advantage: Naturally handles short bursts of traffic (up to the bucket capacity) while strictly capping long-term throughput.
  2. Leaky Bucket:
    • Requests enter a FIFO queue. Requests leak out of the bottom of the bucket at a constant, fixed rate.
    • Advantage: Smooths out traffic into a completely flat, predictable output rate.
  3. Fixed Window Counter:
    • Time is divided into fixed intervals (e.g., 1 minute). A counter tracks requests within that window.
    • Disadvantage: Vulnerable to the boundary surge problem. If a user sends 100 requests at 10:00:59 and another 100 requests at 10:01:01, 200 requests pass through within a two-second window, violating the rate limit.
  4. Sliding Window Counter (The Modern Standard):
    • Combines the memory efficiency of fixed windows with the smoothness of sliding logs.
    • Computes request volume using a weighted average of the current window and previous window:
      text
      Estimated Requests = Current Window Count + Previous Window Count * (1 - Overlap Percentage)
    • Prevents boundary spikes while requiring minimal memory (just two counters per user).

Loading the rest of the lesson…