๐ก๏ธ Redis Sentinel โ
Why Sentinel? โ
Replication solves the read scaling problem, but if the master goes down, you need manual intervention โ getting woken up at 3am, frantically changing configs, praying no data is lost.
Sentinel solves exactly this: automatic monitoring, automatic failover, automatic notifications. You'll never have to get up in the middle of the night to manually switch over again.
๐ก Tip: ๐ก Analogy: Replication means "having a backup server," Sentinel means "having a 24/7 on-call operator that automatically switches to the backup when the primary server fails."
Sentinel Architecture โ
Sentinel itself is also a Redis process, but it doesn't store data โ it's only responsible for monitoring and failover. You need at least 3 Sentinel nodes (odd number for voting).
โโโโโโโโโโโโโโโ
โ Sentinel #1 โ
โโโโโโโโฌโโโโโโโโ
โ Monitor
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ
โSentinel#2โ โSentinel#3โ โ โ
โโโโโโโโโโโโ โโโโโโโโโโโโ โ Client โ
โโโโโโโโโโโโ
โโโโโโโโโโโโโโโ
โ Master โ
โโโโโโโโฌโโโโโโโ
โ Replication
โโโโโโโโดโโโโโโโ
โผ โผ
โโโโโโโโโโโโ โโโโโโโโโโโโ
โ Replica1 โ โ Replica2 โ
โโโโโโโโโโโโ โโโโโโโโโโโโConfiguring Sentinel โ
# /etc/redis/sentinel.conf (Sentinel configuration file)
# Monitor master (quorum=2 means at least 2 Sentinels must agree for failover)
sentinel monitor mymaster 127.0.0.1 6379 2
# Seconds without response before master is considered "subjectively down"
sentinel down-after-milliseconds mymaster 5000
# Failover timeout
sentinel failover-timeout mymaster 30000
# How many replicas reconfigure to new master simultaneously
sentinel parallel-syncs mymaster 1
# Master password (if set)
sentinel auth-pass mymaster your_password
# Notification script (executed after failover, e.g. send alert)
sentinel notification-script mymaster /opt/scripts/alert.sh
# Client reconfiguration script
sentinel client-reconfig-script mymaster /opt/scripts/reconfig.shStarting and Checking Status โ
# Start 3 Sentinel instances (different ports)
redis-sentinel /etc/redis/sentinel-26379.conf
redis-sentinel /etc/redis/sentinel-26380.conf
redis-sentinel /etc/redis/sentinel-26381.conf
# Or use systemctl
systemctl start redis-sentinel
# View Sentinel status
redis-cli -p 26379 sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6379"
7) "flags"
8) "master"
# View current replicas
redis-cli -p 26379 sentinel replicas mymaster
# View other Sentinel nodes
redis-cli -p 26379 sentinel sentinels mymaster
# Simulate failure: stop master
redis-cli -p 6379 shutdown
# Observe Sentinel logs โ automatic failover
[4290] 20 Jan 10:30:15.123 # +sdown master mymaster 127.0.0.1 6379
[4290] 20 Jan 10:30:20.456 # +odown master mymaster 127.0.0.1 6379
[4290] 20 Jan 10:30:25.789 # +failover-attempt master mymaster 127.0.0.1 6379
[4290] 20 Jan 10:30:26.012 # +failover-end master mymaster 127.0.0.1 6379
[4290] 20 Jan 10:30:26.234 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6380Client Connecting to Sentinel โ
# Python client connecting to Sentinel
from redis.sentinel import Sentinel
# Connect to Sentinel cluster
sentinel = Sentinel([
('127.0.0.1', 26379),
('127.0.0.1', 26380),
('127.0.0.1', 26381),
], socket_timeout=0.5)
# Get master connection (auto-discovery, automatic failover)
master = sentinel.master_for('mymaster', socket_timeout=0.5, password='your_password')
master.set('key', 'value')
# Get replica connection (read-write separation)
replica = sentinel.replica_for('mymaster', socket_timeout=0.5, password='your_password')
value = replica.get('key')
# Java client (Jedis)
// Set<String> sentinels = Set.of("127.0.0.1:26379", "127.0.0.1:26380");
// JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels, "password");โ ๏ธ Note: โ ๏ธ Clients must support Sentinel! If you're using a plain Redis client (hardcoded IP:Port), it won't connect to the new master after failover. Python's redis-py, Java's Jedis/Lettuce, and Node's ioredis all support Sentinel.