This bash script (optimize_redis.sh) automates the process of optimizing a Redis server on a Linux system. It applies several system-level and Redis-specific configuration changes to improve performance and handle a higher number of connections.
- Increases file descriptor limits for Redis
- Configures PAM to apply the limits
- Optimizes Redis connection settings
- Increases system-wide TCP backlog
- Adjusts Redis timeout and keepalive settings
- Implements idempotent execution for safe multiple runs
- Root access to the system
- Redis installed and configured
- Basic understanding of Redis and system configuration
- Download the script: wget https://raw.githubusercontent.com/yourusername/redis-optimization/main/optimize_redis.sh Copy2. Make the script executable: chmod +x optimize_redis.sh Copy
Run the script with root privileges: sudo ./optimize_redis.sh Copy
-
Ulimit Optimization
- Adds entries to
/etc/security/limits.confto increase file descriptor limits for Redis.
- Adds entries to
-
PAM Configuration
- Adds
pam_limits.soto/etc/pam.d/common-sessionto ensure limits are applied.
- Adds
-
Redis Configuration
- Sets
maxclientsto 10000 - Disables idle connection timeout
- Enables TCP keepalive with a 300-second interval
- Sets
-
System Configuration
- Increases
net.core.somaxconnandnet.ipv4.tcp_max_syn_backlogin sysctl
- Increases
-
Service Restart
- Restarts the Redis service to apply changes
- This script makes system-wide changes. Review and understand its actions before running, especially in production environments.
- Always test in a non-production environment first.
- Back up your configuration files before running the script.
- You may need to reboot the system for all changes to take effect.
- These optimizations may need adjustment based on your specific hardware, workload, and requirements.
Feel free to fork this project and submit pull requests with improvements or open issues if you find any problems.