Redis Sentinel is a system designed to help managing Redis instances. It performs the following four tasks
Monitoring. Sentinel constantly checks if your master and slave instances are working as expected.
Notification. Sentinel can notify the system administrator, or another computer program, via an API, that something is wrong with one of the monitored Redis instances.
Automatic failover. If a master is not working as expected, Sentinel can start a failover process where a slave is promoted to master, the other additional slaves are reconfigured to use the new master, and the applications using the Redis server informed about the new address to use when connecting.
Configuration provider. Sentinel acts as a source of authority for clients service discovery: clients connect to Sentinels in order to ask for the address of the current Redis master responsible for a given service. If a failover occurs, Sentinels will report the new address.
[21684] 25 Nov 20:54:16 * Slave ask for synchronization
[21684] 25 Nov 20:54:16 * Starting BGSAVE for SYNC
[21684] 25 Nov 20:54:16 * Foregroud saving started by pid 21684
[21684] 25 Nov 20:54:17 * DB saved on disk
[21684] 25 Nov 20:54:17 * Background saving terminated with suc
[21684] 25 Nov 20:54:17 * Synchronization with slave succeeded
Slave1启动时的日志输出
[21068] 25 Nov 20:54:15 * Server started, Redis version 2.4.5
[21068] 25 Nov 20:54:15 * DB loaded from disk: 0 seconds
[21068] 25 Nov 20:54:15 * The server is now ready to accept connections on port
[21068] 25 Nov 20:54:16 - DB 0: 1 keys (0 volatile) in 4 slots HT.
[21068] 25 Nov 20:54:16 - 0 clients connected (0 slaves), 1180024 bytes in use
[21068] 25 Nov 20:54:16 * Connecting to MASTER...
[21068] 25 Nov 20:54:16 * MASTER <-> SLAVE sync started
[21068] 25 Nov 20:54:16 * Non blocking connect for SYNC fired the event.
[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: receiving 19 bytes from master
[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: Loading DB in memory
[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: Finished with success
E:\devsoftware\redis-2.4.5-win32-win64\64bit>redis-cli.exe -h 127.0.0.1 -p 6379
redis 127.0.0.1:6379> set abc 1
OK
redis 127.0.0.1:6379> get abc
"1"
redis 127.0.0.1:6379>Ctrl + C
Since Redis 2.6 by default slaves are read-only.
Note: read only slaves are not designed to be exposed to untrusted clients on the internet. It's just a protection layer against misuse of the instance. Still a read only slave exports by default all the administrative commands
such as CONFIG, DEBUG, and so forth. To a limited extend you can improve security of read only slaves using 'rename-command' to shadow all the administrative / dangerous commands.
Sentinels and Slaves auto discovery
While Sentinels stay connected with other Sentinels in order to reciprocally check the availability of each other, and to exchange messages, you don't need to configure the other Sentinel addresses in every Sentinel instance you run, as Sentinel uses the Redis master Pub/Sub capabilities in order to discover the other Sentinels that are monitoring the same master.
This is obtained by sending Hello Messages into the channel named __sentinel__:hello.
Similarly you don't need to configure what is the list of the slaves attached to a master, as Sentinel will auto discover this list querying Redis.
Every Sentinel publishes a message to every monitored master and slave Pub/Sub channel __sentinel__:hello, every two seconds, announcing its presence with ip, port, runid.
Every Sentinel is subscribed to the Pub/Sub channel __sentinel__:hello of every master and slave, looking for unknown sentinels. When new sentinels are detected, they are added as sentinels of this master.
Hello messages also include the full current configuration of the master. If another Sentinel has a configuration for a given master that is older than the one received, it updates to the new configuration immediately.
Before adding a new sentinel to a master a Sentinel always checks if there is already a sentinel with the same runid or the same address (ip and port pair). In that case all the matching sentinels are removed, and the new added.