14. Monitoring & Performance Tuning
Monitoring and Tuning Redis Performance
Monitoring is essential for maintaining Redis performance and reliability. This module covers key metrics, monitoring tools, and performance tuning. You'll learn to identify and fix performance issues before they impact users.
Redis performance monitoring includes CPU, memory, network, and command latency. Use INFO command for metrics. Use SLOWLOG for slow queries. Use MONITOR for debugging. Monitoring is essential for production deployments. This module covers each aspect in depth with practical examples.
Key Performance Metrics
Key Redis metrics include memory usage, CPU usage, network traffic, command latency, and hit ratio. Monitor used_memory, used_memory_peak, and memory fragmentation. Monitor total_commands_processed and instantaneous_ops_per_sec. Monitor cache hit ratio and evicted keys. These metrics indicate Redis health and performance.
Memory metrics are critical — if Redis runs out of memory, it evicts keys or crashes. Monitor used_memory and used_memory_peak to track memory growth. Monitor mem_fragmentation_ratio — high fragmentation indicates memory issues. Command metrics show workload patterns. Cache hit ratio indicates cache effectiveness.
# Redis INFO command INFO memory # Memory statistics INFO stats # General statistics INFO commandstats # Command statistics INFO cpu # CPU usage INFO clients # Client connections # Key metrics used_memory # Total memory used used_memory_peak # Peak memory usage mem_fragmentation_ratio # Memory fragmentation instantaneous_ops_per_sec # Current operations per second total_commands_processed # Total commands processed keyspace_hits # Cache hits keyspace_misses # Cache misses evicted_keys # Evicted keys # Monitor command latency redis-cli --latency -h localhost -p 6379 redis-cli --latency-history -h localhost -p 6379
Slow Log and Monitoring
SLOWLOG captures slow queries. Configure slowlog-log-slower-than to set the threshold. Use SLOWLOG GET to view queries. Use MONITOR for real-time debugging. These tools help identify performance issues.
Slow log is essential for performance tuning. It shows commands that exceed the latency threshold. Analyze slow log to find problematic commands. Use MONITOR for debugging specific issues, but be careful — MONITOR can impact performance in production.
# Slow log configuration CONFIG SET slowlog-log-slower-than 10000 # 10ms threshold CONFIG SET slowlog-max-len 128 # Keep 128 entries # View slow log SLOWLOG GET 10 # Get 10 slowest queries SLOWLOG LEN # Get number of slow log entries SLOWLOG RESET # Clear slow log # Real-time monitoring MONITOR # Monitor all commands in real time redis-cli --stat # Show real-time statistics redis-cli --bigkeys # Scan for large keys
Performance Tuning
Performance tuning optimizes Redis for your workload. Tune memory settings (maxmemory, maxmemory-policy). Tune connection settings. Use appropriate data structures. Optimize your data model. Monitor and adjust tuning parameters.
Common tuning parameters: maxmemory sets the memory limit. maxmemory-policy controls eviction (allkeys-lru, volatile-lru, noeviction). timeout closes idle connections. tcp-keepalive keeps connections alive. Slowlog threshold captures slow queries. Tune these based on your workload.
- Memory Tuning: Set maxmemory and eviction policy (allkeys-lru for caching).
- Connection Tuning: Set maxclients and timeout for idle connections.
- Data Structures: Use appropriate structures for your use case.
- Data Model: Optimize data model for performance.
- Monitoring: Continuously monitor and adjust tuning parameters.
Knowledge Check
Ready to test your understanding of 14. Monitoring & Performance Tuning?