Lab 17: Redis Cluster & Sentinel

Time: 45 minutes | Level: Advanced | DB: Redis 7

Overview

Redis Sentinel provides automatic failover for single-shard Redis deployments. Redis Cluster provides horizontal sharding across multiple nodes using consistent hash slots (0-16383). This lab covers both high-availability patterns.


Step 1: Redis Sentinel Setup

docker network create redis-sentinel-net

# Start Redis master
docker run -d \
  --name redis-master \
  --network redis-sentinel-net \
  --hostname redis-master \
  redis:7 \
  redis-server --appendonly yes --bind 0.0.0.0

# Start two Redis replicas
for i in 1 2; do
  MASTER_IP=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-master)
  docker run -d \
    --name redis-replica${i} \
    --network redis-sentinel-net \
    redis:7 \
    redis-server --replicaof $MASTER_IP 6379 --appendonly yes --bind 0.0.0.0
  echo "Started replica${i}"
done

sleep 5

# Verify replication
docker exec redis-master redis-cli INFO replication | grep -E "role|connected_slaves|slave[0-9]"

📸 Verified Output:


Step 2: Configure and Start Sentinel Nodes

📸 Verified Output:

💡 Quorum=2 means 2 sentinels must agree a master is down before triggering failover. With 3 sentinels and quorum=2, you need at least 2 sentinels operational.


Step 3: Monitor Sentinel Status

📸 Verified Output:


Step 4: Simulate Master Failure and Sentinel Failover

📸 Verified Output:


Step 5: Cleanup Sentinel and Start Redis Cluster


Step 6: Launch Redis Cluster Nodes

📸 Verified Output:


Step 7: Initialize Redis Cluster

📸 Verified Output:


Step 8: Capstone — Test Hash Slots and Cluster Operations

📸 Verified Output:


Summary

Feature
Sentinel
Cluster

Purpose

HA for single shard

Horizontal scaling

Min nodes

3 sentinels + 1 primary

6 (3 masters + 3 replicas)

Sharding

No

Yes (16384 slots)

Automatic failover

Yes (via sentinels)

Yes (built-in)

Hash slots

N/A

0-16383

Hash tags

N/A

{tag} forces same slot

Command

SENTINEL masters

CLUSTER INFO, CLUSTER NODES

Client

Standard Redis client

Cluster-aware client (-c flag)

Key Takeaways

  • Sentinel: Simple HA for single Redis, no sharding needed

  • Cluster: Automatic sharding + HA; minimum 6 nodes (3 masters + 3 replicas)

  • 16384 hash slots are distributed across masters; add slots to add nodes

  • Hash tags {tag} force co-location of related keys on the same slot

  • redis-cli -c auto-redirects to correct shard (MOVED response handling)

Last updated