Home >Database >Redis >Learn about the sentry mode in Redis in one article

Learn about the sentry mode in Redis in one article

青灯夜游
青灯夜游forward
2022-01-04 10:10:452264browse

This article will introduce you to the sentinel mode in Redis. I hope it will be helpful to you!

Learn about the sentry mode in Redis in one article

#Redis master-slave mode. Once the master node fails, the slave node can be promoted to the master node. At the same time, the client must be notified to update the master node address. This is generally not possible. OK. Therefore, Redis provides Redis Sentinel Sentinel mechanism to solve this problem. [Related recommendations: Redis video tutorial]

Problems with master-slave replication

Learn about the sentry mode in Redis in one article

1. Benefits of master-slave replication

  • If the master node fails, the slave node will be upgraded to the master node
  • Expand the read capacity of the master node and share the pressure of the master node

2. Existing problems

  • The process of upgrading the master node from the slave node requires manual intervention, and at the same time, the client Redis service address must be changed
  • The write capability of the master node, Storage capacity is affected by single-machine limit
  • performance: after Redis replication is interrupted, the slave node will initiate psync. If the synchronization fails at this time, full synchronization will be performed. While the main database performs full backup, millisecond or second-level lag may occur
Sentinel Implementation Principle

1. Some concepts

Main functions

  • Monitoring: Constantly check the master and slave Whether the server is running normally
  • Notification: Once a node fails, other nodes will be notified
  • Automatic failover:When the main node cannot work properly Failover will be performed automatically, and one of the slave nodes will be upgraded to the master node
  • Configuration provider: The client does not configure a single node, but a Sentinel node collection

Subjective offline and objective offline

Generally speaking, each

Sentinel node will continuously monitor other Sentinel Node and Redis node send PING, and confirm whether they are online by replying or not

  • Subjective offline: Applicable to For all master nodes and slave nodes, if Sentinel does not receive a valid reply from the target node within down-after-milliseconds milliseconds, the node will be determined to be subjectively offline.
  • Objective offline: Only used for the master node. If the master node fails, the Sentinel node will pass sentinel is-master-down-by- The addr command asks other Sentinel nodes to determine the status of this node. If more than nodes determine that the primary node is unreachable, the Sentinel node will determine that the primary node is objectively offline.

2. Working principle

Learn about the sentry mode in Redis in one article

  • Each Sentinel sends to other Sentinel nodes and Redis master-slave nodes at a frequency of 1 times/s PING command.
  • If an instance is more than down-after-milliseconds from the last valid reply to the PING command, the instance is Sentinel marked as Subjective offline .
  • If a masterserver is marked as subjectively offline, then all Sentinel nodes of this masterserver are being monitored, 1 time/s Confirm whether this master server has entered the subjective offline state
  • If more than <quorum></quorum> nodes determine that the master node is unreachable, the Sentinel node will It is determined that the master node is objectively offline.
  • When the main server is marked as objectively offline, Sentinel will send the INFO command to all the servers of the offline server, which will start from 10 times/s is changed to 1 times/s.
  • Sentinel The nodes negotiate the master node status. If the master node is in the SDOWN state, the new master node will be automatically selected by voting. Point the remaining slave nodes to the new primary node for data replication.
  • When there are not enough Sentinel to agree that the main server is offline, the objective offline status of the main server will be removed. When the primary server returns a valid reply to the PING command of Sentinel, the of the primary server The subjective offline status will be removed.

3. Message loss

Redis adopts the master-slave replication mode. Once the master node hangs up, the data being synchronized by the slave node may be lost and delayed. The bigger it is, the more is lost.

Redis provides two configuration items to limit the request processing of the main library, namely min-slaves-to-write and min-slaves-max-lag .

  • min-slaves-to-write: This configuration item sets the minimum number of slave libraries that the main library can perform data synchronization;
  • min-slaves-max-lag: This configuration This item sets the maximum delay (in seconds) for the slave library to send an ACK message to the master library when data is copied between the master and slave libraries.

The requirement after combining these two configuration items is that there are at least N slave libraries in the slave library connected to the main library, and the ACK message delay when copying data with the main library cannot exceed T seconds, otherwise, the main library will no longer receive client requests.

So, Sentine cannot guarantee that messages will not be lost at all, but it can also try to ensure that messages are lost as little as possible.

Summary

Sentinel solves the problem of high availability but does not solve the problem of single-node expansion of the master node.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of Learn about the sentry mode in Redis in one article. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete