How to Set Up a Redis Cluster on a VPS for High Availability

How to Set Up a Redis Cluster on a VPS for High Availability

A single Redis instance is fast and simple — but it’s also a single point of failure. For production applications where the cache or session store going down causes immediate user impact, Redis offers two high-availability architectures: Redis Sentinel (master-replica with automatic failover) and Redis Cluster (sharded data across multiple nodes). This guide covers both approaches and when to use each.

Single Redis vs Sentinel vs Cluster

Single Instance Sentinel Cluster
High availability ✅ (failover) ✅ (failover + sharding)
Data sharding
Minimum nodes 1 3 (1 master + 2 replicas) 6 (3 masters + 3 replicas)
Complexity Low Medium High
Best for Dev, small apps Medium apps, session store Large apps, big datasets

💡 For most VPS users: Redis Sentinel on a single VPS (master + replicas on same host) gives good reliability without the complexity of a full cluster. True Cluster requires 3+ separate servers. View VPS Plans →


Part 1: Redis Sentinel Setup (Single VPS)

Sentinel monitors a master Redis instance and automatically promotes a replica if the master fails.

Step 1: Install Redis

sudo apt update
sudo apt install redis-server -y

Step 2: Configure Master

sudo nano /etc/redis/redis.conf
# Master configuration
bind 127.0.0.1
port 6379
requirepass YourRedisPassword!
masterauth YourRedisPassword!

# Enable persistence
appendonly yes
appendfilename "appendonly.aof"
save 900 1
save 300 10
save 60 10000

maxmemory 512mb
maxmemory-policy allkeys-lru

Step 3: Configure Replica

sudo cp /etc/redis/redis.conf /etc/redis/redis-replica.conf
sudo nano /etc/redis/redis-replica.conf
# Replica configuration (different port)
port 6380
replicaof 127.0.0.1 6379
requirepass YourRedisPassword!
masterauth YourRedisPassword!
appendfilename "appendonly-replica.aof"
logfile /var/log/redis/redis-replica.log
dir /var/lib/redis-replica
sudo mkdir -p /var/lib/redis-replica
sudo chown redis:redis /var/lib/redis-replica

Step 4: Start Replica

sudo nano /etc/systemd/system/redis-replica.service
[Unit]
Description=Redis Replica
After=network.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis/redis-replica.conf
User=redis
Group=redis
Restart=always

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable redis-replica
sudo systemctl start redis-replica

# Verify replication is working
redis-cli -p 6380 -a YourRedisPassword! INFO replication | grep role
# Should show: role:slave

Step 5: Configure Sentinel

sudo nano /etc/redis/sentinel.conf
port 26379
daemonize yes
logfile /var/log/redis/sentinel.log
dir /tmp

# Monitor master (quorum=1 means 1 sentinel must agree for failover)
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel auth-pass mymaster YourRedisPassword!
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
sudo nano /etc/systemd/system/redis-sentinel.service
[Unit]
Description=Redis Sentinel
After=redis.service

[Service]
ExecStart=/usr/bin/redis-sentinel /etc/redis/sentinel.conf
User=redis
Restart=always

[Install]
WantedBy=multi-user.target
sudo systemctl enable redis-sentinel
sudo systemctl start redis-sentinel

# Check Sentinel status
redis-cli -p 26379 sentinel masters

Part 2: Connecting Your Application to Sentinel

Applications connect to Sentinel to discover the current master — Sentinel handles all failover automatically:

Node.js with ioredis

npm install ioredis
const Redis = require('ioredis');

const redis = new Redis({
  sentinels: [
    { host: '127.0.0.1', port: 26379 }
  ],
  name: 'mymaster',          // Must match sentinel monitor name
  password: 'YourRedisPassword!',
  sentinelPassword: 'YourRedisPassword!',
  retryStrategy: (times) => Math.min(times * 50, 2000)
});

redis.on('ready', () => console.log('Redis Sentinel connected'));
redis.on('error', (err) => console.error('Redis error:', err));

// Use exactly like regular Redis
await redis.set('key', 'value');
const val = await redis.get('key');

Python with redis-py

pip install redis
from redis.sentinel import Sentinel

sentinel = Sentinel(
    [('127.0.0.1', 26379)],
    socket_timeout=0.1,
    password='YourRedisPassword!'
)

# Get master connection (for writes)
master = sentinel.master_for('mymaster', socket_timeout=0.1)

# Get replica connection (for reads)
slave = sentinel.slave_for('mymaster', socket_timeout=0.1)

master.set('key', 'value')
val = slave.get('key')

Part 3: True Redis Cluster (Multi-Node)

Redis Cluster requires minimum 3 master nodes. Best run across 3 VPS instances for true high availability:

Step 1: Configure Redis on each node

# Run on each of the 3 VPS instances
sudo nano /etc/redis/redis.conf
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
requirepass YourRedisPassword!
masterauth YourRedisPassword!
sudo systemctl restart redis-server

Step 2: Create the cluster

# Run from any one node — replace IPs with your 3 VPS IPs
# --cluster-replicas 1 creates 1 replica per master (6 total nodes)
redis-cli --cluster create \
  VPS1_IP:6379 \
  VPS2_IP:6379 \
  VPS3_IP:6379 \
  --cluster-replicas 0 \
  -a YourRedisPassword!
# Verify cluster status
redis-cli -h VPS1_IP -a YourRedisPassword! cluster info
redis-cli -h VPS1_IP -a YourRedisPassword! cluster nodes

Step 3: Connect application to cluster

// Node.js cluster connection
const Redis = require('ioredis');

const redis = new Redis.Cluster(
  [
    { host: 'VPS1_IP', port: 6379 },
    { host: 'VPS2_IP', port: 6379 },
    { host: 'VPS3_IP', port: 6379 },
  ],
  {
    redisOptions: {
      password: 'YourRedisPassword!'
    }
  }
);

Monitoring Redis Health

# Real-time Redis stats
redis-cli -a YourRedisPassword! --stat

# Memory usage
redis-cli -a YourRedisPassword! INFO memory | grep used_memory_human

# Keyspace hits/misses (cache effectiveness)
redis-cli -a YourRedisPassword! INFO stats | grep -E "keyspace|hits|misses"

# Slow log (queries over 10ms)
redis-cli -a YourRedisPassword! SLOWLOG GET 10

# Monitor all commands in real time (use carefully on production)
redis-cli -a YourRedisPassword! MONITOR

Redis Backup

nano ~/backup-redis.sh
#!/bin/bash
DATE=$(date +%Y-%m-%d_%H-%M)
BACKUP_DIR="/var/backups/redis"
mkdir -p $BACKUP_DIR

# Save RDB snapshot
redis-cli -a YourRedisPassword! BGSAVE
sleep 5  # Wait for save to complete

# Copy the dump file
cp /var/lib/redis/dump.rdb $BACKUP_DIR/dump-$DATE.rdb

# Compress
gzip $BACKUP_DIR/dump-$DATE.rdb

# Remove backups older than 7 days
find $BACKUP_DIR -name "*.gz" -mtime +7 -delete
echo "Redis backup complete: $DATE"

Final Thoughts

Redis Sentinel gives you automatic failover and master promotion for most production VPS workloads without the complexity of a full cluster. For applications where cache availability is critical — e-commerce session stores, real-time leaderboards, high-traffic API caches — Sentinel on a well-configured VPS is the right solution. Full Redis Cluster makes sense when your dataset exceeds a single node’s RAM or when you need horizontal write throughput.

Fast • Reliable • Affordable VPS - DO It Now!

Get top VPS hosting with VPS.DO’s fast, low-cost plans. Try risk-free with our 7-day no-questions-asked refund and start today!