Course contentsShow
← System Design
Lesson 1807 of 1,91943. Real-World System: Rate Limiter ServiceFree lesson

In-Memory vs Persistent Storage for Rate Limiting

Compare memory-based counters (Redis) vs database storage for rate limit state and choose based on durability needs.

In-Memory vs Persistent Storage for Rate Limiting

What you'll learn: How to choose between memory-based systems like Redis and persistent databases for storing rate limit state, based on your durability and performance requirements.

The Core Trade-Off

When implementing a rate limiter, you must decide where to store the counters, timestamps, and state that track each user's request history. The two main approaches are in-memory storage (Redis, Memcached) and persistent storage (PostgreSQL, MySQL, DynamoDB).

In-Memory Storage (Redis)

Redis excels at rate limiting because:

  • Microsecond latency: Critical when you're checking limits on every request
  • Built-in atomic operations: INCR, EXPIRE, Lua scripts prevent race conditions
  • Natural TTL support: Keys expire automatically, no manual cleanup needed
  • High throughput: Handles millions of operations per second

Trade-off: Data is volatile. If Redis crashes, you lose all counters. Users might briefly exceed limits during recovery.

Persistent Storage (Database)

Traditional databases offer:

  • Durability: Counters survive crashes and restarts
  • Audit trails: Full history for compliance or debugging
  • Complex queries: Join with user metadata, analyze patterns

Trade-off: Higher latency (10-100x slower than Redis). Database load becomes a bottleneck at scale.

When to Choose What

Use in-memory (Redis) when:

  • Rate limits are soft guidelines, not strict contracts
  • Sub-millisecond latency matters
  • Temporary counter loss during failures is acceptable

Use persistent storage when:

  • Strict compliance or billing depends on accurate counts
  • You need historical audit logs
  • Traffic is low enough that database latency isn't critical

Hybrid approach: Use Redis for real-time decisions, periodically snapshot critical counters to a database for durability.

Key Takeaway: In-memory storage like Redis is the industry standard for rate limiting due to speed and atomic operations, but persistent databases are essential when strict accuracy and auditability matter more than millisecond-level performance.