Databases
Redis Troubleshooting: Memory, Slow Queries and Latency
Redis performance issues rarely announce themselves clearly. A latency spike might be caused by a slow command, a large key serialize, AOF fsync, or a KEYS scan on a large keyspace. This guide covers the diagnostic commands and inspection patterns for each category.
Diagnosing Redis Latency Spikes
When Redis response time increases suddenly, the first diagnostic step is `redis-cli --latency -h <host>` to measure baseline roundtrip latency. If latency is consistently elevated rather than spiking, the problem is likely a slow command blocking the single-threaded event loop. If latency spikes irregularly, AOF fsync or background save (BGSAVE) is a common culprit.
Run `redis-cli SLOWLOG GET 25` to retrieve the last 25 commands that exceeded the `slowlog-log-slower-than` threshold (default 10,000 microseconds = 10ms). The output includes execution time, timestamp, and the full command. KEYS, SMEMBERS on large sets, LRANGE over large lists, and SORT without LIMIT are the most frequent offenders.
MONITOR mode (`redis-cli MONITOR`) streams all commands hitting the server in real time. Use it briefly to identify unexpected command patterns, but disable it immediately in production — MONITOR itself adds significant overhead on a busy Redis instance.
Memory Fragmentation and Eviction Diagnosis
The `mem_fragmentation_ratio` from `INFO memory` measures the ratio of RSS memory (allocated by OS) to used memory (reported by Redis). A ratio above 1.5 indicates significant fragmentation — Redis has allocated memory it is not actively using, causing apparent memory waste.
Redis 4.0+ supports active defragmentation via `CONFIG SET activedefrag yes`. This background process reorganizes memory to reduce fragmentation without requiring a restart. Enable it during low-traffic periods and monitor `active_defrag_running` in `INFO stats` to verify it is working.
Key eviction triggered by `maxmemory` policy can silently discard session data, cache entries, and rate limit counters. Monitor `evicted_keys` in `INFO stats` — any non-zero value in a production cache-aside pattern deserves investigation. If eviction is expected (LRU cache), verify the `maxmemory-policy` is set to the intended algorithm.
Inspecting Redis Over SSH Tunnels with ShellMate
Production Redis instances should never expose port 6379 to the public internet. Access them securely over SSH local port forwarding: `ssh -L 6379:localhost:6379 user@appserver` then connect `redis-cli -h localhost` to reach the remote Redis instance through the encrypted tunnel.
ShellMate Redis plugin automates this connection pattern — it establishes the SSH tunnel automatically when you open the Redis plugin for a configured host, then presents a full key browser, memory dashboard, and interactive CLI console without requiring manual port forwarding commands.
In the plugin's command console, run `CLIENT LIST` to inspect active connections, `DEBUG SLEEP 0` to measure roundtrip time, and `OBJECT ENCODING <key>` to understand data structure storage efficiency for specific keys.
Related ShellMate resources
Questions about Redis Troubleshooting
How do I find slow Redis commands in production?
Run `redis-cli SLOWLOG GET 25` to see commands that exceeded the slowlog threshold. Check for KEYS, SMEMBERS on large sets, unbound LRANGE, and SORT without LIMIT — these are common event loop blockers in production Redis instances.
What does high mem_fragmentation_ratio mean?
A mem_fragmentation_ratio above 1.5 means Redis's OS-allocated memory (RSS) is significantly larger than its reported used memory, indicating memory fragmentation. Enable active defragmentation with CONFIG SET activedefrag yes on Redis 4.0+ to address this without a restart.
How can I access Redis securely without exposing port 6379?
Use SSH local port forwarding: ssh -L 6379:localhost:6379 user@server, then connect via redis-cli -h localhost. ShellMate Redis plugin automates this with a built-in SSH tunnel, presenting a full GUI key browser and command console over the encrypted connection.
Put the guide into practice
Organize hosts, choose a deliberate credential path, and keep SSH work visible in a focused desktop workspace.