Databases 18 min read

Redis Latency Analysis: Common Causes and How to Diagnose Them

This article explains why Redis latency can suddenly increase, covering high‑complexity commands, large keys, concentrated expirations, memory limits, fork overhead, CPU binding, AOF settings, swap usage, and network overload, and provides concrete diagnostic commands and mitigation strategies.

Top Architect
Top Architect
Top Architect
Redis Latency Analysis: Common Causes and How to Diagnose Them

High‑Complexity Commands

If Redis latency spikes, first check the slow‑log. Set the threshold (in microseconds) and keep only the latest 1000 entries:

CONFIG SET slowlog-log-slower-than 5000
CONFIG SET slowlog-max-len 1000

Then retrieve recent slow entries: SLOWLOG get 5 Commands with O(n) complexity such as sort, sunion, zunionstore, or any O(n) operation on large data sets can cause noticeable delays.

Large Keys

When slow‑log shows only SET or DELETE commands, investigate whether very large keys are being written. Large keys increase memory allocation and release time. Use the built‑in big‑key scanner: redis-cli -h $host -p $port --bigkeys -i 0.01 The scanner runs a SCAN over all keys and measures their size with STRLEN, LLEN, HLEN, SCARD, ZCARD. Limit the scan interval with -i to reduce impact on QPS.

Concentrated Expiration

Mass expirations at a fixed time can overload Redis because the active expiration task runs in the main thread. Look for frequent use of EXPIREAT or PEXPIREAT. A simple mitigation is to add a random offset when setting expiration:

# Randomly expire within 5 minutes after the intended time
redis.expireat(key, expire_time + random(300))

Memory Limit Reached

When maxmemory is hit, Redis must evict keys before new writes, which adds latency. Choose an eviction policy that fits the workload (e.g., allkeys-lru, volatile-lru, allkeys-random, etc.). Large‑key eviction is especially costly.

Fork Overhead

Background RDB/AOF generation forks a child process. Forking copies the page table; with a large memory footprint this can block the main thread for seconds. Monitor the duration with INFOlatest_fork_usec. Avoid enabling AOF or RDB rewrite on heavily loaded instances, and prefer performing backups on replicas during off‑peak hours.

CPU Binding

Binding Redis to specific CPUs is discouraged because the forked child inherits the CPU affinity, leading to contention during persistence and increased latency.

AOF Configuration

AOF can be tuned with three fsync policies: appendfsync always: highest durability, highest latency. appendfsync everysec: balances durability (max 1 s loss) and performance. appendfsync no: relies on OS, lowest durability.

For most workloads, appendfsync everysec is recommended.

Swap Usage

If the host starts swapping, Redis latency can rise to hundreds of milliseconds because memory pages are read from disk. Detect swap usage, free memory, and restart the instance (preferably after a master‑slave switchover) to clear swap.

Network Saturation

Even with a healthy Redis process, a saturated NIC can cause request delays and packet loss. Monitor network traffic, set alerts, and scale out or migrate instances when bandwidth limits are reached.

Conclusion

The article summarizes typical Redis latency sources—command complexity, large keys, expiration bursts, memory limits, fork overhead, CPU binding, AOF settings, swap, and network load—and provides concrete commands and best‑practice recommendations for developers and DBAs to keep Redis performant.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceredisLatencyMemory
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.