Redis Sentinel ให้ Simple และ Automatic High Availability (HA) Solution สำหรับ Redis คุ้นเคย MongoDB Elections ไม่ต่างมาก Start: Master Replicates To N Slaves Sentinel Daemons Running (App Servers หรือ Redis Servers) Track Master Health
Sentinel Detect Master Non-responsive -> Broadcast SDOWN (Subjectively Down) Other Sentinels Quorum Reached Master Down -> Broadcast ODOWN (Objectively Down) New Master Elected Need Quorum/Majority Sentinels Agree ODOWN -> Best Practice: Odd Number Sentinels Avoid Ties
Note: Highly Recommended Redis 2.8 Branch+ Best Performance With Sentinel
How It Works
Sentinels Handle Failover By Re-writing Config Files Redis Instances Scenario:
Master "A" Replicates To Slaves "B" และ "C" 3 Sentinels (s1, s2, s3) On App Servers Write To Redis "A" Offline Sentinels See "A" Offline Send SDOWN Messages Agree "A" Down -> "A" ODOWN Status Election: Who Most Ahead? "B" Chosen New Master
Config "B" -> No Longer Slave Anyone Config "C" Rewritten -> No Longer Slave "A" But "B" Continue Normal "A" Comes Back Online Sentinels Recognize Rewrite Config "A" -> Slave Of "B" (Current Master)
Configuration
Config Sentinels Not Hard Hardest: Where To Place Sentinel Processes Recommend Run On App Servers If Possible Presumably Setup -> Concerned Write Availability Master Sentinels Provide Insight App Server Talk To Master Run On Redis Instance Servers Also OK
Config Step: Reference Example File Example sentinel.conf Redis 2.8.4 Ubuntu 14.04 Works Any 2.8.x Added 2 Lines Top:
daemonize yes logfile /var/log/redis/redis-sentinel.log
Daemonize Mode Log Messages To File Not Stdout
Configurable Options เยอะ Commented Well Focus 2 Options:
Most Important: Tell Sentinels Where Current Master Resides Line:
sentinel monitor mymaster 127.0.0.1 6379 2
Monitor "mymaster" (Arbitrary Name) IP/Port + Quorum Sentinels For Failover (Min 2) Change: Master IP/Port (If Not Standard 6379)
Next May Want Change:
sentinel down-after-milliseconds mymaster 30000
Time Sentinel Wait Before Declare Master SDOWN Default 30s Typically Lower To 10s Don't Reduce Too Low -> Failovers Too Often
Look Other Options Notification Script Interesting Track Failovers
sentinel.conf Configured -> Start Daemon:
redis-server /path/to/sentinel.conf --sentinel
Testing Failover
Sentinels Online -> Dry Run Failover Ensure Configured Correctly
Connect Sentinel Via redis-cli:
redis-cli -p 26379
Info Command:
127.0.0.1:26379> INFO
Info: Current Master, Slaves Count, Sentinels Monitoring
Test Failover Execute:
127.0.0.1:26379> SENTINEL failover mymaster
Force ODOWN Current Master -> Failover Happens Shortly Run INFO Again -> New Master Listed
Conclusion
Hopefully Helpful Demystify Redis และ Sentinel Questions -> Post Below!