Skip to content

Instantly share code, notes, and snippets.

@greentornado
Created August 12, 2024 04:07
Show Gist options
  • Select an option

  • Save greentornado/2728ce3acf25bf3fa61aa770a1217039 to your computer and use it in GitHub Desktop.

Select an option

Save greentornado/2728ce3acf25bf3fa61aa770a1217039 to your computer and use it in GitHub Desktop.

Redis Optimization Script

Overview

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.

Features

  • 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

Prerequisites

  • Root access to the system
  • Redis installed and configured
  • Basic understanding of Redis and system configuration

Installation

  1. 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

Usage

Run the script with root privileges: sudo ./optimize_redis.sh Copy

What the Script Does

  1. Ulimit Optimization

    • Adds entries to /etc/security/limits.conf to increase file descriptor limits for Redis.
  2. PAM Configuration

    • Adds pam_limits.so to /etc/pam.d/common-session to ensure limits are applied.
  3. Redis Configuration

    • Sets maxclients to 10000
    • Disables idle connection timeout
    • Enables TCP keepalive with a 300-second interval
  4. System Configuration

    • Increases net.core.somaxconn and net.ipv4.tcp_max_syn_backlog in sysctl
  5. Service Restart

    • Restarts the Redis service to apply changes

Caution

  • 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.

Contributing

Feel free to fork this project and submit pull requests with improvements or open issues if you find any problems.

#!/bin/bash
# Check if script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Function to add line if it doesn't exist
add_line_if_not_exists() {
local file="$1"
local line="$2"
grep -qF -- "$line" "$file" || echo "$line" >> "$file"
}
# Optimize ulimit
echo "Optimizing ulimit..."
add_line_if_not_exists "/etc/security/limits.conf" "redis soft nofile 65535"
add_line_if_not_exists "/etc/security/limits.conf" "redis hard nofile 65535"
# Add pam_limits to common-session
add_line_if_not_exists "/etc/pam.d/common-session" "session required pam_limits.so"
# Optimize Redis connections
echo "Optimizing Redis connections..."
sed -i '/^# *maxclients/c\maxclients 10000' /etc/redis/redis.conf
# Increase TCP backlog
echo "Increasing TCP backlog..."
add_line_if_not_exists "/etc/sysctl.conf" "net.core.somaxconn = 65535"
add_line_if_not_exists "/etc/sysctl.conf" "net.ipv4.tcp_max_syn_backlog = 65535"
# Apply sysctl changes
sysctl -p
# Additional Redis optimizations
echo "Applying additional Redis optimizations..."
sed -i '/^timeout/c\timeout 0' /etc/redis/redis.conf
sed -i '/^# *tcp-keepalive/c\tcp-keepalive 300' /etc/redis/redis.conf
# Restart Redis
echo "Restarting Redis..."
systemctl restart redis
echo "Redis optimization complete. Please check the changes and restart your system if necessary."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment