Databases 39 min read

Why Is My Redis Slowing Down? Comprehensive Diagnosis & Optimization Guide

This article provides a thorough step‑by‑step guide to identify why Redis latency spikes, covering baseline performance testing, slowlog analysis, big‑key handling, expiration patterns, memory limits, fork overhead, huge pages, AOF settings, CPU binding, swap usage, memory fragmentation, network bandwidth, and practical optimization techniques.

Sanyou's Java Diary
Sanyou's Java Diary
Sanyou's Java Diary
Why Is My Redis Slowing Down? Comprehensive Diagnosis & Optimization Guide

Redis is a high‑performance in‑memory database, but latency can increase unexpectedly. This guide walks through a systematic troubleshooting process to determine whether Redis is truly slowing down and how to resolve common issues.

Confirm Redis Is Slow

First, verify if the Redis instance is actually experiencing higher latency by checking service‑side tracing and measuring baseline performance with redis-cli --intrinsic-latency 60 and redis-cli --latency-history -i 1.

Baseline Performance Test

Run a benchmark on a healthy instance to record maximum and average latency; compare this with the suspect instance. If the suspect instance’s latency is more than twice the baseline, it is considered slow.

Analyze Slowlog

Set a slowlog threshold (e.g., 5 ms) with

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

and retrieve recent entries using SLOWLOG get 5. Look for high‑complexity commands (O(N) or worse) such as SORT , SUNION , ZUNIONSTORE or large O(N) operations.

Big‑Key Issues

If simple commands like SET or DEL appear in the slowlog, investigate big keys. Scan for them with

redis-cli -h 127.0.0.1 -p 6379 --bigkeys -i 0.01

and avoid storing oversized values; use UNLINK (Redis 4.0+) or enable lazy‑free eviction for large deletions.

Concentrated Expiration

Mass expiration at fixed times can block the main thread. Search for EXPIREAT / PEXPIREAT patterns and stagger expirations with random offsets or enable lazy‑free expiration.

Memory Limit & Eviction

When maxmemory is reached, Redis evicts keys according to the configured policy (e.g., allkeys‑lru , volatile‑lru ). Eviction of big keys can be costly; consider using random eviction or adjusting the policy.

Fork Overhead

Background RDB/AOF rewrites fork a child process, copying page tables. Large instances cause long fork times, blocking the server. Monitor latest_fork_usec via INFO and keep instance size reasonable (<10 GB).

Transparent Huge Pages

Huge pages increase memory‑allocation latency. Disable them by setting echo never > /sys/kernel/mm/transparent_hugepage/enabled for Redis‑dedicated machines.

AOF Configuration

Choose an appropriate appendfsync policy. always hurts performance; no risks data loss; everysec is a balance but can still block under heavy I/O. Optionally set no-appendfsync-on-rewrite yes during AOF rewrite.

CPU Binding

Binding Redis to a single CPU core can cause contention with child processes. Bind to multiple cores on the same physical CPU or use Redis 6.0’s server_cpulist, bio_cpulist, aof_rewrite_cpulist, and bgsave_cpulist settings.

Swap Usage

Swap dramatically slows Redis. Check swap with cat /proc/<pid>/smaps | egrep '^(Swap|Size)'. If significant, add memory or free swap by restarting the instance after a failover.

Memory Fragmentation

High mem_fragmentation_ratio (>1.5) indicates fragmentation. Enable activedefrag yes (Redis 4.0+) with appropriate thresholds, or restart for older versions.

Network Bandwidth

Monitor network traffic; a single instance saturating bandwidth can increase latency for all instances on the host.

Other Factors

Prefer long‑living connections over frequent short connections.

Ensure monitoring tools use long connections and reasonable polling intervals.

Dedicate servers to Redis to avoid resource contention from other applications.

Conclusion

Redis performance issues span CPU, memory, disk, and network layers. Understanding command complexities, expiration strategies, persistence mechanisms, OS features (COW, huge pages, swap), and proper resource planning enables effective diagnosis and optimization.

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.

optimizationdatabaseredisperformance tuningLatency
Sanyou's Java Diary
Written by

Sanyou's Java Diary

Passionate about technology, though not great at solving problems; eager to share, never tire of learning!

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.