Redis Troubleshooting Cheatsheet: Ultimate Guide for Optimal Performance and Scalability
Redis is a powerful in-memory data structure store that serves as a database, cache, and message broker. However, achieving optimal performance and scalability requires proper configuration, monitoring, and troubleshooting. This comprehensive cheatsheet provides essential commands and techniques for diagnosing and resolving Redis performance issues.
Performance Monitoring Commands
Memory Analysis
# Check memory usage and statistics redis-cli INFO memory # Monitor memory usage in real-time redis-cli --latency-history -i 1 # Analyze memory usage by key patterns redis-cli --bigkeys # Sample memory usage across keyspace redis-cli --memkeys --memkeys-samples 1000
Connection and Client Monitoring
# Monitor connected clients redis-cli CLIENT LIST # Track client connections over time redis-cli INFO clients # Monitor slow queries redis-cli SLOWLOG GET 10 # Real-time command monitoring redis-cli MONITOR
Performance Metrics
# Comprehensive server statistics redis-cli INFO stats # Check hit/miss ratios redis-cli INFO stats | grep -E "(keyspace_hits|keyspace_misses)" # Monitor operations per second redis-cli INFO stats | grep instantaneous_ops_per_sec
Memory Optimization Techniques
Memory Configuration
# Set maximum memory limit CONFIG SET maxmemory 2gb # Configure eviction policy CONFIG SET maxmemory-policy allkeys-lru # Enable memory optimization CONFIG SET hash-max-ziplist-entries 512 CONFIG SET hash-max-ziplist-value 64
Memory Troubleshooting
# Find memory-consuming keys redis-cli --bigkeys --bigkeys-samples 100000 # Analyze key expiration redis-cli EVAL " local keys = redis.call('KEYS', '*') local expired = 0 local total = 0 for i=1,#keys do total = total + 1 if redis.call('TTL', keys[i]) > 0 then expired = expired + 1 end end return {expired, total} " 0 # Check fragmentation ratio redis-cli INFO memory | grep mem_fragmentation_ratio
Latency Troubleshooting
Latency Monitoring
# Enable latency monitoring CONFIG SET latency-monitor-threshold 100 # Check latency events LATENCY LATEST # Historical latency data LATENCY HISTORY command # Reset latency statistics LATENCY RESET
Network Latency Diagnostics
# Test network latency redis-cli --latency -h redis-server -p 6379 # Continuous latency monitoring redis-cli --latency-history -h redis-server -p 6379 -i 1 # Intrinsic latency measurement redis-cli --intrinsic-latency 60
Connection Issues Resolution
Connection Pool Optimization
# Check current connections redis-cli INFO clients # Set connection limits CONFIG SET maxclients 10000 # Monitor connection patterns redis-cli CLIENT LIST | grep -E "(age|idle)" # Kill problematic connections redis-cli CLIENT KILL TYPE normal
Authentication and Security
# Set password authentication CONFIG SET requirepass your_secure_password # Enable ACL (Redis 6+) ACL SETUSER username on >password ~* &* +@all # List active ACL users ACL LIST # Monitor failed authentication attempts redis-cli INFO stats | grep rejected_connections
Persistence and Backup Optimization
RDB Configuration
# Configure RDB snapshots CONFIG SET save "900 1 300 10 60 10000" # Manual RDB backup BGSAVE # Check last save time LASTSAVE # Disable RDB if not needed CONFIG SET save ""
AOF Configuration
# Enable AOF persistence CONFIG SET appendonly yes # Set AOF sync policy CONFIG SET appendfsync everysec # Rewrite AOF file BGREWRITEAOF # Check AOF status redis-cli INFO persistence
Scaling and Clustering
Redis Cluster Diagnostics
# Check cluster status redis-cli CLUSTER INFO # List cluster nodes redis-cli CLUSTER NODES # Check slot distribution redis-cli CLUSTER SLOTS # Fix cluster issues redis-cli --cluster fix cluster-node:6379
Replication Monitoring
# Check replication status redis-cli INFO replication # Monitor replication lag redis-cli --latency-history -i 1 # Force replication sync redis-cli PSYNC ? -1
Performance Tuning Parameters
Critical Configuration Settings
# Optimize for high throughput CONFIG SET tcp-keepalive 60 CONFIG SET timeout 300 CONFIG SET tcp-backlog 511 # Memory optimization CONFIG SET maxmemory-samples 10 CONFIG SET hash-max-ziplist-entries 512 CONFIG SET list-max-ziplist-size -2 # Persistence tuning CONFIG SET rdbcompression yes CONFIG SET rdbchecksum yes CONFIG SET stop-writes-on-bgsave-error no
Operating System Optimizations
# Disable transparent huge pages echo never > /sys/kernel/mm/transparent_hugepage/enabled # Set overcommit memory echo 1 > /proc/sys/vm/overcommit_memory # Optimize network settings echo 'net.core.somaxconn = 65535' >> /etc/sysctl.conf echo 'net.ipv4.tcp_max_syn_backlog = 65535' >> /etc/sysctl.conf
Common Issues and Solutions
High Memory Usage
# Identify large keys redis-cli --bigkeys # Implement key expiration redis-cli EXPIRE key_name 3600 # Use memory-efficient data structures redis-cli EVAL " redis.call('HSET', 'hash_key', 'field1', 'value1') return redis.call('MEMORY', 'USAGE', 'hash_key') " 0
Slow Performance
# Analyze slow queries redis-cli SLOWLOG GET 100 # Check for blocking operations redis-cli INFO commandstats | grep -E "(del|flushdb|flushall)" # Optimize data structures redis-cli EVAL " local cursor = 0 local keys = {} repeat local result = redis.call('SCAN', cursor, 'COUNT', 1000) cursor = tonumber(result[1]) for i=1,#result[2] do table.insert(keys, result[2][i]) end until cursor == 0 return #keys " 0
Connection Timeouts
# Increase timeout settings CONFIG SET timeout 0 # Optimize connection pooling CONFIG SET tcp-keepalive 300 # Monitor connection lifecycle redis-cli CLIENT LIST | awk '{print $3}' | sort | uniq -c
Monitoring and Alerting Setup
Key Metrics to Monitor
# Create monitoring script cat > redis_monitor.sh << 'EOF' #!/bin/bash REDIS_HOST="localhost" REDIS_PORT="6379" # Memory usage MEM_USED=$(redis-cli -h $REDIS_HOST -p $REDIS_PORT INFO memory | grep used_memory_human | cut -d: -f2) echo "Memory Used: $MEM_USED" # Hit ratio HITS=$(redis-cli -h $REDIS_HOST -p $REDIS_PORT INFO stats | grep keyspace_hits | cut -d: -f2) MISSES=$(redis-cli -h $REDIS_HOST -p $REDIS_PORT INFO stats | grep keyspace_misses | cut -d: -f2) RATIO=$(echo "scale=2; $HITS/($HITS+$MISSES)*100" | bc) echo "Hit Ratio: $RATIO%" # Connected clients CLIENTS=$(redis-cli -h $REDIS_HOST -p $REDIS_PORT INFO clients | grep connected_clients | cut -d: -f2) echo "Connected Clients: $CLIENTS" EOF chmod +x redis_monitor.sh
Automated Health Checks
# Redis health check script cat > redis_health.sh << 'EOF' #!/bin/bash REDIS_CLI="redis-cli" THRESHOLD_MEMORY=80 THRESHOLD_CLIENTS=1000 # Check if Redis is responding if ! $REDIS_CLI ping > /dev/null 2>&1; then echo "CRITICAL: Redis is not responding" exit 2 fi # Check memory usage MEM_PERCENT=$($REDIS_CLI INFO memory | grep used_memory_rss | cut -d: -f2 | tr -d '\r') MAX_MEM=$($REDIS_CLI CONFIG GET maxmemory | tail -1) if [ "$MAX_MEM" != "0" ]; then USAGE=$(echo "scale=0; $MEM_PERCENT/$MAX_MEM*100" | bc) if [ "$USAGE" -gt "$THRESHOLD_MEMORY" ]; then echo "WARNING: Memory usage is ${USAGE}%" fi fi echo "Redis health check passed" EOF chmod +x redis_health.sh
Best Practices Summary
Configuration Optimization
- Set appropriate maxmemory limits
- Choose optimal eviction policies
- Configure persistence based on requirements
- Tune network and timeout settings
Monitoring Strategy
- Implement comprehensive monitoring
- Set up alerting for key metrics
- Regular performance baseline reviews
- Proactive capacity planning
Operational Excellence
- Regular backup verification
- Performance testing under load
- Documentation of configuration changes
- Incident response procedures
This cheatsheet provides a solid foundation for troubleshooting Redis performance issues and maintaining optimal scalability. Regular monitoring and proactive optimization using these techniques will ensure your Redis deployment performs efficiently under varying workloads.
Remember to test configuration changes in a development environment before applying them to production systems, and always maintain proper backups before making significant modifications.
Be the first to comment