Why Is Redis Slowing Down? 10 Common Causes and How to Fix Them
This guide explains how to identify and resolve Redis performance degradation by measuring baseline latency, checking slowlog, avoiding high‑complexity commands, handling big keys, managing expiration spikes, tuning memory limits, reducing fork overhead, disabling huge pages, configuring AOF, binding CPUs, preventing swap usage, fixing memory fragmentation, and using lazy‑free mechanisms.
1. Measure baseline latency
Run an intrinsic latency test on the Redis server to obtain the maximum response time over a period (e.g., 60 s): ./redis-cli --intrinsic-latency 120 Typical output shows a peak latency such as 119 µs. You can also monitor min/avg/max latency over time:
redis-cli -h 127.0.0.1 -p 6379 --latency-history -i 1If the observed latency exceeds roughly twice the baseline, the instance is considered slow.
2. Identify high‑complexity commands
Check the slowlog (default threshold 5 ms, length 500). Adjust the threshold if needed:
CONFIG SET slowlog-log-slower-than 5000 # 5 ms
CONFIG SET slowlog-max-len 500Commands with O(N) or higher complexity (e.g., SORT, SUNION, ZUNIONSTORE) or O(N) on very large N increase CPU usage and network transfer time.
3. Detect big keys
If simple commands like SET or DEL appear in the slowlog, scan for oversized keys: redis-cli -h 127.0.0.1 -p 6379 --bigkeys -i 1 The report lists the number of sampled keys, total size, and the largest key per data type. Use the -i interval to throttle the scan and avoid OPS spikes.
4. Mitigate concentrated expiration
Mass expiration at the same timestamp can cause latency spikes because Redis actively expires keys every 100 ms. Reduce the impact by adding random jitter to expiration times or enabling lazyfree-lazy-expire (Redis 4.0+).
5. Handle memory‑limit eviction
When maxmemory is reached, Redis evicts keys according to the configured policy. Eviction runs in the main thread and can block requests, especially for big keys. Common policies:
allkeys-lru volatile-lru allkeys-random volatile-random allkeys-ttl noeviction allkeys-lfu(Redis 4.0+) volatile-lfu (Redis 4.0+)
6. Reduce fork overhead
Background RDB/AOF rewrites fork a child process. Fork copies the parent’s page tables; for large instances this can take seconds and block the main thread. Check the duration with: INFO | grep latest_fork_usec A high value (e.g., 59477 µs) indicates a noticeable pause.
7. Disable transparent huge pages
Linux transparent huge pages allocate memory in 2 MiB chunks. Even a tiny write forces a 2 MiB allocation, increasing latency. Verify the setting: cat /sys/kernel/mm/transparent_hugepage/enabled If the output shows [always], disable it:
echo never > /sys/kernel/mm/transparent_hugepage/enabled8. Tune AOF fsync policy
Redis provides three appendfsync options: appendfsync always – highest durability, highest I/O cost. appendfsync no – best performance, risk of data loss. appendfsync everysec – writes return immediately; a background thread fsyncs once per second.
During an AOF rewrite, the background thread may block on disk I/O, causing the main thread to stall if the previous fsync has not finished.
9. Optimize CPU binding (Redis 6.0+)
Binding Redis to a single logical CPU can cause contention between the main thread and forked processes. Use Redis 6.0’s per‑thread CPU list configuration to bind different thread groups to separate cores:
# Before binding (Redis 6.0)
taskset -c 0 ./redis-server
# After binding (Redis 6.0)
server_cpulist 0-7:2 # main & IO threads on cores 0,2,4,6
bio_cpulist 1,3 # background I/O threads
aof_rewrite_cpulist 8-11
bgsave_cpulist 1,10,1110. Detect and eliminate swap usage
When Redis starts swapping, latency skyrockets. Find the Redis PID and inspect its memory maps:
# Find PID
ps -aux | grep redis-server
# Inspect /proc/$pid/smaps for Swap entries
cat /proc/$pid/smaps | egrep '^(Swap|Size)'If significant memory is swapped, add RAM or scale out the cluster.
11. Reduce memory fragmentation
Fragmentation ratio = used_memory_rss / used_memory. A ratio > 1.5 indicates > 50 % fragmentation. Mitigate by restarting (Redis < 4.0) or enabling active defragmentation (Redis ≥ 4.0):
activedefrag yes
active-defrag-ignore-bytes 100mb
active-defrag-threshold-lower 10
active-defrag-threshold-upper 100
active-defrag-cycle-min 1
active-defrag-cycle-max 25
active-defrag-max-scan-fields 100012. Use lazy free for asynchronous deletion
Redis 4.0+ supports non‑blocking deletions: UNLINK – async delete. FLUSHALL ASYNC / FLUSHDB ASYNC – fast bulk clear.
Relevant configuration flags (default no):
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush noEnable the flags that match your workload (e.g., lazyfree-lazy-expire yes for expired keys).
13. Optimize AOF rewrite I/O
During an AOF rewrite you can temporarily disable fsync in the child process to reduce I/O contention: no-appendfsync-on-rewrite yes Be aware this increases the risk of data loss if a crash occurs during the rewrite.
14. Swap recovery
To free swap, restart the instance after promoting a replica to master, or add memory / scale the cluster. Monitor swap usage via INFO memory and set alerts.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
dbaplus Community
Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.
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.
