Skip to content

๐Ÿ›ก๏ธ 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).

text
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚ Sentinel #1  โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                โ”‚ Monitor
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚               โ”‚               โ”‚
โ–ผ               โ–ผ               โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚Sentinel#2โ”‚ โ”‚Sentinel#3โ”‚ โ”‚          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚  Client   โ”‚
                          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚   Master    โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                โ”‚ Replication
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ–ผ             โ–ผ
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚ Replica1 โ”‚ โ”‚ Replica2 โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Configuring Sentinel โ€‹

text
# /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.sh

Starting and Checking Status โ€‹

bash
# 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 6380

Client Connecting to Sentinel โ€‹

python
# 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.