🔴 Redis Advanced

What are large key anti-patterns in Redis and how do you fix them?

Answer

A large key (also called "big key" or "hot key") is a key whose value is very large or whose collection contains too many members. Examples: a String value of 10MB, a Hash with 5 million fields, a Set with 1 million members, a List used as an unbounded queue. Problems: serializing/deserializing large values consumes significant CPU and memory on the client. Operations on large collections (like HGETALL on a million-field hash) block the single Redis thread for milliseconds, causing latency spikes for all other clients. To detect big keys: redis-cli --bigkeys scans the keyspace, or scan with SCAN + OBJECT ENCODING/MEMORY USAGE. Fixes: (1) Split large collections using key sharding (e.g., hash:bucket:{key % N}). (2) Use smaller page sizes and cursor-based iteration (HSCAN, SSCAN, ZSCAN). (3) Set a max size and use LTRIM to cap list growth. (4) Store large blobs in object storage (S3) and keep only the reference in Redis. (5) Compress values before storing.