Why Redis Becomes Slow and How to Optimize Its Performance
The article explains common reasons why Redis latency increases, such as intrinsic latency, high‑complexity commands, big keys, expiration spikes, memory limits, fork overhead, huge pages, AOF configuration, CPU binding, swap usage and memory fragmentation, and provides detailed troubleshooting steps and optimization techniques.
This article analyzes why Redis may become slow and offers a comprehensive set of diagnostics and optimizations.
1. Intrinsic latency : Use ./redis-cli --intrinsic-latency 120 to measure the maximum latency within 60 seconds; values above the baseline (e.g., >2 ms on low‑end hardware) indicate slowdown.
2. High‑complexity commands : Check the slowlog and avoid O(N) or higher commands such as SORT , SUNION , ZUNIONSTORE , or large N values, as they increase CPU usage.
3. Big keys : Scan for big keys with redis-cli -h 127.0.0.1 -p 6379 --bigkeys -i 1 and limit their size; large keys also worsen fork and copy‑on‑write overhead.
4. Expiration spikes : Randomize expiration times to avoid mass key deletions at the same moment, and consider enabling lazy‑free expiration (e.g., lazyfree-lazy-expire yes ).
5. Memory limit reached : When maxmemory is hit, Redis evicts keys according to the configured policy (e.g., allkeys-lru , volatile-lru ); choose a policy that suits your workload.
6. Fork overhead : Forking for RDB/AOF rewrite copies the process memory; large instances cause long pauses. Monitor latest_fork_usec via INFO and keep instance size below ~10 GB.
7. Huge pages : If transparent huge pages are enabled ( cat /sys/kernel/mm/transparent_hugepage/enabled shows [always] ), disable them to avoid 2 MB copy overhead.
8. AOF configuration : Choose an appropriate appendfsync policy; appendfsync everysec balances safety and performance, but watch for disk I/O contention during AOF rewrite.
9. CPU binding : Bind Redis to multiple logical cores of the same physical CPU, or use Redis 6.0's server_cpulist , bio_cpulist , aof_rewrite_cpulist , etc., to reduce contention.
10. Swap usage : Detect swap with cat /proc/ $pid /smaps | egrep '^(Swap|Size)' ; avoid swap by adding memory or scaling out.
11. Memory fragmentation : Check mem_fragmentation_ratio via INFO memory ; if >1.5, consider restarting (Redis <4.0) or enabling activedefrag (Redis ≥4.0).
Finally, the article provides a checklist of optimization actions, such as limiting O(N) commands, using lazy‑free deletion, adjusting eviction policies, controlling instance size, disabling huge pages, tuning AOF, and monitoring key metrics like expired_keys and latest_fork_usec to maintain low latency.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.