Redis in Production: Common Gotchas
Redis is incredibly fast, but its single-threaded nature means that a single mistake can bring down your entire application's performance. Here are 5 common pitfalls developers face when scaling Redis.
1. The "Big Key" Problem (O(N) Operations)
Since Redis is single-threaded, running an (N)$ operation on a large data structure blocks all other requests.
- The Pitfall: Storing thousands of items in a single Hash or Set and then calling
HGETALLorSMEMBERS. - The Solution: Use
HSCANorSSCANto iterate over large structures in smaller increments, or split large keys into multiple smaller ones.
2. Blocking the Main Thread
Some commands are inherently slow and should almost never be used in production.
- The Pitfall: Using
KEYS *to find patterns. In a large database, this will lock Redis for several seconds. - The Solution: Use
SCANinstead. It is non-blocking and safe for production.
3. Incorrect Eviction Policies
When Redis runs out of memory, it starts deleting keys based on its maxmemory-policy.
- The Pitfall: Using
allkeys-lruwhen you have persistent data that should not be deleted. - The Solution: If you are using Redis for both caching and persistence, ensure your important keys have no TTL and use
volatile-lruornoevictionto protect them.
4. The "Hot Key" Problem
Even though Redis is fast, a single key being accessed by thousands of clients simultaneously can bottleneck the CPU or the network interface of a single node.
- The Pitfall: Storing a globally shared configuration or a high-traffic "trending" item in a single key.
- The Solution: Use client-side caching (Redis 6+) or replicate the hot key across multiple nodes/clusters.
5. Persistence Latency (Forking)
To save an RDB snapshot, Redis calls fork(). On a large instance with many writes, this can cause a "hiccup" in latency.
- The Pitfall: Running a 32GB Redis instance and performing frequent RDB saves. The
fork()can take hundreds of milliseconds. - The Solution: Use a smaller memory footprint per instance (sharding) or run backups on a dedicated replica to avoid impacting the master's performance.
Summary
Redis is a precision tool. By avoiding blocking operations, managing key sizes, and choosing the right eviction policy, you can ensure it remains the fastest part of your stack.
