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

Rate Limiting State Storage

Where to store rate limit counters: in-memory, Redis, or distributed cache, and their consistency tradeoffs.

Rate Limiting State Storage

You'll learn: where to store rate limit counters and the consistency tradeoffs of each storage option.

When implementing rate limiting algorithms, you need somewhere to track state—counters, timestamps, or tokens. Where you store this data dramatically affects your system's performance, scalability, and consistency guarantees.

Storage Options

In-Memory (Local)

Store counters directly in your application's memory (hash maps, dictionaries). This is blazingly fast with zero network overhead, but state is isolated per server instance. If requests from the same user hit different servers, each server counts independently—potentially allowing 2x or 3x your intended limit.

Analogy: Each bouncer at different club entrances has their own guest counter, but doesn't coordinate with the others.

Redis (Centralized)

A shared Redis instance provides a single source of truth. All servers increment the same counters, ensuring accurate global limits. The tradeoff? Network latency on every rate limit check (though Redis operations are typically sub-millisecond). Redis offers atomic operations like INCR and EXPIRE, perfect for rate limiting.

Distributed Cache

Systems like Hazelcast or distributed Redis clusters offer middle ground—near-in-memory performance with shared state. However, eventual consistency means brief windows where counts might be slightly inaccurate across regions.

Consistency Tradeoffs

  • Strong consistency (centralized Redis): accurate limits, higher latency
  • Eventual consistency (distributed cache): lower latency, possible temporary over-limit
  • No consistency (local memory): fastest, but limits multiply by server count

Most production systems choose Redis for user-facing rate limits (accuracy matters) and in-memory for internal service protection (speed matters more).

Key Takeaway: Rate limiting storage is a CAP theorem tradeoff—choose centralized storage (Redis) for accurate global limits or local memory for speed when approximate limits suffice.