Course contentsShow
← System Design
Lesson 980 of 1,91925. Rate Limiting and ThrottlingFree lesson

Redis-Based Distributed Rate Limiting

Using Redis atomic operations and Lua scripts to implement rate limiting across multiple application servers.

Redis-Based Distributed Rate Limiting

What You'll Learn

You'll learn how to use Redis atomic operations and Lua scripts to implement consistent rate limiting across multiple application servers without race conditions.

The Core Problem and Solution

When multiple application servers share rate-limiting responsibility, they need a single source of truth for counter state. Redis excels here because:

  1. Atomic Operations: Redis commands execute atomically, preventing race conditions
  2. Centralized State: All servers query the same counters
  3. Performance: In-memory operations with microsecond latency
  4. TTL Support: Automatic cleanup of expired time windows

How Redis Enables Distributed Rate Limiting

Basic Pattern (Fixed Window): Use INCR to atomically increment a counter keyed by user:123:minute:2024-01-15-10:30. Set TTL to expire the key after the window closes.

Atomic Lua Scripts: For complex algorithms like sliding window counters, Redis Lua scripts execute multiple operations atomically. The script can:

  • Check current count
  • Add new request timestamp
  • Remove expired entries
  • Return whether request is allowed

All in one atomic transaction, preventing the "check-then-act" race condition.

Example Key Structure

rate_limit:{user_id}:{window_start}

Redis operations like INCR, EXPIRE, and Lua's redis.call() ensure that even if 100 servers simultaneously check the same user's limit, only the correct number of requests proceed.

Analogy

Think of Redis as a shared turnstile counter at a stadium entrance. All gates (servers) read from and update the same counter display. The counter increments atomically—no two gates can simultaneously read "99" and both let someone through when the limit is 100.

Key Takeaway: Redis atomic operations and Lua scripts provide the synchronization primitive needed for accurate, high-performance distributed rate limiting across multiple application servers.