Why Redis Gets Slow: Common Latency Causes and How to Fix Them
This article explains why a high‑performance Redis instance can suddenly experience high latency, covering complex commands, large keys, expiration bursts, memory limits, fork overhead, CPU binding, AOF settings, swap usage, and network saturation, and provides practical troubleshooting and mitigation steps.
Redis is an in‑memory database with very high QPS, but latency spikes can occur due to misuse or operational issues.
Complex Commands
Check the slowlog, set a threshold, view recent entries, and avoid O(n) commands such as SORT, SUNION, ZUNIONSTORE on large data sets.
# Record commands slower than 5 ms
CONFIG SET slowlog-log-slower-than 5000
# Keep only the latest 1000 entries
CONFIG SET slowlog-max-len 1000Query recent logs with SLOWLOG GET 5 to identify slow commands.
Large Keys
Identify big keys with redis-cli --bigkeys -i 0.01 and avoid storing excessive data in a single key. Use lazy‑free in Redis 4.0 or split instances to mitigate impact.
Key Expiration Burst
Mass expiration at a fixed time can block the main thread. Search for EXPIREAT or PEXPIREAT in code and add a random offset to spread expirations.
# Randomly expire within 5 minutes after the original time
redis.expireat(key, expire_time + random(300))Memory Limits
When maxmemory is reached, eviction policies (e.g., allkeys‑lru, volatile‑lru) add latency, especially for large keys. Consider using random eviction or sharding instances.
Fork Overhead
RDB/AOF persistence and full‑sync create a child process via fork, copying page tables and consuming CPU. Monitor latest_fork_usec and schedule backups during low‑traffic periods.
CPU Binding
Binding Redis to a specific CPU worsens fork contention; avoid CPU pinning when persistence is enabled.
AOF Settings
Choose appendfsync everysec for a balance between durability and performance; avoid appendfsync always unless data loss is unacceptable.
Swap Usage
Swap dramatically slows Redis. Monitor memory and swap, release swap by restarting or failover, and keep enough free RAM.
Network Saturation
High NIC load causes packet loss and latency. Monitor network traffic, scale out or migrate instances when bandwidth is exhausted.
Summary
Redis latency can stem from complex commands, large keys, expiration bursts, memory limits, fork overhead, CPU binding, AOF configuration, swap, or network saturation. Understanding these mechanisms and applying the appropriate mitigations keeps Redis performant.
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 Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
