- Memory Management
- Set
maxmemory
: Determine the maximum memory Redis can use based on your server’s resources. This prevents Redis from consuming all system memory, which could lead to crashes. Take a note of how much memory you actually have on the system (e.g in linuxfree -g -h -t
should show you how much total and how much you have left currently).maxmemory 4gb
- Choose an appropriate
maxmemory-policy
: For a semi-high traffic site,allkeys-lru
is a good option. It evicts the least recently used keys when Redis reaches the maxmemory limit, which helps maintain performance.maxmemory-policy allkeys-lru
- Set
- Persistence Configuration
- Enable AOF (Append-Only File): AOF provides durability by logging every write operation. This is useful for recovery in case of a crash.
appendonly yes
- Optimize AOF Rewrite: Set
auto-aof-rewrite-percentage
andauto-aof-rewrite-min-size
to reasonable values to avoid excessive disk I/O.auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb
- RDB Snapshots: If you don’t need real-time durability, you can use RDB snapshots instead of AOF or in combination. Configure the frequency based on your needs.
save 900 1 save 300 10 save 60 10000
- Enable AOF (Append-Only File): AOF provides durability by logging every write operation. This is useful for recovery in case of a crash.
- Performance Tuning
- Set
tcp-keepalive
: Ensure long connections stay open with a reasonable keepalive setting, which helps maintain connections during network interruptions. According to the docs “A reasonable value for this option is 300 seconds, which is the new Redis default starting with Redis 3.2.1”tcp-keepalive 300 #this is the default
- Adjust
timeout
: Set a timeout for idle clients to free up resources. This depends on your application’s needs but is typically set between 300-600 seconds.timeout 300
- Disable
swap
on your server: Redis performs best with no swapping, so ensure your system’s swap is disabled or minimal.
- Set
4. Network Optimization
• Bind to specific IPs: If you have multiple network interfaces, bind Redis to a specific one to avoid unintended exposure.
bind 127.0.0.1 192.168.1.100
• Enable protected-mode
: Ensure Redis is protected from external access by enabling protected mode, which restricts access to trusted clients only.
protected-mode yes
5. Security
• Require Password Authentication: Protect Redis with a strong password.
requirepass yourpassword
• Disable Dangerous Commands: Consider renaming or disabling commands like FLUSHALL
, FLUSHDB
, and SHUTDOWN
to prevent accidental or malicious use.
rename-command FLUSHALL "" rename-command FLUSHDB "" rename-command SHUTDOWN ""
6. Monitoring and Alerts
• Enable Slow Log: Track slow queries to identify performance bottlenecks.
slowlog-log-slower-than 10000 # Logs queries slower than 10ms slowlog-max-len 128
• Use Redis Monitoring Tools: Tools like Redis Sentinel for monitoring and failover, or third-party services like Datadog, can help monitor Redis performance and set up alerts.
7. Data Optimization
• Optimize Key Expiration: Use TTL (Time to Live) for keys that don’t need to persist indefinitely. This helps manage memory.
SET key value EX 60 # Expires in 60 seconds
• Data Structure Optimization: Choose appropriate data structures (e.g., strings, lists, sets) to minimize memory usage and maximize performance.
8. Scaling
• Consider Sharding: If traffic increases, consider Redis Cluster or external sharding to distribute the load across multiple instances.
Updating redis
Edit redis.conf
(usually /etc/redis/redis.conf
) and set your values. Also to set to the currently running redis instance, run redis-cli
, then CONFIG SET
(example CONFIG SET maxmemory 4G
) then CONFIG REWRITE
.
That’s all for now. Make sure to adjust the settings according to your specific server resources and traffic patterns. Feel free to leave your suggestions or questions in the comments!