Why Is Your Redis Slowing Down? 7 Common Latency Culprits and How to Fix Them
This article examines typical Redis latency spikes—such as complex O(n) commands, large keys, concentrated expirations, memory limits, fork overhead, CPU binding, AOF settings, swap usage, and network saturation—explaining how to detect, monitor, and mitigate each issue to maintain high‑performance operation.
Redis is an in‑memory database that can handle hundreds of thousands of queries per second, but latency spikes often occur due to misuse or sub‑optimal operations.
Complex Commands
When latency suddenly rises, first check the slowlog. Set the slowlog threshold (in microseconds) and limit the log length:
CONFIG SET slowlog-log-slower-than 5000
CONFIG SET slowlog-max-len 1000Query recent slow entries with: SLOWLOG get 5 Commands with O(n) complexity such as SORT, SUNION, ZUNIONSTORE or large data sets can cause high latency.
Large Keys
If slowlog shows simple commands like SET or DEL taking long, investigate whether very large keys are being written. Large keys increase memory allocation and release time.
Scan for big keys using: redis-cli -h $host -p $port --bigkeys -i 0.01 The -i option controls the interval between scans to limit QPS impact.
Concentrated Expiration
Mass expiration at a fixed time can block the main thread. Redis uses active and lazy expiration; active expiration runs in the main thread and may pause client requests for up to 25 ms.
Search code for EXPIREAT or PEXPIREAT and stagger expirations, e.g.:
# Randomly offset expiration within 5 minutes
redis.expireat(key, expire_time + random(300))Memory Limit Reached
When maxmemory is hit, Redis evicts keys according to the configured policy (e.g., allkeys-lru, volatile-lru, allkeys-random, etc.). Eviction of large keys further increases latency.
Fork Overhead
Background RDB/AOF rewriting forks a child process. Fork copies page tables; with large memory this can block the parent for seconds, especially on constrained CPUs.
Monitor fork time with:
INFO
# look at latest_fork_usec (microseconds)Avoid CPU binding, as the child inherits the binding and competes for CPU resources.
AOF Settings
AOF can be configured with three fsync policies: appendfsync always – highest durability, highest latency. appendfsync everysec – balances durability and performance (recommended). appendfsync no – relies on OS, lowest durability.
Use appendfsync everysec for most workloads.
Swap Usage
If the system starts swapping, Redis latency can jump to hundreds of milliseconds. Detect swap usage, free memory, and restart instances after a failover to avoid prolonged slowdown.
Network Saturation
High network I/O or full NIC bandwidth causes packet loss and increased latency. Monitor network traffic and scale or migrate instances when needed.
Summary
The article consolidates common Redis latency sources—from command complexity and big keys to memory limits, fork overhead, CPU binding, AOF configuration, swap, and network load—and provides detection and mitigation strategies to keep 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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
