Knowledge Overview

Prerequisites

  • Required Knowledge Prerequisites
  • Linux system administration experience with package management, systemd services, and file permissions
  • Basic networking concepts including TCP/IP, ports, firewalls, and SSH connectivity between servers
  • Command line proficiency for executing terminal commands, editing configuration files, and process management
  • Redis fundamentals understanding of key-value operations, data types, and basic Redis commands
  • System monitoring basics familiarity with log files, process monitoring, and performance metrics analysis

What You'll Learn

  • What You'll Learn from This Post
  • Master Redis cluster architecture fundamentals including hash slots, sharding, and distributed data storage concepts
  • Configure production-ready 6-node clusters with proper master-replica relationships and failover mechanisms
  • Implement automatic sharding across 16,384 hash slots for optimal data distribution and performance
  • Set up robust failover systems that promote replicas to masters within seconds during node failures
  • Monitor cluster health effectively using built-in Redis commands and external monitoring tools like Prometheus
  • Scale clusters horizontally by adding/removing nodes while maintaining data availability and slot distribution
  • Troubleshoot common issues including network connectivity, slot migration errors, and performance bottlenecks
  • Backup and restore cluster data with coordinated procedures across distributed nodes
  • Optimize memory usage and performance through proper configuration and load balancing strategies
  • Secure cluster communications with firewall rules, authentication, and network isolation techniques

Tools Required

  • Required Tools and Software
  • Redis Server 3.0+ installed on all cluster nodes (preferably Redis 6.0+ for latest features)
  • Linux servers (minimum 6 nodes recommended) running Ubuntu 18.04+, CentOS 7+, or RHEL 7+
  • Network connectivity between all nodes with ports 7000 and 17000 accessible for cluster communication
  • SSH access to all cluster nodes for remote configuration and management tasks
  • Text editor (nano, vim, or emacs) for modifying Redis configuration files and scripts

Time Investment

23 minutes reading time
46-69 minutes hands-on practice

Guide Content

Quick Redis Cluster Setup

Redis cluster setup enables horizontal scaling and high availability for in-memory data storage. Create a minimum 6-node cluster (3 masters, 3 replicas) using redis-cli --cluster create command with automatic slot distribution and failover capabilities.

Quick Setup Commands:

Bash
# Install Redis on all nodes
sudo apt update && sudo apt install redis-server -y

# Create cluster with 6 nodes
redis-cli --cluster create 192.168.1.10:7000 192.168.1.11:7000 192.168.1.12:7000 \
192.168.1.13:7000 192.168.1.14:7000 192.168.1.15:7000 \
--cluster-replicas 1

Table of Contents

  1. What is Redis Cluster and Why Use It?
  2. How to Plan Redis Cluster Architecture
  3. How to Install Redis for Clustering
  4. How to Configure Redis Cluster Nodes
  5. How to Create and Initialize Redis Cluster
  6. How to Manage Redis Cluster Sharding
  7. How to Configure Redis Cluster Failover
  8. How to Monitor Redis Cluster Health
  9. How to Scale Redis Cluster Nodes
  10. How to Backup Redis Cluster Data
  11. Frequently Asked Questions
  12. Troubleshooting Redis Cluster Issues

What is Redis Cluster and Why Use It?

Redis cluster setup provides automatic data sharding across multiple Redis instances, ensuring horizontal scalability and fault tolerance. Unlike single Redis instances, clusters distribute data using hash slots and maintain replicas for high availability.

Redis Cluster Key Features

Automatic Sharding: Redis clusters automatically partition data across 16,384 hash slots distributed among master nodes. This approach ensures even data distribution and eliminates hotspots commonly found in traditional caching solutions.

Built-in Failover: When master nodes fail, replica nodes automatically promote themselves to maintain service availability. The cluster continues operating with remaining nodes, providing seamless failover without external coordination tools.

Linear Scalability: Adding nodes to Redis cluster setup increases both storage capacity and throughput proportionally. Each new master node handles approximately 1/N of total hash slots, where N represents the number of master nodes.

Redis Cluster vs Single Instance Comparison

FeatureSingle RedisRedis Cluster
Maximum MemoryLimited to single server RAMDistributed across multiple nodes
ThroughputSingle server CPU coresCombined CPU power of all nodes
AvailabilitySingle point of failureAutomatic failover with replicas
ScalingVertical scaling onlyHorizontal scaling support
Data PersistenceSingle backup pointDistributed backups across nodes

How to Plan Redis Cluster Architecture

Effective Redis cluster architecture planning determines long-term performance and reliability. Consider data distribution patterns, network topology, and hardware specifications when designing your cluster layout.

Minimum Redis Cluster Requirements

Node Count: Redis clusters require minimum 3 master nodes for quorum-based decisions. However, production environments typically deploy 6 nodes (3 masters + 3 replicas) for proper redundancy and load distribution.

Bash
# Check current system resources
free -h
cat /proc/cpuinfo | grep processor | wc -l
df -h /var/lib/redis

# Verify network connectivity between cluster nodes
ping -c 3 192.168.1.10
telnet 192.168.1.11 7000

Redis Cluster Topology Design

Master Node Distribution: Distribute master nodes across different physical servers or availability zones to prevent correlated failures. Each master should handle roughly equal hash slot ranges for balanced load distribution.

Replica Placement: Position replica nodes on different hardware from their corresponding masters. This strategy ensures data availability during hardware failures and provides read scaling opportunities for applications.

Bash
# Example topology for 6-node cluster
# Master Nodes:
# redis-master-01: 192.168.1.10:7000 (slots 0-5461)
# redis-master-02: 192.168.1.11:7000 (slots 5462-10922)
# redis-master-03: 192.168.1.12:7000 (slots 10923-16383)

# Replica Nodes:
# redis-replica-01: 192.168.1.13:7000 (replica of master-01)
# redis-replica-02: 192.168.1.14:7000 (replica of master-02)
# redis-replica-03: 192.168.1.15:7000 (replica of master-03)

How to Install Redis for Clustering

Redis cluster installation requires specific configuration modifications compared to standard Redis deployments. Therefore, prepare all nodes with appropriate Redis versions and cluster-enabled settings before initialization.

Installing Redis on Ubuntu/Debian Systems

System Preparation: Update package repositories and install Redis server with development tools for compilation support when needed.

Bash
# Update system packages
sudo apt update && sudo apt upgrade -y

# Install Redis server and tools
sudo apt install redis-server redis-tools build-essential -y

# Verify Redis installation
redis-server --version
redis-cli --version

# Check Redis service status
sudo systemctl status redis-server
sudo systemctl enable redis-server

Installing Redis on CentOS/RHEL Systems

Repository Configuration: Enable EPEL repository for Redis packages or compile from source for latest versions with cluster features.

Bash
# Enable EPEL repository
sudo yum install epel-release -y

# Install Redis server
sudo yum install redis -y

# Alternative: Install from source for latest version
wget https://download.redis.io/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable
make
sudo make install

# Configure systemd service
sudo systemctl enable redis
sudo systemctl start redis

Redis Security Configuration

Network Security: Configure firewall rules to allow Redis cluster communication while restricting unauthorized access to cluster ports.

Bash
# Configure firewall for Redis cluster ports
sudo ufw allow from 192.168.1.0/24 to any port 7000
sudo ufw allow from 192.168.1.0/24 to any port 17000

# For CentOS/RHEL systems
sudo firewall-cmd --permanent --add-port=7000/tcp
sudo firewall-cmd --permanent --add-port=17000/tcp
sudo firewall-cmd --reload

# Verify port connectivity
ss -tulpn | grep 7000
netstat -tulpn | grep :7000

How to Configure Redis Cluster Nodes

Redis cluster configuration requires specific parameters in redis.conf files to enable cluster functionality. Additionally, each node needs unique configuration addressing networking, persistence, and cluster-specific settings.

Base Redis Cluster Configuration

Essential Cluster Settings: Create redis.conf with cluster-enabled parameters for each node in your Redis cluster setup.

Bash
# Create Redis cluster configuration directory
sudo mkdir -p /etc/redis/cluster
sudo mkdir -p /var/lib/redis/cluster
sudo mkdir -p /var/log/redis

# Create configuration file for first node
sudo tee /etc/redis/cluster/redis-7000.conf > /dev/null <<EOF
# Basic Redis settings
port 7000
bind 0.0.0.0
protected-mode no
tcp-backlog 511
timeout 0
tcp-keepalive 300

# Cluster configuration
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.1.10
cluster-announce-port 7000
cluster-announce-bus-port 17000

# Persistence settings
appendonly yes
appendfilename "appendonly-7000.aof"
appendfsync everysec
dir /var/lib/redis/cluster

# Logging configuration
loglevel notice
logfile /var/log/redis/redis-7000.log
syslog-enabled yes
syslog-ident redis-7000

# Memory management
maxmemory-policy allkeys-lru
maxmemory 2gb
EOF

Node-Specific Configuration Files

Individual Node Setup: Create separate configuration files for each Redis cluster node with appropriate IP addresses and port numbers.

Bash
# Create configuration files for all 6 nodes
for i in {10..15}; do
    port=$((7000))
    ip="192.168.1.$i"
    
    sudo tee /etc/redis/cluster/redis-$ip-$port.conf > /dev/null <<EOF
port $port
bind 0.0.0.0
protected-mode no
cluster-enabled yes
cluster-config-file nodes-$ip-$port.conf
cluster-node-timeout 15000
cluster-announce-ip $ip
cluster-announce-port $port
cluster-announce-bus-port $((port + 10000))
appendonly yes
appendfilename "appendonly-$ip-$port.aof"
dir /var/lib/redis/cluster
logfile /var/log/redis/redis-$ip-$port.log
maxmemory 2gb
maxmemory-policy allkeys-lru
EOF
done

Redis Cluster Systemd Services

Service Management: Create systemd service units for each Redis cluster node to enable automatic startup and process management.

Bash
# Create systemd service template
sudo tee /etc/systemd/system/redis-cluster@.service > /dev/null <<EOF
[Unit]
Description=Redis Cluster Node %i
After=network.target

[Service]
Type=notify
ExecStart=/usr/bin/redis-server /etc/redis/cluster/redis-%i.conf
ExecReload=/bin/kill -USR2 \$MAINPID
TimeoutStopSec=0
Restart=always
User=redis
Group=redis
RuntimeDirectory=redis-cluster
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF

# Enable and start Redis cluster services
for i in {10..15}; do
    ip="192.168.1.$i"
    port="7000"
    sudo systemctl enable redis-cluster@$ip-$port
    sudo systemctl start redis-cluster@$ip-$port
    sudo systemctl status redis-cluster@$ip-$port
done

How to Create and Initialize Redis Cluster

Redis cluster creation involves connecting individual Redis instances into a coordinated cluster with automatic slot distribution and replica assignment. Moreover, this process establishes the foundation for distributed operations and failover capabilities.

Redis Cluster Initialization Command

Cluster Creation: Use redis-cli cluster creation tool to establish your Redis cluster setup with automatic slot assignment and replica configuration.

Bash
# Verify all nodes are running
for ip in 192.168.1.{10..15}; do
    redis-cli -h $ip -p 7000 ping
done

# Create Redis cluster with automatic slot distribution
redis-cli --cluster create \
    192.168.1.10:7000 192.168.1.11:7000 192.168.1.12:7000 \
    192.168.1.13:7000 192.168.1.14:7000 192.168.1.15:7000 \
    --cluster-replicas 1

# Verify cluster status
redis-cli -h 192.168.1.10 -p 7000 cluster info
redis-cli -h 192.168.1.10 -p 7000 cluster nodes

Manual Cluster Configuration Alternative

Advanced Setup: Configure Redis cluster manually for custom slot distribution or specific master-replica relationships.

Bash
# Connect nodes manually (alternative approach)
# First, connect all nodes to form cluster mesh
for master in 192.168.1.{10..12}; do
    for node in 192.168.1.{10..15}; do
        if [ "$master" != "$node" ]; then
            redis-cli -h $master -p 7000 CLUSTER MEET $node 7000
        fi
    done
done

# Assign hash slots to masters
redis-cli -h 192.168.1.10 -p 7000 CLUSTER ADDSLOTS {0..5461}
redis-cli -h 192.168.1.11 -p 7000 CLUSTER ADDSLOTS {5462..10922}
redis-cli -h 192.168.1.12 -p 7000 CLUSTER ADDSLOTS {10923..16383}

# Configure replicas
redis-cli -h 192.168.1.13 -p 7000 CLUSTER REPLICATE $(redis-cli -h 192.168.1.10 -p 7000 CLUSTER MYID)
redis-cli -h 192.168.1.14 -p 7000 CLUSTER REPLICATE $(redis-cli -h 192.168.1.11 -p 7000 CLUSTER MYID)
redis-cli -h 192.168.1.15 -p 7000 CLUSTER REPLICATE $(redis-cli -h 192.168.1.12 -p 7000 CLUSTER MYID)

Cluster Validation and Testing

Operational Verification: Test Redis cluster functionality with data operations and failover scenarios to ensure proper setup.

Bash
# Test cluster operations
redis-cli -h 192.168.1.10 -p 7000 -c set test_key "Hello Redis Cluster"
redis-cli -h 192.168.1.11 -p 7000 -c get test_key
redis-cli -h 192.168.1.12 -p 7000 -c set another_key "Distributed Data"

# Check key distribution across slots
redis-cli -h 192.168.1.10 -p 7000 CLUSTER KEYSLOT test_key
redis-cli -h 192.168.1.10 -p 7000 CLUSTER KEYSLOT another_key

# Monitor cluster statistics
redis-cli -h 192.168.1.10 -p 7000 INFO cluster
redis-cli -h 192.168.1.10 -p 7000 CLUSTER COUNTKEYSINSLOT 1000

How to Manage Redis Cluster Sharding

Redis cluster sharding automatically distributes data across nodes using consistent hashing algorithms. Understanding slot allocation and key distribution patterns helps optimize performance and plan capacity requirements for growing datasets.

Redis Hash Slot Distribution

Slot Algorithm: Redis clusters divide the keyspace into 16,384 hash slots using CRC16 checksum calculations. Each key maps to a specific slot, and consequently, a designated master node handles that slot range.

Bash
# Calculate hash slot for specific keys
redis-cli -h 192.168.1.10 -p 7000 CLUSTER KEYSLOT user:12345
redis-cli -h 192.168.1.10 -p 7000 CLUSTER KEYSLOT session:abcdef
redis-cli -h 192.168.1.10 -p 7000 CLUSTER KEYSLOT cache:product:678

# View slot allocation per node
redis-cli -h 192.168.1.10 -p 7000 CLUSTER NODES | grep master
redis-cli -h 192.168.1.10 -p 7000 CLUSTER SLOTS

# Count keys in specific slot ranges
for slot in {0..16383..1000}; do
    count=$(redis-cli -h 192.168.1.10 -p 7000 CLUSTER COUNTKEYSINSLOT $slot)
    echo "Slot $slot: $count keys"
done

Hash Tag Implementation

Key Grouping: Use hash tags to ensure related keys reside on the same Redis cluster node, enabling multi-key operations and atomic transactions.

Bash
# Hash tag examples - keys with same tag go to same slot
redis-cli -h 192.168.1.10 -p 7000 -c set user:{12345}:profile "John Doe"
redis-cli -h 192.168.1.10 -p 7000 -c set user:{12345}:settings "theme:dark"
redis-cli -h 192.168.1.10 -p 7000 -c set user:{12345}:permissions "admin"

# Verify same slot assignment
redis-cli -h 192.168.1.10 -p 7000 CLUSTER KEYSLOT user:{12345}:profile
redis-cli -h 192.168.1.10 -p 7000 CLUSTER KEYSLOT user:{12345}:settings
redis-cli -h 192.168.1.10 -p 7000 CLUSTER KEYSLOT user:{12345}:permissions

# Multi-key operations work with hash tags
redis-cli -h 192.168.1.10 -p 7000 -c MGET user:{12345}:profile user:{12345}:settings

Slot Migration and Rebalancing

Dynamic Rebalancing: Migrate hash slots between nodes to balance load distribution or accommodate cluster topology changes.

Bash
# Check current slot distribution
redis-cli -h 192.168.1.10 -p 7000 CLUSTER NODES | awk '/master/ {print $1, $9}'

# Migrate specific slot from one node to another
source_node="192.168.1.10:7000"
target_node="192.168.1.11:7000"
slot_number=1000

# Get node IDs
source_id=$(redis-cli -h 192.168.1.10 -p 7000 CLUSTER MYID)
target_id=$(redis-cli -h 192.168.1.11 -p 7000 CLUSTER MYID)

# Perform slot migration
redis-cli -h 192.168.1.11 -p 7000 CLUSTER SETSLOT $slot_number IMPORTING $source_id
redis-cli -h 192.168.1.10 -p 7000 CLUSTER SETSLOT $slot_number MIGRATING $target_id

# Migrate keys in the slot
redis-cli -h 192.168.1.10 -p 7000 CLUSTER GETKEYSINSLOT $slot_number 100 | \
while read key; do
    redis-cli -h 192.168.1.10 -p 7000 MIGRATE 192.168.1.11 7000 "$key" 0 5000
done

# Complete migration
redis-cli -h 192.168.1.10 -p 7000 CLUSTER SETSLOT $slot_number NODE $target_id
redis-cli -h 192.168.1.11 -p 7000 CLUSTER SETSLOT $slot_number NODE $target_id

How to Configure Redis Cluster Failover

Redis cluster failover mechanisms automatically promote replica nodes to master status when failures occur. Furthermore, proper failover configuration ensures minimal downtime and data consistency during node failures or maintenance operations.

Automatic Failover Configuration

Failover Settings: Configure cluster node timeout and failover parameters to balance between false positives and quick recovery times.

Bash
# Configure failover timeouts in redis.conf
echo "cluster-node-timeout 15000" >> /etc/redis/cluster/redis-7000.conf
echo "cluster-slave-validity-factor 10" >> /etc/redis/cluster/redis-7000.conf
echo "cluster-migration-barrier 1" >> /etc/redis/cluster/redis-7000.conf
echo "cluster-require-full-coverage no" >> /etc/redis/cluster/redis-7000.conf

# Restart Redis services to apply configuration
sudo systemctl restart redis-cluster@192.168.1.10-7000

# Monitor failover events
redis-cli -h 192.168.1.10 -p 7000 --latency-history -i 1
redis-cli -h 192.168.1.10 -p 7000 MONITOR | grep -i failover

Manual Failover Operations

Controlled Failover: Initiate manual failover for maintenance operations or testing failover procedures without waiting for node failures.

Bash
# Check current master-replica relationships
redis-cli -h 192.168.1.10 -p 7000 CLUSTER NODES | grep -E "(master|slave)"

# Initiate manual failover from replica to master
redis-cli -h 192.168.1.13 -p 7000 CLUSTER FAILOVER

# Force failover (use carefully - potential data loss)
redis-cli -h 192.168.1.13 -p 7000 CLUSTER FAILOVER FORCE

# Takeover (immediate promotion without consensus)
redis-cli -h 192.168.1.13 -p 7000 CLUSTER FAILOVER TAKEOVER

# Verify new cluster topology
redis-cli -h 192.168.1.10 -p 7000 CLUSTER NODES

Failover Monitoring and Alerting

Health Monitoring: Implement monitoring scripts to detect failover events and alert administrators about cluster state changes.

Bash
#!/bin/bash
# Create failover monitoring script
cat > /usr/local/bin/redis-cluster-monitor.sh << 'EOF'
#!/bin/bash
CLUSTER_NODES="192.168.1.10 192.168.1.11 192.168.1.12 192.168.1.13 192.168.1.14 192.168.1.15"
ALERT_EMAIL="admin@company.com"
LOG_FILE="/var/log/redis-cluster-monitor.log"

check_cluster_health() {
    local healthy_masters=0
    local total_masters=0
    
    for node in $CLUSTER_NODES; do
        if redis-cli -h $node -p 7000 ping &>/dev/null; then
            node_info=$(redis-cli -h $node -p 7000 CLUSTER NODES 2>/dev/null)
            if echo "$node_info" | grep -q "myself,master"; then
                total_masters=$((total_masters + 1))
                if echo "$node_info" | grep -q "connected"; then
                    healthy_masters=$((healthy_masters + 1))
                fi
            fi
        fi
    done
    
    echo "$(date): Masters healthy: $healthy_masters/$total_masters" >> $LOG_FILE
    
    if [ $healthy_masters -lt 2 ]; then
        echo "CRITICAL: Redis cluster has insufficient masters" | \
        mail -s "Redis Cluster Alert" $ALERT_EMAIL
    fi
}

check_cluster_health
EOF

chmod +x /usr/local/bin/redis-cluster-monitor.sh

# Schedule monitoring script
echo "*/5 * * * * /usr/local/bin/redis-cluster-monitor.sh" | crontab -

How to Monitor Redis Cluster Health

Redis cluster monitoring requires tracking node status, slot distribution, memory usage, and performance metrics across all cluster members. Comprehensive monitoring ensures early detection of issues and optimal cluster performance.

Basic Cluster Health Commands

Essential Monitoring: Use built-in Redis commands to assess cluster state and identify potential issues before they impact applications.

Bash
# Check overall cluster health
redis-cli -h 192.168.1.10 -p 7000 CLUSTER INFO
redis-cli -h 192.168.1.10 -p 7000 INFO replication
redis-cli -h 192.168.1.10 -p 7000 INFO memory

# Monitor all cluster nodes status
redis-cli -h 192.168.1.10 -p 7000 CLUSTER NODES | \
awk '{print $2, $3, $8}' | column -t

# Check slot coverage and assignment
redis-cli -h 192.168.1.10 -p 7000 CLUSTER CHECK
redis-cli -h 192.168.1.10 -p 7000 CLUSTER SLOTS | head -20

# Monitor key distribution statistics
for node in 192.168.1.{10..15}; do
    keys=$(redis-cli -h $node -p 7000 DBSIZE)
    memory=$(redis-cli -h $node -p 7000 INFO memory | grep used_memory_human | cut -d: -f2)
    echo "Node $node: $keys keys, Memory: $memory"
done

Performance Monitoring Metrics

Comprehensive Metrics: Track throughput, latency, and resource utilization across Redis cluster nodes for capacity planning and performance optimization.

Bash
# Create performance monitoring script
cat > /usr/local/bin/redis-cluster-stats.sh << 'EOF'
#!/bin/bash
NODES="192.168.1.10 192.168.1.11 192.168.1.12 192.168.1.13 192.168.1.14 192.168.1.15"

echo "=== Redis Cluster Performance Report ===" 
echo "Timestamp: $(date)"
echo

for node in $NODES; do
    echo "Node: $node"
    
    # Basic stats
    redis-cli -h $node -p 7000 INFO stats | grep -E "(total_commands_processed|total_connections_received|keyspace_hits|keyspace_misses)"
    
    # Memory usage
    redis-cli -h $node -p 7000 INFO memory | grep -E "(used_memory_human|used_memory_peak_human|mem_fragmentation_ratio)"
    
    # Network stats
    redis-cli -h $node -p 7000 INFO clients | grep -E "(connected_clients|blocked_clients)"
    
    # Replication lag (for replicas)
    redis-cli -h $node -p 7000 INFO replication | grep "master_repl_offset\|slave_repl_offset"
    
    echo "---"
done
EOF

chmod +x /usr/local/bin/redis-cluster-stats.sh

# Run performance monitoring
/usr/local/bin/redis-cluster-stats.sh

# Schedule regular monitoring
echo "*/10 * * * * /usr/local/bin/redis-cluster-stats.sh >> /var/log/redis-cluster-stats.log" | crontab -

Cluster Monitoring with External Tools

Advanced Monitoring: Integrate Redis cluster metrics with monitoring platforms like Prometheus, Grafana, or Zabbix for comprehensive observability.

Bash
# Install Redis Exporter for Prometheus
wget https://github.com/oliver006/redis_exporter/releases/latest/download/redis_exporter-v1.45.0.linux-amd64.tar.gz
tar -xzf redis_exporter-v1.45.0.linux-amd64.tar.gz
sudo cp redis_exporter-v1.45.0.linux-amd64/redis_exporter /usr/local/bin/

# Create systemd service for Redis exporter
sudo tee /etc/systemd/system/redis-exporter.service > /dev/null <<EOF
[Unit]
Description=Redis Exporter
After=network.target

[Service]
Type=simple
User=redis
ExecStart=/usr/local/bin/redis_exporter \
  -redis.addr=redis://192.168.1.10:7000 \
  -redis.addr=redis://192.168.1.11:7000 \
  -redis.addr=redis://192.168.1.12:7000 \
  -web.listen-address=:9121 \
  -log.level=info
Restart=always

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl enable redis-exporter
sudo systemctl start redis-exporter

# Verify metrics endpoint
curl http://localhost:9121/metrics | grep redis_

How to Scale Redis Cluster Nodes

Redis cluster scaling involves adding or removing nodes while maintaining data availability and proper slot distribution. Scaling operations require careful planning to ensure minimal performance impact during cluster topology changes.

Adding New Nodes to Cluster

Horizontal Scaling: Expand Redis cluster capacity by adding new master nodes or replicas to existing masters for improved throughput and redundancy.

Bash
# Prepare new node configuration
new_node_ip="192.168.1.16"
new_node_port="7000"

# Install and configure Redis on new node
sudo tee /etc/redis/cluster/redis-$new_node_ip-$new_node_port.conf > /dev/null <<EOF
port $new_node_port
bind 0.0.0.0
protected-mode no
cluster-enabled yes
cluster-config-file nodes-$new_node_ip-$new_node_port.conf
cluster-node-timeout 15000
cluster-announce-ip $new_node_ip
cluster-announce-port $new_node_port
cluster-announce-bus-port $((new_node_port + 10000))
appendonly yes
dir /var/lib/redis/cluster
logfile /var/log/redis/redis-$new_node_ip-$new_node_port.log
maxmemory 2gb
maxmemory-policy allkeys-lru
EOF

# Start new Redis instance
sudo systemctl start redis-cluster@$new_node_ip-$new_node_port

# Add new node to existing cluster
redis-cli --cluster add-node $new_node_ip:$new_node_port 192.168.1.10:7000

# Rebalance slots to include new master
redis-cli --cluster rebalance 192.168.1.10:7000 --cluster-use-empty-masters

# Verify cluster status after scaling
redis-cli -h 192.168.1.10 -p 7000 CLUSTER NODES
redis-cli -h 192.168.1.10 -p 7000 CLUSTER CHECK

Adding Replica Nodes

Replica Scaling: Increase read capacity and fault tolerance by adding replica nodes to existing masters in your Redis cluster setup.

Bash
# Add replica to existing master
new_replica_ip="192.168.1.17"
master_id=$(redis-cli -h 192.168.1.10 -p 7000 CLUSTER MYID)

# Configure and start new replica
sudo tee /etc/redis/cluster/redis-$new_replica_ip-7000.conf > /dev/null <<EOF
port 7000
bind 0.0.0.0
protected-mode no
cluster-enabled yes
cluster-config-file nodes-$new_replica_ip-7000.conf
cluster-node-timeout 15000
cluster-announce-ip $new_replica_ip
cluster-announce-port 7000
cluster-announce-bus-port 17000
appendonly yes
dir /var/lib/redis/cluster
logfile /var/log/redis/redis-$new_replica_ip-7000.log
EOF

sudo systemctl start redis-cluster@$new_replica_ip-7000

# Add replica node and assign to master
redis-cli --cluster add-node $new_replica_ip:7000 192.168.1.10:7000 --cluster-slave
redis-cli -h $new_replica_ip -p 7000 CLUSTER REPLICATE $master_id

# Verify replica assignment
redis-cli -h 192.168.1.10 -p 7000 CLUSTER NODES | grep $new_replica_ip

Removing Nodes from Cluster

Cluster Downsizing: Remove unnecessary nodes while ensuring proper slot redistribution and maintaining cluster quorum requirements.

Bash
# Remove replica node (safer operation)
replica_node="192.168.1.17:7000"
redis-cli --cluster del-node 192.168.1.10:7000 $(redis-cli -h 192.168.1.17 -p 7000 CLUSTER MYID)

# Remove master node (requires slot migration)
master_to_remove="192.168.1.16:7000"
target_master="192.168.1.10:7000"

# First, migrate all slots from the master to be removed
redis-cli --cluster reshard 192.168.1.10:7000 \
    --cluster-from $(redis-cli -h 192.168.1.16 -p 7000 CLUSTER MYID) \
    --cluster-to $(redis-cli -h 192.168.1.10 -p 7000 CLUSTER MYID) \
    --cluster-slots 1365 \
    --cluster-yes

# Then remove the empty master
redis-cli --cluster del-node 192.168.1.10:7000 $(redis-cli -h 192.168.1.16 -p 7000 CLUSTER MYID)

# Verify cluster integrity
redis-cli -h 192.168.1.10 -p 7000 CLUSTER CHECK

How to Backup Redis Cluster Data

Redis cluster backup strategies must account for distributed data across multiple nodes and ensure consistency across the entire cluster. Effective backup procedures include both individual node backups and cluster-wide coordination mechanisms.

Individual Node Backup Procedures

Node-Level Backups: Create consistent backups of each Redis cluster node using RDB snapshots and AOF persistence files.

Bash
#!/bin/bash
# Create Redis cluster backup script
cat > /usr/local/bin/redis-cluster-backup.sh << 'EOF'
#!/bin/bash
BACKUP_DIR="/backup/redis-cluster"
DATE=$(date +%Y%m%d_%H%M%S)
NODES="192.168.1.10 192.168.1.11 192.168.1.12 192.168.1.13 192.168.1.14 192.168.1.15"

# Create backup directory
mkdir -p $BACKUP_DIR/$DATE

for node in $NODES; do
    echo "Backing up Redis node: $node"
    node_dir="$BACKUP_DIR/$DATE/$node"
    mkdir -p $node_dir
    
    # Force RDB snapshot
    redis-cli -h $node -p 7000 BGSAVE
    
    # Wait for background save to complete
    while [ "$(redis-cli -h $node -p 7000 LASTSAVE)" = "$(redis-cli -h $node -p 7000 LASTSAVE)" ]; do
        sleep 1
    done
    
    # Copy RDB and AOF files
    ssh $node "sudo cp /var/lib/redis/cluster/dump.rdb $node_dir/ 2>/dev/null || true"
    ssh $node "sudo cp /var/lib/redis/cluster/appendonly-*.aof $node_dir/ 2>/dev/null || true"
    ssh $node "sudo cp /var/lib/redis/cluster/nodes-*.conf $node_dir/ 2>/dev/null || true"
    
    # Save cluster configuration
    redis-cli -h $node -p 7000 CLUSTER NODES > $node_dir/cluster-nodes.txt
    redis-cli -h $node -p 7000 INFO > $node_dir/node-info.txt
done

# Create cluster metadata backup
redis-cli -h 192.168.1.10 -p 7000 CLUSTER SLOTS > $BACKUP_DIR/$DATE/cluster-slots.txt
redis-cli -h 192.168.1.10 -p 7000 CLUSTER INFO > $BACKUP_DIR/$DATE/cluster-info.txt

# Compress backup
tar -czf $BACKUP_DIR/redis-cluster-backup-$DATE.tar.gz -C $BACKUP_DIR $DATE
rm -rf $BACKUP_DIR/$DATE

echo "Redis cluster backup completed: $BACKUP_DIR/redis-cluster-backup-$DATE.tar.gz"
EOF

chmod +x /usr/local/bin/redis-cluster-backup.sh

# Schedule regular backups
echo "0 2 * * * /usr/local/bin/redis-cluster-backup.sh" | crontab -

Cluster Restoration Procedures

Disaster Recovery: Restore Redis cluster from backups with proper slot assignment and replica configuration.

Bash
#!/bin/bash
# Redis cluster restore script
cat > /usr/local/bin/redis-cluster-restore.sh << 'EOF'
#!/bin/bash
BACKUP_FILE=$1
RESTORE_DIR="/tmp/redis-restore"

if [ -z "$BACKUP_FILE" ]; then
    echo "Usage: $0 <backup-file.tar.gz>"
    exit 1
fi

# Extract backup
mkdir -p $RESTORE_DIR
tar -xzf $BACKUP_FILE -C $RESTORE_DIR

# Stop all Redis cluster services
for node in 192.168.1.{10..15}; do
    ssh $node "sudo systemctl stop redis-cluster@$node-7000"
    ssh $node "sudo rm -f /var/lib/redis/cluster/*"
done

# Restore data files to each node
for node_backup in $RESTORE_DIR/*/192.168.1.*; do
    node_ip=$(basename $node_backup)
    echo "Restoring node: $node_ip"
    
    # Copy data files back
    ssh $node_ip "sudo mkdir -p /var/lib/redis/cluster"
    scp $node_backup/* $node_ip:/tmp/
    ssh $node_ip "sudo cp /tmp/dump.rdb /var/lib/redis/cluster/ 2>/dev/null || true"
    ssh $node_ip "sudo cp /tmp/appendonly-*.aof /var/lib/redis/cluster/ 2>/dev/null || true"
    ssh $node_ip "sudo chown -R redis:redis /var/lib/redis/cluster"
done

# Start Redis instances
for node in 192.168.1.{10..15}; do
    ssh $node "sudo systemctl start redis-cluster@$node-7000"
    sleep 2
done

# Rebuild cluster if needed
echo "Cluster restoration completed. Verify with:"
echo "redis-cli -h 192.168.1.10 -p 7000 CLUSTER NODES"
echo "redis-cli -h 192.168.1.10 -p 7000 CLUSTER CHECK"
EOF

chmod +x /usr/local/bin/redis-cluster-restore.sh

Continuous Backup Monitoring

Backup Validation: Implement automated backup verification and monitoring to ensure backup integrity and successful completion.

Bash
# Create backup monitoring script
cat > /usr/local/bin/redis-backup-monitor.sh << 'EOF'
#!/bin/bash
BACKUP_DIR="/backup/redis-cluster"
ALERT_EMAIL="admin@company.com"
MAX_BACKUP_AGE=86400  # 24 hours in seconds

# Check latest backup age
latest_backup=$(ls -t $BACKUP_DIR/redis-cluster-backup-*.tar.gz 2>/dev/null | head -1)

if [ -z "$latest_backup" ]; then
    echo "ERROR: No Redis cluster backups found" | \
    mail -s "Redis Backup Alert: No backups found" $ALERT_EMAIL
    exit 1
fi

backup_age=$(($(date +%s) - $(stat -c %Y "$latest_backup")))

if [ $backup_age -gt $MAX_BACKUP_AGE ]; then
    echo "WARNING: Latest Redis backup is $((backup_age/3600)) hours old" | \
    mail -s "Redis Backup Alert: Backup too old" $ALERT_EMAIL
fi

# Verify backup integrity
if ! tar -tzf "$latest_backup" >/dev/null 2>&1; then
    echo "ERROR: Latest Redis backup file is corrupted" | \
    mail -s "Redis Backup Alert: Corrupted backup" $ALERT_EMAIL
fi

echo "$(date): Backup validation completed for $latest_backup" >> /var/log/redis-backup-monitor.log
EOF

chmod +x /usr/local/bin/redis-backup-monitor.sh

# Schedule backup monitoring
echo "0 */6 * * * /usr/local/bin/redis-backup-monitor.sh" | crontab -

Frequently Asked Questions

What is the minimum number of nodes required for Redis cluster setup?

Redis clusters require a minimum of 3 master nodes to form a quorum for cluster decisions. However, production deployments typically use 6 nodes (3 masters + 3 replicas) to ensure proper redundancy and fault tolerance. Each master should have at least one replica to prevent data loss during node failures.

How does Redis cluster handle data distribution across nodes?

Redis cluster uses consistent hashing with 16,384 hash slots distributed across master nodes. Each key maps to a specific slot using CRC16 checksum calculation. Keys with the same hash tag (content between curly braces) always reside on the same node, enabling multi-key operations and atomic transactions.

Can Redis cluster setup work with different Redis versions?

Redis cluster requires version 3.0 or higher for full clustering support. While clusters can temporarily operate with mixed versions during rolling upgrades, all nodes should run the same Redis version for optimal compatibility and performance. Version mismatches may cause communication issues or feature incompatibilities.

How does Redis cluster failover affect application performance?

Redis cluster failover typically completes within seconds, causing minimal application impact. During failover, clients may experience brief connection errors as replicas promote to master status. Applications using Redis client libraries with cluster awareness automatically reconnect to new masters and continue operations seamlessly.

What happens when Redis cluster loses quorum?

When Redis cluster loses quorum (majority of masters fail), the cluster enters read-only mode to prevent split-brain scenarios. Write operations fail until quorum restores. Setting cluster-require-full-coverage no allows partial cluster operation but risks data inconsistency in network partition scenarios.

How can I monitor Redis cluster performance effectively?

Monitor Redis cluster health using built-in commands like CLUSTER INFO, CLUSTER NODES, and INFO stats. External tools like Redis Exporter for Prometheus provide comprehensive metrics. Key metrics include node status, slot distribution, memory usage, command throughput, and replication lag.

Is it possible to resize Redis cluster hash slots?

Redis cluster hash slot count (16,384) is fixed and cannot be changed. However, slots can be migrated between nodes during rebalancing operations. Adding or removing nodes requires redistributing existing slots, which Redis cluster tools handle automatically during scaling operations.

How do I handle Redis cluster split-brain scenarios?

Redis cluster prevents split-brain situations by requiring majority consensus for master promotion. Nodes in minority partitions become read-only until connectivity restores. Proper network design with redundant connections and monitoring helps minimize partition frequency and duration.

Troubleshooting Redis Cluster Issues

Cluster Formation Failures

Problem: Redis cluster creation fails with "Node is not empty" error messages during initialization.

Solution: Clear existing Redis data and cluster configuration files before attempting cluster creation.

Bash
# Stop all Redis services
for node in 192.168.1.{10..15}; do
    sudo systemctl stop redis-cluster@$node-7000
done

# Clear data directories
for node in 192.168.1.{10..15}; do
    ssh $node "sudo rm -f /var/lib/redis/cluster/*"
    ssh $node "sudo systemctl start redis-cluster@$node-7000"
done

# Recreate cluster
redis-cli --cluster create 192.168.1.10:7000 192.168.1.11:7000 \
192.168.1.12:7000 192.168.1.13:7000 192.168.1.14:7000 192.168.1.15:7000 \
--cluster-replicas 1

Node Communication Problems

Problem: Cluster nodes cannot communicate due to firewall or network configuration issues.

Diagnostic Commands:

Bash
# Test node connectivity
telnet 192.168.1.10 7000
telnet 192.168.1.10 17000

# Check firewall rules
sudo ufw status
sudo iptables -L | grep 7000

# Verify Redis binding configuration
redis-cli -h 192.168.1.10 -p 7000 CONFIG GET bind

Solution: Configure firewall rules and network settings for proper cluster communication.

Bash
# Open required ports
sudo ufw allow from 192.168.1.0/24 to any port 7000
sudo ufw allow from 192.168.1.0/24 to any port 17000

# Update Redis configuration
echo "bind 0.0.0.0" >> /etc/redis/cluster/redis-7000.conf
echo "protected-mode no" >> /etc/redis/cluster/redis-7000.conf
sudo systemctl restart redis-cluster@192.168.1.10-7000

Slot Migration Errors

Problem: Hash slot migration fails with timeout or data inconsistency errors.

Diagnostic Commands:

Bash
# Check slot assignment status
redis-cli -h 192.168.1.10 -p 7000 CLUSTER NODES | grep -E "(importing|migrating)"

# Verify slot coverage
redis-cli -h 192.168.1.10 -p 7000 CLUSTER CHECK

# Monitor migration progress
redis-cli -h 192.168.1.10 -p 7000 CLUSTER COUNTKEYSINSLOT <slot_number>

Solution: Complete or abort failed migrations and verify cluster integrity.

Bash
# Fix stuck slot migration
slot_number=1000
node_id="your-node-id-here"

# Clear migration state
redis-cli -h 192.168.1.10 -p 7000 CLUSTER SETSLOT $slot_number STABLE
redis-cli -h 192.168.1.11 -p 7000 CLUSTER SETSLOT $slot_number NODE $node_id

# Verify fix
redis-cli -h 192.168.1.10 -p 7000 CLUSTER CHECK

Memory and Performance Issues

Problem: Redis cluster nodes experiencing high memory usage or slow performance.

Diagnostic Commands:

Bash
# Check memory usage across nodes
for node in 192.168.1.{10..15}; do
    memory=$(redis-cli -h $node -p 7000 INFO memory | grep used_memory_human)
    echo "Node $node: $memory"
done

# Monitor slow queries
redis-cli -h 192.168.1.10 -p 7000 CONFIG SET slowlog-log-slower-than 10000
redis-cli -h 192.168.1.10 -p 7000 SLOWLOG GET 10

# Check key distribution
redis-cli -h 192.168.1.10 -p 7000 --bigkeys

Solution: Optimize memory usage and balance load distribution across cluster nodes.

Bash
# Configure memory limits and eviction policies
for node in 192.168.1.{10..15}; do
    redis-cli -h $node -p 7000 CONFIG SET maxmemory 2gb
    redis-cli -h $node -p 7000 CONFIG SET maxmemory-policy allkeys-lru
done

# Rebalance cluster slots if needed
redis-cli --cluster rebalance 192.168.1.10:7000

# Monitor improvements
redis-cli -h 192.168.1.10 -p 7000 INFO stats | grep keyspace_hits

Additional Resources

Further Reading

Official Documentation

Community Resources

Related LinuxTips.pro Articles


This comprehensive guide covers Redis cluster setup from basic installation through advanced management techniques. For questions or additional support, join the LinuxTips.pro community and share your Redis clustering experiences.