Master Redis Single‑Node Deployment: From Source Build to Docker and Production Best Practices
This comprehensive guide walks you through installing Redis on a single server using source compilation and Docker, configuring persistence, memory management, backups, disaster recovery, and systemd service setup, ensuring a production‑ready Redis instance that can handle over 10,000 QPS.
Redis Series Overview
Redis is often deployed as a single‑node service in production for simplicity, but proper configuration is essential to avoid issues such as disk exhaustion, memory spikes, and data loss.
1. Source Installation Process
1.1 Download, compile, and install
# Install build tools
sudo yum groupinstall "Development Tools" -y
sudo yum install jemalloc-devel -y
cd /usr/local/src
wget http://download.redis.io/redis-stable.tar.gz
tar -xzvf redis-stable.tar.gz -C /home/user/
cd /home/user/redis-stable
make
sudo make install
sudo cp redis.conf /etc/redis.conf
sudo vim /etc/redis.conf1.2 Modify configuration file
# Enable protected mode and bind to localhost
bind 127.0.0.1 -::1
protected-mode yes
port 6379
logfile /home/uredis/redis/redis-server.log
requirepass yourpassword
save 900 1
save 300 10
save 60 10000
dbfilename dump.rdb
appendonly yes
appendfilename appendonly.aof
maxmemory 2gb
maxmemory-policy allkeys-lru
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command CONFIG ""
rename-command SHUTDOWN ""
rename-command SAVE ""1.3 Create a non‑root user
# Add user and set permissions
sudo useradd -m uredis
sudo chown -R uredis:uredis /home/uredis/redis
sudo chmod -R 770 /home/uredis/redis1.4 Set up systemd service
# /etc/systemd/system/redis.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
User=uredis
Group=uredis
[Install]
WantedBy=multi-user.target sudo systemctl daemon-reload
sudo systemctl enable redis
sudo systemctl start redis
sudo systemctl status redis1.5 Client connection test
redis-cli -p 6379
set mykey "Hello, Redis!"
get mykey
ping2. Docker Image Installation
Pull the official Redis image and customize the configuration before running the container.
# Pull image
sudo docker pull redis:7
# Run container with custom config and data volume
sudo docker run --name redis-instance -d \
-p 6379:6379 \
-v /home/zzs/redis-data:/data \
-v /home/zzs/redis.conf:/usr/local/etc/redis/redis.conf \
redis:7 redis-server /usr/local/etc/redis/redis.confAfter startup, logs and persistence files are generated.
3. Default Configuration Reference
The following excerpt from redis.conf can be used as a baseline for production deployments.
bind 127.0.0.1 -::1
protected-mode yes
port 6379
timeout 0
tcp-keepalive 300
loglevel notice
logfile ""
Databases 16
always-show-logo no
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
replica-priority 100
maxmemory 2gb
maxmemory-policy allkeys-lru
appendonly no
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
appendfsync everysec
save 900 1
save 300 10
save 60 100004. Memory and Disk Management
4.1 RDB and AOF Overview
RDB creates point‑in‑time snapshots using a forked child process, while AOF logs every write operation to enable precise recovery. Both mechanisms should be enabled in production to minimize data loss.
# RDB snapshot configuration
save 900 1
save 300 10
save 60 10000
# AOF configuration
appendonly yes
appendfilename appendonly.aof
appendfsync everysec
# AOF rewrite trigger
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb4.2 Scheduled Backups
# Daily RDB backup at 02:00
0 2 * * * cp /data/redis/dump.rdb /backup/redis/dump-$(date +\%F).rdb
# Delete backups older than 7 days
0 3 * * * find /backup/redis/ -type f -name "*.rdb" -mtime +7 -exec rm -f {} \;4.3 Disaster Recovery Script
#!/bin/bash
# Verify RDB file
redis-server --test-memory 1024 --dbfilename /backup/redis/dump-$(date +\%F).rdb > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "RDB backup is valid"
else
echo "RDB backup is invalid" | mail -s "Redis RDB Backup Error" [email protected]
fiIn case of failure, restore the latest dump and restart Redis:
# Stop Redis
systemctl stop redis
# Restore backup
cp /backup/redis/dump-2023-10-10.rdb /var/lib/redis/dump.rdb
# Start Redis
systemctl start redis4.4 Memory Allocation and Eviction Policies
# Example memory settings
maxmemory 4gb
maxmemory-policy allkeys-lru # LRU eviction for cache workloads
# Alternative policy
# maxmemory-policy noeviction # Reject writes when memory is fullA properly tuned single‑node Redis instance can sustain over 10,000 QPS for typical cache workloads.
Lin is Dream
Sharing Java developer knowledge, practical articles, and continuous insights into computer engineering.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
