Overview
Redis is designed for high performance, but proper configuration and system tuning can significantly improve throughput and latency. This guide covers performance optimization techniques.Memory Allocator
Redis memory performance is heavily influenced by the memory allocator choice.Allocator Options
Redis supports three memory allocators:- jemalloc (default on Linux)
- libc malloc
- tcmalloc
jemalloc (Recommended)
Default on Linux - Best for most workloads. Advantages:- Lower memory fragmentation
- Better performance with concurrent allocations
- Good for long-running Redis instances
- Optimized for multi-threaded scenarios
libc malloc
Default on macOS - Standard C library allocator. Use Cases:- Simple deployments
- When jemalloc causes issues
- Embedded systems
tcmalloc
Google’s TCMalloc - Alternative high-performance allocator. Advantages:- Very fast for small allocations
- Low overhead
Check Current Allocator
Memory Fragmentation
Monitor fragmentation ratio:- < 1.0: Redis is swapping (critical issue!)
- ~1.0: Ideal, no fragmentation
- 1.0 - 1.5: Normal, acceptable
- > 1.5: High fragmentation, consider:
- Restarting Redis
- Switching to jemalloc
- Reviewing data access patterns
CPU Optimization
I/O Threading
Redis is single-threaded for command execution but can use multiple I/O threads for network operations.- 4 cores: Use 3 I/O threads
- 8 cores: Use 6-7 I/O threads
- Always leave at least one core for main thread
- Test with redis-benchmark
Disable Transparent Huge Pages (THP)
THP can cause latency issues with fork().CPU Affinity
For critical deployments, pin Redis to specific CPU cores:Network Optimization
TCP Backlog
Increase backlog for high connection rates:TCP Keepalive
Detect dead connections faster:Client Timeout
Close idle connections:Memory Optimization
Set Maxmemory
Always set memory limit in production:Eviction Policies
Choose based on use case: Cache Use Case:Data Structure Optimization
Use Appropriate Data Types
Strings:Key Naming Conventions
Use consistent, short key names:- Use abbreviations for high-cardinality parts
- Keep structure identifiers readable
- Example:
user:123:name→u:123:n
Persistence Optimization
Optimize RDB Saves
Optimize AOF
Lazy Freeing
Use background deletion for large keys:Latency Optimization
Enable Latency Monitoring
Avoid Slow Commands
KEYS Command:Command Pipelining
Batch commands to reduce round trips: Without Pipelining:Use MGET/MSET
Operating System Tuning
Disable Swap
Swapping kills Redis performance:Increase File Descriptors
Memory Overcommit
Required for BGSAVE:TCP Optimizations
Benchmarking
Using redis-benchmark
Interpreting Results
Key metrics:- Requests per second: Higher is better
- Latency percentiles: Lower is better
- Throughput: MB/s processed
Real-World Testing
Test with your actual workload:Performance Monitoring
Key Metrics to Track
Performance Checklist
- Use jemalloc memory allocator
- Disable Transparent Huge Pages
- Set appropriate maxmemory and eviction policy
- Configure TCP backlog and kernel limits
- Enable lazy freeing for large keys
- Use pipelining and MGET/MSET
- Avoid KEYS and other O(N) commands
- Monitor and optimize slow queries
- Disable swap
- Enable I/O threading if needed
- Set appropriate persistence options
- Use SCAN instead of KEYS
- Optimize data structures
- Regular benchmarking and monitoring
- Keep Redis updated
Troubleshooting Performance Issues
High Latency
Check:- Review slow queries
- Check memory fragmentation
- Verify network latency
- Check CPU usage
- Review persistence settings
High Memory Usage
Check:- Set maxmemory limit
- Configure eviction policy
- Optimize data structures
- Check for memory leaks
- Review key expiration
Low Throughput
Check:- Enable I/O threading
- Use pipelining
- Optimize commands
- Check network bandwidth
- Review client connection pooling