Skip to content

💡 Practical Experience

The KEYS Command is a Nuclear Bomb

KEYS * iterates through every key in the entire database — running it on an instance with millions of keys can freeze Redis for several seconds, causing all clients to timeout. In production, use SCAN instead — it uses cursor-based iteration and won't block.

Use used_memory_rss, Not used_memory

used_memory is only Redis's own accounting of logical memory. used_memory_rss is the actual physical memory allocated by the operating system. A large gap between the two indicates memory fragmentation. For monitoring, use rss, and set alert thresholds based on rss.

Big Keys Are the Root of All Evil

A String value storing 100MB of JSON, a Hash with 1 million fields — these are big keys. Big keys cause: blocking during deletion (DEL on a big key freezes for seconds), blocking during replica sync, uneven memory distribution (in Cluster mode, one node becomes bloated). Use redis-cli --bigkeys for regular scanning.

Don't Set Expiration Too Precisely

Setting 1 million keys to expire after 3600 seconds means they all expire at the same time, causing Redis to process a million deletion requests instantly — CPU spikes to 100%. The solution: add a random offset to expiration times: EXPIRE key (3600 + RANDOM(0,600)).