Redis master slave replication and sentinel mode (2)


Redis master-slave replication

In order to share the data service pressure of stand-alone redis, read-write separation is needed. Therefore, a master-slave structure of redis is built. The master node is responsible for writing and the slave node is responsible for reading. The master node synchronizes the data to the slave node on a regular basis.

Configuration master slave

#Which redis is the main line added to the configuration file
slaveof ip port
#Password of the master node
masterauth h123

#Read only from node, default configuration
slave-read-only yes
#Turn off TCP? Nodelay. No matter how big the packet is, it will be sent directly. By default, it is OK
repl-disable-tcp-nodelay no
#By default, the master node sends heartbeat packets to the slave node every 10 seconds, which is OK by default
repl-ping-slave-period 10

Master slave topology

Slavof only indicates which slave node the machine is, so it is flexible to implement master-slave structure

  • One master and one slave: used for the master node failover slave node. When the “write” command of the master node is highly concurrent and needs persistence, AOF can be enabled only at the slave node (not required by the master node), which ensures the data security and avoids the impact of persistence on the master node
  • One master and many slaves: used to read more scenes, but this will cause the master node to need to synchronize more slaves and affect the bandwidth of the master node.
  • Tree Master Slave: the master node pushes fewer slave nodes, and the slave node recursively pushes

have access toinfoCommand to query the number and location of current redis slave nodes. You need to traverse the query one by one to get a complete topology

Data synchronization

Redis 2.8 and abovepsyncCommand complete synchronization, process subFull volume replicationAndPartial replication

  • Full replication: generally used in the first replication scenario (full replication after the first slave establishment)
  • Partial replication: if there is a problem in the network, when the slave node connects to the master node again, the master node reissues the missing data and synchronizes the data incrementally every time

Fault repair

When the primary node fails, it can no longer provide external write service, but the slave node can provide read service

One of them can be transformed from a node to a master node, which requires manual processing and cannot be highly available

Processing flow

  1. Modify a slave node as the master node and execute at the nodeslaveof no one
  2. Let other slave nodes use the current master node as the master node, and redis cli executesslaveof ip port

Redis sentinel mechanism

Sentinel mechanism is mainly to solve the problem that master-slave replication cannot automatically switch between master nodes. When the master node fails, sentinel will automatically complete fault discovery and repair, and notify the application party to achieve high availability

Configure the sentinel node profile and launch three Sentinels

#Background start
daemonize yes
#Monitoring master node
sentinel monitor mymaster 6379 1
#Start sentry 
./redis-sentinel conf/sentinel.conf

Sentinel’s surveillance mission

  1. Sent every 10 seconds to the master and slave nodesinfoCommand to get the latest topology
  2. Each sentinel node will send the sentinel node’s judgment on the master node and the current sentinel node’s information to the specified topic of the redis data node every 2 seconds (Hello * *); at the same time, each sentinel node will also subscribe to this topic to understand the judgment of other nodes, which is actually completed through publish / subscribe.
  3. Every sentinel will send a ping command to the master node, slave node and other sentinel nodes every 1 second to check whether the nodes are normal.

Judge whether the master node is abnormal

Sdown: ping command if thedown-after-millisecondsIf no effective reply is received, the data node is considered to be offline subjectively.

Objective offline: the number of sentinels over the election is abnormal

When a sentinel node judges that the master node is abnormal, the sentinel node will pass the commandsentinel is-masterdown-by-addrSeek the judgment of other sentinel nodes on the master node. When the sentinel nodes that exceed the number of quorum (election) answer the offline, it is considered that the master node is offline objectively

Election of sentinel leaders

When the primary node is offline, a sentinel node needs to be selected as the sentinel leader to complete the subsequent work of selecting a new primary node.

Redis master slave replication and sentinel mode (2)

The general idea of this election is:

  • Each sentinel node is sent bysentinel is-master-down-by addrOrder to apply for sentinel leadership.
  • And each sentinel node is receiving asentinel is-master-down-by addrIn the command, only the first node is allowed to vote, and the command of other nodes will be rejected.
  • If a sentinel node receives more than half of the votes, it becomes the sentinel leader.
  • If the first three steps do not select a sentinel leader within a certain period of time, the next election will be resumed.

As you can see, the process of electing leaders is similar to that of electing leaders in raft.

How to choose who becomes the master node

Redis master slave replication and sentinel mode (2)

Among the remaining slave nodes, select the new master node in the following order:

  • Filter out “unhealthy” data nodes, such as subjective offline, disconnected slave nodes, nodes that haven’t replied to the sentinel node ping command within five seconds, and slave nodes that have lost contact with the master node.
  • Select the slave node with the highest slave priority. If it exists, return it. If it does not exist, continue the following process.
  • Select the slave node with the largest copy offset, which means that the data above the slave node is the most complete. If it exists, it will return. If it does not exist, it will continue the following process.
  • At this point, all the remaining slave nodes are in the same state. Select the slave node with the lowest runid.

Promote slave node to master node

Finally, the slave node is transferred to the master node, and other slave nodes use it as the master node. It is the same process as manual processing, except that the initiator becomes a sentry

  1. Modify it as the master node and execute it at the nodeslaveof no one
  2. Let other slave nodes execute as the current master nodeslaveof ip port
  3. The sentinel node collection will update the original master node to the slave node, and command it to copy the data of the new master node after it recovers.

    A little promotion

    It’s not easy to create. I hope to support my open source software and my gadgets. Welcome to gitee to click stars, fork, and mention bugs.

Excel general import and export, support excel formula
Blog address:

Use template code, generate code from database, and some small tools often used in projects
Blog address:

Recommended Today

Laravel service container must know

The article was forwarded from the professional laravel developer community. Original link: To learn how to build an application with laravel is not only to learn how to use different classes and components in the framework, but also to remember allartisanCommand or all helper functions (we have Google). Learning to code with laravel is […]