Redis offers three main clustering solutions: Redis Cluster, master/slave replication, and Sentinel mode for automatic failover and fault recovery.
Sentinel Mode Overview
Sentinel mode builds upon the master/slave replication model by introducing monitoring agents that automatically handle failures. Traditional master/slave setups require manual intervention when the master server fails, which is time-consuming and results in service downtime. Sentinel addresses this limitation by providing automated failover capabilities.
Sentinel State Persistence
Sentinel states are persistently stored in the sentinel configuration file. Each time a new configuration is received or created, it's written to disk with a version timestamp. This ensures safe stopping and restarting of santinel processes.
Sentinel Functions
- Master server health monitoring
- Automatic master-slave failover when master fails
- Configuration updates across master, slave, and sentinel configurations
How Sentinel Operates
Each Sentienl instance performs several periodic tasks:
- Sends PING commands to master, slaves, and other Sentinels every second
- Marks instances as subjectively down if they don't respond within the configured timeout
- Confirms master failure with other Sentinels when enough agree on the failure
- Sends INFO commands to all known masters and slaves every 10 seconds
- Increases INFO command frequency to slaves when master is down
Three Core Monitoring Tasks
- Topology Discovery: Every 10 seconds, each Sentinel sends INFO commands to master and slaves to discover the current cluster topology. This allows Sentinels to detect new slaves as they join.
- Sentinel Communication: Every 2 seconds, Sentinels publish their status and master assessments to a common channel while subscribing to the same channel to learn about other Sentinels.
- Heartbeat Monitoring: Every second, Sentinels send PING commands to master, slaves, and other Sentinels to verify node health.
Sentinel Deployment Process
First, ensure Redis master-slave replication is properly configured.
Create Sentinel Directories
mkdir -p /opt/redis/sentinel
Create Sentinel Configuration Files
cat > /opt/redis/sentinel/sentinel.conf << EOF
port 26380
dir "/opt/redis/sentinel"
logfile /opt/redis/sentinel/sentinel.log
protected-mode no
daemonize yes
sentinel monitor redis-primary 192.168.1.10 6379 2
sentinel down-after-milliseconds redis-primary 5000
sentinel auth-pass redis-primary secure123
EOF
Configuration Parameters:
sentinel monitor redis-primary 192.168.1.10 6379 2: Defines the initial master server and requires 2 Sentinel votes for failover decisionssentinel down-after-milliseconds redis-primary 5000: Marks master as down after 5 seconds without responsesentinel auth-pass redis-primary secure123: Authentication password for Redis cluster access
Start Sentinel
redis-sentinel /opt/redis/sentinel/sentinel.conf
netstat -lntup | grep 26380
tail -f /opt/redis/sentinel/sentinel.log
Simulate Master Failure
redis-cli -a secure123 -p 6379 shutdown
redis-cli -a secure123 -p 6379 info replication
Sentinel will automatically detect the master failure and initiate failover, promoting a slave to master. The new master will be 192.168.1.11 in this example.
Recover Failed Master
systemctl start redis
netstat -lntup | grep 6379
The recovered node will automatically rejoin as a slave.
Common Sentinel Commands
PING
Check if Redis or Sentinel instances are responding:
redis-cli -a secure123 -p 26380 PING
# Returns: PONG
SENTINEL masters
List monitored master servers and their status:
redis-cli -a secure123 -p 26380 sentinel masters
SENTINEL slaves
List slaves for a specific master:
redis-cli -a secure123 -p 26380 sentinel slaves redis-primary
SENTINEL get-master-addr-by-name
Retrieve IP and port of the current master:
redis-cli -a secure123 -p 26380 sentinel get-master-addr-by-name redis-primary
SENTINEL reset
Reset monitoring state for a master:
redis-cli -a secure123 -p 26380 sentinel reset redis-primary
SENTINEL failover
Force manual failover:
redis-cli -a secure123 -p 26380 sentinel failover redis-primary