Chapter 2: Installation and Configuration
Authored by syscook.dev
What is Redis Installation and Configuration?
Redis installation involves setting up the Redis server on your system, while configuration involves customizing Redis settings to meet your specific requirements. Proper installation and configuration are essential for optimal performance, security, and reliability.
Key Areas:
- Installation Methods: Package managers, source compilation, Docker
- Configuration Files: redis.conf settings and parameters
- Security Configuration: Authentication, network security, encryption
- Performance Tuning: Memory optimization, persistence settings
- Monitoring Setup: Logging, metrics, and health checks
- Production Readiness: High availability, backup strategies
Why Proper Installation and Configuration Matter?
1. Performance Optimization
Correct configuration ensures optimal performance for your use case.
# Example: Performance-focused configuration
# redis.conf
# Memory management
maxmemory 2gb
maxmemory-policy allkeys-lru
# Persistence settings
save 900 1
save 300 10
save 60 10000
# Network settings
tcp-keepalive 300
timeout 0
# Performance tuning
tcp-backlog 511
databases 16
Configuration Benefits:
- Optimized memory usage
- Efficient persistence strategy
- Network performance tuning
- Resource allocation optimization
2. Security Hardening
Proper security configuration protects your Redis instance.
# Example: Security configuration
# redis.conf
# Authentication
requirepass your_strong_password_here
# Network security
bind 127.0.0.1 10.0.0.0/8
protected-mode yes
port 6379
# Command renaming for security
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG ""
rename-command CONFIG "CONFIG_b835729c"
# SSL/TLS configuration
tls-port 6380
tls-cert-file /path/to/redis.crt
tls-key-file /path/to/redis.key
tls-ca-cert-file /path/to/ca.crt
3. Production Readiness
Production configuration ensures reliability and maintainability.
# Example: Production configuration
# redis.conf
# Logging
loglevel notice
logfile /var/log/redis/redis-server.log
# Persistence
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# Memory management
maxmemory 4gb
maxmemory-policy allkeys-lru
maxmemory-samples 5
# Slow log
slowlog-log-slower-than 10000
slowlog-max-len 128
How to Install and Configure Redis?
1. Installation Methods
Ubuntu/Debian Installation
# Update package list
sudo apt update
# Install Redis
sudo apt install redis-server
# Start Redis service
sudo systemctl start redis-server
# Enable Redis to start on boot
sudo systemctl enable redis-server
# Check Redis status
sudo systemctl status redis-server
# Test Redis connection
redis-cli ping
# Should return: PONG
CentOS/RHEL Installation
# Install EPEL repository
sudo yum install epel-release
# Install Redis
sudo yum install redis
# Start Redis service
sudo systemctl start redis
# Enable Redis to start on boot
sudo systemctl enable redis
# Check Redis status
sudo systemctl status redis
# Test Redis connection
redis-cli ping
macOS Installation
# Install using Homebrew
brew install redis
# Start Redis service
brew services start redis
# Or start manually
redis-server
# Test Redis connection
redis-cli ping
Docker Installation
# Pull Redis image
docker pull redis:latest
# Run Redis container
docker run -d --name redis-server -p 6379:6379 redis:latest
# Run with custom configuration
docker run -d --name redis-server \
-p 6379:6379 \
-v /path/to/redis.conf:/usr/local/etc/redis/redis.conf \
redis:latest redis-server /usr/local/etc/redis/redis.conf
# Run with password
docker run -d --name redis-server \
-p 6379:6379 \
redis:latest redis-server --requirepass your_password
# Test connection
docker exec -it redis-server redis-cli ping
Source Compilation
# Download Redis source
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
# Compile Redis
make
# Install Redis
sudo make install
# Create Redis user
sudo adduser --system --group --no-create-home redis
# Create directories
sudo mkdir -p /var/lib/redis
sudo mkdir -p /var/log/redis
sudo mkdir -p /etc/redis
# Set permissions
sudo chown redis:redis /var/lib/redis
sudo chown redis:redis /var/log/redis
sudo chown redis:redis /etc/redis
2. Configuration Management
Basic Configuration File
# /etc/redis/redis.conf
# Network configuration
bind 127.0.0.1
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
# General configuration
daemonize yes
supervised systemd
pidfile /var/run/redis/redis-server.pid
loglevel notice
logfile /var/log/redis/redis-server.log
databases 16
# Snapshotting (RDB)
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
# Replication
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-ping-replica-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
repl-backlog-size 1mb
repl-backlog-ttl 3600
# Security
requirepass your_strong_password_here
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG ""
rename-command CONFIG "CONFIG_b835729c"
# Clients
maxclients 10000
# Memory management
maxmemory 2gb
maxmemory-policy allkeys-lru
maxmemory-samples 5
# Append only file (AOF)
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
# Lua scripting
lua-time-limit 5000
# Slow log
slowlog-log-slower-than 10000
slowlog-max-len 128
# Latency monitoring
latency-monitor-threshold 0
# Event notification
notify-keyspace-events ""
# Advanced configuration
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
Production Configuration
# Production Redis configuration
# /etc/redis/redis-production.conf
# Network security
bind 10.0.0.100
port 6379
protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
# Security
requirepass your_very_strong_password_here
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG ""
rename-command CONFIG "CONFIG_b835729c"
rename-command SHUTDOWN "SHUTDOWN_b835729c"
# Logging
loglevel notice
logfile /var/log/redis/redis-production.log
syslog-enabled yes
syslog-ident redis
syslog-facility local0
# Persistence
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
# AOF
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# Memory management
maxmemory 8gb
maxmemory-policy allkeys-lru
maxmemory-samples 5
# Performance tuning
databases 16
hz 10
dynamic-hz yes
# Monitoring
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 100
# Client limits
maxclients 10000
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
3. Security Configuration
Authentication Setup
# Set password in redis.conf
requirepass your_strong_password_here
# Or set password at runtime
redis-cli
127.0.0.1:6379> CONFIG SET requirepass "your_strong_password_here"
OK
127.0.0.1:6379> AUTH your_strong_password_here
OK
# Connect with password
redis-cli -a your_strong_password_here
# Or authenticate after connection
redis-cli
127.0.0.1:6379> AUTH your_strong_password_here
OK
Network Security
# Bind to specific interfaces
bind 127.0.0.1 10.0.0.0/8
# Use protected mode
protected-mode yes
# Change default port
port 6380
# Use SSL/TLS
tls-port 6380
tls-cert-file /path/to/redis.crt
tls-key-file /path/to/redis.key
tls-ca-cert-file /path/to/ca.crt
tls-protocols "TLSv1.2 TLSv1.3"
Command Security
# Disable dangerous commands
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG ""
rename-command CONFIG "CONFIG_b835729c"
rename-command SHUTDOWN "SHUTDOWN_b835729c"
rename-command EVAL "EVAL_b835729c"
# Rename commands with random strings
rename-command CONFIG "CONFIG_$(openssl rand -hex 8)"
4. Performance Tuning
Memory Optimization
# Set maximum memory
maxmemory 4gb
# Set eviction policy
maxmemory-policy allkeys-lru
# Optimize data structures
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
Persistence Optimization
# RDB optimization
save 900 1
save 300 10
save 60 10000
rdbcompression yes
rdbchecksum yes
# AOF optimization
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
Network Optimization
# Network settings
tcp-backlog 511
tcp-keepalive 300
timeout 0
# Client buffer limits
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
Practical Examples
1. Complete Installation Script
Ubuntu/Debian Installation Script
#!/bin/bash
# install-redis-ubuntu.sh
# Update system
sudo apt update && sudo apt upgrade -y
# Install Redis
sudo apt install redis-server -y
# Create Redis user
sudo adduser --system --group --no-create-home redis
# Create directories
sudo mkdir -p /var/lib/redis
sudo mkdir -p /var/log/redis
sudo mkdir -p /etc/redis
# Set permissions
sudo chown redis:redis /var/lib/redis
sudo chown redis:redis /var/log/redis
sudo chown redis:redis /etc/redis
# Create systemd service file
sudo tee /etc/systemd/system/redis.service > /dev/null <<EOF
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=redis
Group=redis
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/bin/redis-cli shutdown
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd
sudo systemctl daemon-reload
# Enable and start Redis
sudo systemctl enable redis
sudo systemctl start redis
# Check status
sudo systemctl status redis
echo "Redis installation completed successfully!"
Docker Compose Setup
# docker-compose.yml
version: '3.8'
services:
redis:
image: redis:7-alpine
container_name: redis-server
ports:
- "6379:6379"
volumes:
- redis_data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
restart: unless-stopped
environment:
- REDIS_PASSWORD=your_strong_password_here
networks:
- redis_network
redis-commander:
image: rediscommander/redis-commander:latest
container_name: redis-commander
ports:
- "8081:8081"
environment:
- REDIS_HOSTS=local:redis:6379:0:your_strong_password_here
depends_on:
- redis
networks:
- redis_network
volumes:
redis_data:
networks:
redis_network:
driver: bridge
2. Configuration Management
Environment-Specific Configurations
#!/bin/bash
# setup-redis-configs.sh
# Development configuration
cat > redis-dev.conf <<EOF
# Development Redis Configuration
bind 127.0.0.1
port 6379
daemonize no
loglevel debug
logfile ""
save ""
appendonly no
maxmemory 256mb
maxmemory-policy allkeys-lru
EOF
# Staging configuration
cat > redis-staging.conf <<EOF
# Staging Redis Configuration
bind 127.0.0.1 10.0.0.0/8
port 6379
daemonize yes
loglevel notice
logfile /var/log/redis/redis-staging.log
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
maxmemory 2gb
maxmemory-policy allkeys-lru
requirepass staging_password_here
EOF
# Production configuration
cat > redis-production.conf <<EOF
# Production Redis Configuration
bind 10.0.0.100
port 6379
daemonize yes
loglevel notice
logfile /var/log/redis/redis-production.log
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
maxmemory 8gb
maxmemory-policy allkeys-lru
requirepass production_strong_password_here
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG ""
rename-command CONFIG "CONFIG_$(openssl rand -hex 8)"
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 100
EOF
echo "Redis configurations created successfully!"
3. Security Hardening
Security Setup Script
#!/bin/bash
# secure-redis.sh
# Generate strong password
REDIS_PASSWORD=$(openssl rand -base64 32)
# Generate random command names
CONFIG_CMD="CONFIG_$(openssl rand -hex 8)"
SHUTDOWN_CMD="SHUTDOWN_$(openssl rand -hex 8)"
DEBUG_CMD="DEBUG_$(openssl rand -hex 8)"
# Create secure configuration
cat > redis-secure.conf <<EOF
# Secure Redis Configuration
bind 127.0.0.1
port 6379
protected-mode yes
requirepass $REDIS_PASSWORD
# Disable dangerous commands
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG "$DEBUG_CMD"
rename-command CONFIG "$CONFIG_CMD"
rename-command SHUTDOWN "$SHUTDOWN_CMD"
# Logging
loglevel notice
logfile /var/log/redis/redis-secure.log
# Persistence
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
# Memory management
maxmemory 2gb
maxmemory-policy allkeys-lru
# Performance
tcp-keepalive 300
timeout 0
EOF
# Save credentials securely
echo "Redis Password: $REDIS_PASSWORD" > redis-credentials.txt
echo "Config Command: $CONFIG_CMD" >> redis-credentials.txt
echo "Shutdown Command: $SHUTDOWN_CMD" >> redis-credentials.txt
echo "Debug Command: $DEBUG_CMD" >> redis-credentials.txt
chmod 600 redis-credentials.txt
echo "Secure Redis configuration created!"
echo "Credentials saved to redis-credentials.txt"
echo "Please store credentials securely and delete this file!"
Best Practices
1. Security Configuration
# Always use strong passwords
requirepass $(openssl rand -base64 32)
# Disable dangerous commands
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG ""
# Use protected mode
protected-mode yes
# Bind to specific interfaces
bind 127.0.0.1 10.0.0.0/8
2. Performance Optimization
# Set appropriate memory limits
maxmemory 4gb
maxmemory-policy allkeys-lru
# Optimize persistence
appendonly yes
appendfsync everysec
# Tune data structures
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
3. Monitoring Setup
# Enable slow log
slowlog-log-slower-than 10000
slowlog-max-len 128
# Enable latency monitoring
latency-monitor-threshold 100
# Set appropriate log level
loglevel notice
Common Pitfalls and Solutions
1. Memory Issues
# ❌ Problem: Out of memory errors
# Solution: Set appropriate memory limits and eviction policy
# Set memory limit
maxmemory 4gb
# Set eviction policy
maxmemory-policy allkeys-lru
# Monitor memory usage
redis-cli INFO memory
2. Security Issues
# ❌ Problem: Redis accessible without authentication
# Solution: Enable authentication and network security
# Set password
requirepass your_strong_password_here
# Enable protected mode
protected-mode yes
# Bind to specific interfaces
bind 127.0.0.1
3. Performance Issues
# ❌ Problem: Slow Redis performance
# Solution: Optimize configuration and monitor
# Check slow log
redis-cli SLOWLOG GET 10
# Monitor latency
redis-cli --latency
# Check memory usage
redis-cli INFO memory
Conclusion
Proper Redis installation and configuration are essential for optimal performance, security, and reliability. By understanding:
- What installation methods and configuration options are available
- Why proper configuration is crucial for production environments
- How to install and configure Redis for different use cases
You can set up Redis instances that are secure, performant, and ready for production use. Proper configuration ensures that your Redis deployment meets your specific requirements and provides the best possible performance.
Next Steps
- Practice with different configuration scenarios
- Learn about Redis data types and operations
- Move on to Chapter 3: Data Types and Operations
This tutorial is part of the Redis Mastery series by syscook.dev