Introduction
Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. This guide will walk you through the installation and advanced configuration of Redis on Debian and RedHat-based systems.
Installation
On Debian
# Update the package database
sudo apt update
# Install Redis
sudo apt install redis-server
# Start Redis
sudo systemctl start redis
# Enable Redis to start on boot
sudo systemctl enable redis
# Check Redis status
sudo systemctl status redis
On RedHat
# Enable the EPEL repository
sudo yum install epel-release
# Install Redis
sudo yum install redis
# Start Redis
sudo systemctl start redis
# Enable Redis to start on boot
sudo systemctl enable redis
# Check Redis status
sudo systemctl status redis
Basic Configuration
Redis configuration file is typically located at /etc/redis/redis.conf
on Debian and /etc/redis.conf
on RedHat. Here are some basic configuration options:
# Set the IP address that Redis will listen on
bind 127.0.0.1
# Set the port that Redis will listen on
port 6379
# Set the maximum memory usage
maxmemory 256mb
# Set the policy for key eviction when memory limit is reached
maxmemory-policy allkeys-lru
Advanced Configuration
Persistence
Redis provides two mechanisms for persistence: RDB snapshots and AOF (Append Only File). Both can be enabled for redundancy.
RDB Snapshots
# Save the DB if at least 1 key changed in the last 900 seconds (15 minutes)
save 900 1
# Save the DB if at least 10 keys changed in the last 300 seconds (5 minutes)
save 300 10
# Save the DB if at least 10000 keys changed in the last 60 seconds (1 minute)
save 60 10000
# Location of the snapshot file
dbfilename dump.rdb
# Directory where to save the snapshot file
dir /var/lib/redis
AOF (Append Only File)
# Enable AOF
appendonly yes
# AOF file name
appendfilename "appendonly.aof"
# AOF fsync policy (always, everysec, no)
appendfsync everysec
Security
Securing Redis involves configuring a password, setting proper access permissions, and enabling encryption if needed.
# Set a password for Redis
requirepass yourpassword
# Only allow connections from certain IP addresses
bind 127.0.0.1 ::1
# Disable dangerous commands (example: FLUSHALL)
rename-command FLUSHALL ""
Performance Tuning
Optimize Redis performance by configuring various parameters.
# Set the maximum number of clients
maxclients 10000
# Set the timeout for idle clients
timeout 300
# Configure the number of databases
databases 16
Monitoring and Maintenance
Regular monitoring and maintenance of Redis are essential for ensuring its performance and reliability.
Monitoring
# Monitor Redis server
redis-cli monitor
# Check Redis info and statistics
redis-cli info
# View connected clients
redis-cli client list
Backup
# Create a backup of the current database
cp /var/lib/redis/dump.rdb /backup/dump.rdb.bak
# Restore from a backup
cp /backup/dump.rdb.bak /var/lib/redis/dump.rdb
Conclusion
By following this guide, you should have a well-configured Redis instance on either Debian or RedHat. Proper configuration and regular maintenance are crucial for leveraging the full potential of Redis.