Redis & Advanced Caching

Monitoring and Debugging Redis

20 min Lesson 24 of 30

Monitoring and Debugging Redis

Effective monitoring and debugging are crucial for maintaining Redis performance, identifying issues before they impact users, and optimizing resource usage in production environments.

INFO Command - Comprehensive Server Statistics

The INFO command provides detailed information about Redis server status, organized into sections:

Basic INFO Usage:
# Get all information
INFO

# Get specific section
INFO server
INFO clients
INFO memory
INFO persistence
INFO stats
INFO replication
INFO cpu
INFO commandstats
INFO cluster
INFO keyspace
Key Metrics to Monitor:
# Server section
redis_version:7.0.5
uptime_in_seconds:864000
uptime_in_days:10

# Clients section
connected_clients:42
blocked_clients:0
tracking_clients:0

# Memory section
used_memory_human:2.50M
used_memory_rss_human:8.20M
mem_fragmentation_ratio:1.23
maxmemory_human:4.00G

# Stats section
total_connections_received:10523
total_commands_processed:1045234
instantaneous_ops_per_sec:1250
keyspace_hits:984521
keyspace_misses:60713
evicted_keys:0
expired_keys:12453
Critical Metrics:
  • mem_fragmentation_ratio: Should be 1.0-1.5 (higher = wasted memory)
  • keyspace_hits/misses: Hit ratio = hits/(hits+misses), aim for >80%
  • evicted_keys: Non-zero = memory pressure, consider increasing maxmemory
  • connected_clients: Monitor for connection leaks
  • instantaneous_ops_per_sec: Current throughput

MONITOR Command - Real-time Command Logging

MONITOR shows every command processed by Redis in real-time, useful for debugging but impacts performance:

Start Monitoring:
redis-cli MONITOR

Example Output:
1634567890.123456 [0 127.0.0.1:54321] "GET" "user:1000:name"
1634567890.234567 [0 127.0.0.1:54322] "SET" "session:abc" "data"
1634567890.345678 [0 192.168.1.50:12345] "HGETALL" "cart:5000"
1634567890.456789 [0 127.0.0.1:54321] "INCR" "counter:pageviews"
Performance Warning: MONITOR significantly reduces Redis throughput (up to 50%). Only use for short debugging sessions, never in production under heavy load.
Filter MONITOR Output:
# Monitor only SET commands
redis-cli MONITOR | grep SET

# Monitor commands from specific IP
redis-cli MONITOR | grep "192.168.1.50"

# Monitor commands on specific keys
redis-cli MONITOR | grep "user:*"

# Save to file for analysis
redis-cli MONITOR > redis-monitor.log

SLOWLOG - Slow Query Analysis

SLOWLOG records queries that exceed a configured threshold, essential for identifying performance bottlenecks:

Configure Slow Log:
# Set threshold to 10ms (10000 microseconds)
CONFIG SET slowlog-log-slower-than 10000

# Keep last 128 slow queries
CONFIG SET slowlog-max-len 128

# Make permanent in redis.conf
slowlog-log-slower-than 10000
slowlog-max-len 128
View Slow Log:
# Get last 10 slow queries
SLOWLOG GET 10

Output Format:
1) 1) (integer) 14 # Unique ID
2) (integer) 1634567890 # Unix timestamp
3) (integer) 23451 # Execution time (microseconds)
4) 1) "KEYS" # Command
2) "user:*" # Arguments
5) "127.0.0.1:54321" # Client IP:port
6) "" # Client name

# Get number of entries in log
SLOWLOG LEN

# Clear slow log
SLOWLOG RESET
Analysis Tip: Common slow commands include KEYS (use SCAN instead), SORT without LIMIT, and operations on large collections (lists with millions of items).

redis-cli Diagnostic Tools

redis-cli includes built-in tools for performance analysis and diagnostics:

--bigkeys - Find Largest Keys:
redis-cli --bigkeys

Sample Output:
Scanning the entire keyspace...
Biggest string: "cache:homepage" (2048576 bytes)
Biggest list: "queue:emails" (50000 items)
Biggest set: "tags:all" (8934 members)
Biggest hash: "user:1000:profile" (1000 fields)
Biggest zset: "leaderboard:global" (100000 members)

# Options
--bigkeys --i 0.1 # Sample every 0.1 seconds
--bigkeys -a mypass # With authentication
--latency - Measure Latency:
# Basic latency test
redis-cli --latency

# Output: min: 0, max: 2, avg: 0.15 (1234 samples)

# Latency history (tracks over time)
redis-cli --latency-history

# Latency distribution
redis-cli --latency-dist

# Intrinsic latency (system delays)
redis-cli --intrinsic-latency 100
--stat - Live Statistics:
redis-cli --stat

Output (updates every second):
------- data ------ --------------------- load -------------------- - child -
keys mem clients blocked requests connections
506 1.45M 10 0 10000 (+0) 50
506 1.45M 10 0 10234 (+234) 50
506 1.47M 11 0 10567 (+333) 51
--scan - Safe Key Scanning:
# Scan all keys
redis-cli --scan

# Scan with pattern
redis-cli --scan --pattern "user:*"

# Count keys matching pattern
redis-cli --scan --pattern "session:*" | wc -l

# Delete keys matching pattern safely
redis-cli --scan --pattern "temp:*" | xargs redis-cli DEL

Memory Analysis Commands

Detailed memory usage analysis for optimization:

MEMORY Commands:
# Memory usage of specific key
MEMORY USAGE user:1000
# Output: (integer) 1024

# Memory statistics
MEMORY STATS
# Output: Detailed breakdown of memory allocation

# Memory doctor (optimization suggestions)
MEMORY DOCTOR
# Output: Suggestions based on current usage patterns

# Memory usage by sample
MEMORY MALLOC-STATS

# Purge memory (run defragmentation)
MEMORY PURGE
Analyze Memory Fragmentation:
INFO memory | grep fragmentation
# mem_fragmentation_ratio:1.23

# If ratio > 1.5, consider:
# 1. Restart Redis to defragment
# 2. Enable active defragmentation
CONFIG SET activedefrag yes
CONFIG SET active-defrag-threshold-lower 10
CONFIG SET active-defrag-threshold-upper 100

CLIENT Commands - Connection Debugging

Monitor and manage client connections:

Client Management:
# List all connected clients
CLIENT LIST

Output Format:
id=123 addr=127.0.0.1:54321 fd=8 name= age=300 idle=5 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get

# Fields explanation:
# id: Unique client ID
# addr: Client IP and port
# age: Connection age (seconds)
# idle: Idle time (seconds)
# flags: N=normal, M=master, S=slave, P=pubsub
# db: Current database number
# cmd: Last command executed
Client Operations:
# Get current client ID
CLIENT ID

# Set client name
CLIENT SETNAME myapp-web-1

# Get client name
CLIENT GETNAME

# Kill specific client
CLIENT KILL 127.0.0.1:54321

# Kill all clients matching filter
CLIENT KILL TYPE normal SKIPME yes

# Pause all clients for 1000ms
CLIENT PAUSE 1000

# Track client memory
CLIENT TRACKING ON

Latency Monitoring

Detect and diagnose latency spikes:

Enable Latency Monitoring:
# Monitor events taking > 100ms
CONFIG SET latency-monitor-threshold 100

# View recent latency events
LATENCY LATEST

Output:
1) 1) "command"
2) (integer) 1634567890
3) (integer) 250
4) (integer) 350

# View latency history for specific event
LATENCY HISTORY command

# Latency doctor (diagnosis)
LATENCY DOCTOR

# Reset latency data
LATENCY RESET
Latency Events:
command # Slow commands
fast-command # Fast command taking too long
fork # Fork operation for persistence
aof-write # AOF write operation
aof-fsync-always # AOF sync to disk
rdb-unlink-temp-file # RDB file cleanup

Redis Insight - GUI Monitoring Tool

Redis Insight provides a graphical interface for monitoring, profiling, and debugging:

Redis Insight Features:
  • Browser: Visual key browser with search and filters
  • Profiler: Real-time command profiling (like MONITOR)
  • Slowlog: Visual slow query analysis
  • Memory Analysis: Memory usage charts and recommendations
  • CLI: Built-in command-line interface
  • Cluster Management: Multi-node cluster overview
  • Workbench: Execute commands and scripts
Install Redis Insight:
# Download from RedisLabs
https://redis.com/redis-enterprise/redis-insight/

# Or via Docker
docker run -d -p 8001:8001 redislabs/redisinsight:latest

# Access at http://localhost:8001

Command Statistics

Track which commands are used most frequently:

View Command Stats:
INFO commandstats

Output:
cmdstat_get:calls=458234,usec=1234567,usec_per_call=2.69
cmdstat_set:calls=312456,usec=987654,usec_per_call=3.16
cmdstat_hgetall:calls=12345,usec=456789,usec_per_call=37.01

# Reset command stats
CONFIG RESETSTAT
Optimization Tip: Commands with high usec_per_call are candidates for optimization. Look for HGETALL on large hashes, SMEMBERS on large sets, or KEYS usage.

Persistence Monitoring

Monitor RDB and AOF operations:

RDB Status:
INFO persistence

rdb_last_save_time:1634567890
rdb_changes_since_last_save:12345
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:5
rdb_current_bgsave_time_sec:-1

AOF Status:
aof_enabled:1
aof_rewrite_in_progress:0
aof_last_rewrite_time_sec:12
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

Debugging Common Issues

Issue: High Memory Usage
1. Run redis-cli --bigkeys to find large keys
2. Check INFO memory for mem_fragmentation_ratio
3. Review evicted_keys - if >0, increase maxmemory
4. Check for keys without TTL: redis-cli --scan | xargs redis-cli TTL | grep -c "\-1"

Issue: Slow Performance
1. Check SLOWLOG GET 100 for slow queries
2. Run MONITOR briefly to see live commands
3. Verify INFO stats for keyspace_misses ratio
4. Check CLIENT LIST for blocked clients

Issue: Connection Problems
1. Check INFO clients for connected_clients vs maxclients
2. Look for rejected_connections in INFO stats
3. Review timeout setting in redis.conf
4. Check network latency with redis-cli --latency

Monitoring Best Practices

Production Monitoring Checklist:
  • ✅ Set up automated INFO collection every 60 seconds
  • ✅ Alert on mem_fragmentation_ratio > 1.5
  • ✅ Alert on keyspace hit ratio < 80%
  • ✅ Alert on evicted_keys > 0
  • ✅ Monitor connected_clients for leaks
  • ✅ Review SLOWLOG daily for optimization opportunities
  • ✅ Track instantaneous_ops_per_sec for capacity planning
  • ✅ Monitor used_memory vs maxmemory
  • ✅ Set latency-monitor-threshold and review LATENCY DOCTOR
  • ✅ Use Redis Insight for visual troubleshooting
Exercise: Set up comprehensive Redis monitoring: 1) Create a script that runs INFO every 60 seconds and logs key metrics, 2) Enable SLOWLOG with 10ms threshold and analyze slow queries, 3) Use CLIENT LIST to identify idle connections, 4) Run redis-cli --bigkeys to find memory-intensive keys, 5) Check memory fragmentation and enable active defragmentation if needed.