Redis Cluster & Sentinel
High availability and horizontal scaling
Skills taught: redis.clustering
Redis Primary-Replica Replication
Before Sentinel or Cluster, understand replication. A replica synchronises all writes from the primary in real time. Replicas serve reads, reduce load, and provide a failover target. But failover is manual without Sentinel.
# Start a replica that replicates from primary at 6379
redis-server --port 6380 --replicaof 127.0.0.1 6379
# Check replication status
redis-cli -p 6379 INFO replication
# role:master
# connected_slaves:1
# slave0:ip=127.0.0.1,port=6380,state=online,offset=...
redis-cli -p 6380 INFO replication
# role:slave
# master_host:127.0.0.1
# master_port:6379
# master_link_status:up
Redis Sentinel — Automated Failover
Sentinel monitors the primary and replicas. When the primary fails, Sentinels vote and promote a replica automatically — typically within 10–30 seconds.
# sentinel.conf (run 3 Sentinels for quorum of 2)
sentinel monitor mymaster 127.0.0.1 6379 2
# 2 = quorum (how many Sentinels must agree primary is down)
sentinel down-after-milliseconds mymaster 5000
# Mark primary down after 5 seconds of no response
sentinel failover-timeout mymaster 60000
# Failover must complete within 60 seconds
sentinel parallel-syncs mymaster 1
# Only 1 replica syncs with new primary at a time (reduces load)
# Start Sentinels
redis-sentinel sentinel.conf &
redis-sentinel sentinel2.conf &
redis-sentinel sentinel3.conf &
# Check Sentinel status
redis-cli -p 26379 SENTINEL masters
redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster
Connecting via Sentinel in Node.js
import Redis from 'ioredis';
// Connect via Sentinel — automatically follows failovers
const redis = new Redis({
sentinels: [
{ host: '127.0.0.1', port: 26379 },
{ host: '127.0.0.1', port: 26380 },
{ host: '127.0.0.1', port: 26381 },
],
name: 'mymaster', // must match 'sentinel monitor <name>'
role: 'master', // 'master' for writes, 'slave' for reads
});
// The client asks Sentinel for the current primary address
// and reconnects automatically after failover
await redis.set('key', 'value');
Redis Cluster — Sharding + HA
Redis Cluster shards data across multiple nodes using 16384 hash slots. Each primary owns a slot range. Replicas provide failover. Keys route automatically via CRC16.
# Check cluster health
redis-cli -p 7000 CLUSTER INFO
# cluster_state:ok
# cluster_slots_assigned:16384
# cluster_known_nodes:6
# See which nodes own which slots
redis-cli -p 7000 CLUSTER NODES
# See where a key would go
redis-cli -p 7000 CLUSTER KEYSLOT user:1 # slot number
redis-cli -p 7000 CLUSTER KEYSLOT user:2 # different slot
# Hash tags: force co-location
redis-cli -p 7000 CLUSTER KEYSLOT "{user:1}.name" # same slot
redis-cli -p 7000 CLUSTER KEYSLOT "{user:1}.email" # same slot
Sentinel vs Cluster
- Redis Sentinel: HA only — no sharding
- Redis Cluster: Sharding + HA
Tip: For most applications, start with Redis Sentinel (simpler, full compatibility). Only move to Cluster when your dataset exceeds single-node capacity. Many teams use managed Redis (AWS ElastiCache, Upstash, Redis Cloud) to avoid managing Sentinel or Cluster manually.
Frequently Asked Questions
Should I use Sentinel or Cluster?
Use Sentinel for HA without sharding (simpler, full command compatibility). Use Cluster only when you need horizontal scale — your dataset doesn't fit in one node.
What happens during a Sentinel failover?
Sentinels detect the primary is down, vote to agree, elect a Sentinel leader, choose a replica to promote, reconfigure other replicas to follow the new primary, and notify clients. Total time: typically 10-30 seconds.
How do managed Redis services handle HA?
AWS ElastiCache, Redis Cloud, and Upstash abstract Sentinel/Cluster internally. ElastiCache offers 'Cluster Mode Disabled' (Sentinel-like) and 'Cluster Mode Enabled' (sharded Cluster). Failover is transparent to your application.