Why Is Redis Slowing Down? Proven Techniques to Diagnose and Fix Latency Issues
Redis latency spikes can stem from complex commands, large keys, concentrated expirations, memory limits, fork overhead, CPU binding, AOF settings, swap usage, or network saturation, and this guide explains how to identify, monitor, and mitigate each cause using slowlog, bigkeys, info, and configuration tweaks.
Redis is an in‑memory database with extremely high throughput, but in production it often exhibits sudden latency spikes. Understanding Redis internals and monitoring key metrics are essential for effective troubleshooting.
High‑Complexity Commands
When latency suddenly increases, first check the slowlog. Set a threshold (e.g., 5 ms) and limit the log length:
CONFIG SET slowlog-log-slower-than 5000
CONFIG SET slowlog-max-len 1000Query recent entries: SLOWLOG get 5 If commands with O(n) complexity such as sort, sunion, zunionstore or large LRANGE operations appear frequently, they are likely the cause. Reduce the amount of data processed per request or avoid these commands.
Large Keys
Slowlog may also show simple SET or DEL commands that take long because the key size is huge. Large keys increase memory allocation and release time.
Scan for big keys with: redis-cli -h $host -p $port --bigkeys -i 0.01 When scanning, limit the frequency with -i to avoid QPS spikes. Consider using Redis 4.0+ lazy‑free for asynchronous deletion of big keys.
Concentrated Expiration
Mass expiration at a fixed point (e.g., many keys using expireat or pexpireat) can block the main thread. The delay is not recorded in the slowlog.
Disperse expiration times by adding a random offset:
# Randomly expire within 5 minutes after the original time
redis.expireat(key, expire_time + random(300))Monitor expired_keys via INFO and alert on sudden spikes.
Memory Limit Reached
When maxmemory is hit, Redis evicts keys before writing new data. Eviction policies (e.g., allkeys-lru, volatile-lru, allkeys-random) affect latency. Avoid storing big keys and consider splitting data across multiple instances.
Fork Overhead
Background RDB/AOF rewriting forks a child process. Forking large memory maps can block the server; the duration is visible as latest_fork_usec in INFO. Schedule persistence during low‑traffic periods or disable AOF if data loss is acceptable.
CPU Binding
Binding Redis to specific CPUs can cause contention during forked persistence, increasing latency. Avoid CPU pinning when using RDB/AOF.
Enabling AOF
AOF writes each command to a log. Three sync policies exist: appendfsync always – highest durability, worst performance. appendfsync everysec – balances safety and speed (recommended). appendfsync no – relies on OS, lowest durability.
Choose everysec for most workloads.
Swap Usage
If the host swaps, Redis memory accesses become disk‑bound, causing hundreds of milliseconds latency. Monitor memory and swap, and restart instances after clearing swap, preferably via a master‑slave switchover.
Network Saturation
Even with a healthy Redis process, a saturated NIC can introduce network‑level delays and packet loss. Track network traffic and scale bandwidth or migrate instances when needed.
Summary
Redis latency can arise from command complexity, large keys, expiration patterns, memory limits, fork overhead, CPU binding, AOF configuration, swap usage, or network bottlenecks. Developers should understand command complexities and use appropriate data structures, while DBAs should monitor persistence, memory, CPU, and network metrics to maintain high performance.
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.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
