Redis sentry mechanism

  php, redis



Last articleMainly saidRedis copied contentHowever, Redis replication has a disadvantage. When the Master of the host goes down, we need to manually solve the handover, such as using slaveof no one. In fact, master-slave replication has not been realized. High availability focuses on backup machines. The redundancy of the system in the cluster is utilized. When one machine in the system is damaged, other backup machines can quickly replace it to start the service.

The Problem of Master-Slave Replication


Once the primary node goes down and the write service is unavailable, it needs to be manually switched, the primary node is re-selected and the master-slave relationship is manually set.

So how to solve it? If we have a monitoring program that can monitor the status of each machine and make timely adjustments, the manual operation will become automatic. Sentinel came into being to solve this problem.

Principle and Implementation of Sentinel Mechanism

Redis Sentinel

Redis Sentinel is a distributed architecture, which includes several Sentinel nodes and Redis data nodes. Each Sentinel node will monitor the data node and other Sentinel nodes. When it finds that the node is unreachable, it will mark the node as offline. If the identified node is the master node, it will also “negotiate” with other Sentinel nodes. when most Sentinel nodes think that the master node is unreachable, they will elect a Sentinel node to complete the automatic failover and will notify the Redis application of this change in real time. The whole process is completely automatic and does not require manual intervention, so this scheme effectively solves the problem of high availability of Redis.

As shown in the figure:

Basic failover process

1) The master node fails. At this time, the two slave nodes lose connection with the master node and master-slave replication fails.


2) Each Sentinel node discovers the failure of the primary node through regular monitoring


3) If multiple Sentinel nodes reach an agreement on the failure of the primary node, one of the nodes will be elected as the leader to be responsible for the failover.


4) The 4)Sentinel leader node performed failover, and the whole process was basically the same as our manual adjustment, but it was completed automatically.

5) After the failover, the entire Redis Sentinel structure has re-elected a new master node.



Use docker to create the following redis container, which can be referenced here[Advanced ]docker Arranges PHP Development EnvironmentLinux docker-compose actual combatLearn about container technology

redis-sentinel1    22530 -> 22530    sentinel
 redis-sentinel2    22531 -> 6379    sentinel
 redis-sentinel3    22532 -> 6379    sentinel
 redis-master2    6383  -> 6379    Master
 redis-slave2    6384  -> 6379    Slave
 redis-slave3    6385  -> 6379    Slave


Sentinel’s Core Configuration

sentinel monitor mymaster 7000 2

The name, IP and port of the monitored master node, and the last 2 means that several Sentinels will fail over if problems are found. For example, if the configuration is 2, it means that at least two sentinel nodes think the master node is unreachable, then the unreachable determination is objective. The smaller the setting, the looser the conditions for getting offline, and the stricter the reverse. It is generally recommended to set it to half of Sentinel node plus 1.

sentinel down-after-millseconds mymaster 30000

This is the timeout period (in milliseconds). For example, when you ping a machine, how long does it take before the ping fails, then you think there is a problem.

sentinel parallel-syncs mymaster 1

When the Sentinel node set reaches an agreement on the failure determination of the master node, the Sentinel leader node will perform a failover operation to select a new master node, and the original slave node will initiate a replication operation to the new master node. parallel-syncs is used to limit the number of slave nodes that initiate a replication operation to the new master node after a failover, indicating whether Sentinel is concurrent or serial. 1 means that only one copy can be made at a time, which can reduce the pressure on Master.


sentinel auth-pass <master-name> <password>

If the master node monitored by Sentinel is configured with a password, sentinel auth-pass configuration prevents Sentinel node from failing to monitor the master node by adding the password of the master node.

sentinel failover-timeout mymaster 180000

Indicates the time of failover.


1)Sentinel nodes should not be deployed on a physical “machine”.

It is specially emphasized here that a physical machine is made into several virtual machines or popular containers nowadays. Although they have different IP addresses, they are actually the same physical machine. The same physical machine means that if this machine has any hardware failure, all virtual machines will be affected. In order to realize real high availability of Sentinel node set, please do not deploy Sentinel nodes on the same physical machine.

2) Deploy at least three and odd Sentinel nodes.

More than 3 nodes improve the accuracy of fault determination by increasing the number of Sentinel nodes, because the leader election requires at least half plus 1 node, and odd nodes can save 1 node on the basis of meeting this condition.