Speaking of the high availability of Redis servers, how can you ensure that the backup machine is a complete backup of the original server? This is where sentry and replication are needed.

  • Sentinel: Can manage multiple Redis servers, providing monitoring, alerting, and automatic failover.
  • Replication: A server responsible for making a Redis server capable of hosting multiple backups.

Redis uses these two features to make Redis highly available.

Sentry (sentinal)

Sentinel is a very important component in Redis cluster architecture. Sentinel mainly solves the problem of human intervention when the master/slave replication fails.

1. Main functions of Redis Sentry

(1) Cluster monitoring: responsible for monitoring whether the Redis master and Slave processes work normally

(2) Message notification: If a Redis instance fails, the sentry is responsible for sending a message as an alarm notification to the administrator

(3) Failover: If the master node fails, it is automatically transferred to the slave node

(4) Configuration center: If failover occurs, notify client client of the new master address

2. High availability of Redis Sentry

Principle: When the primary node fails, Redis Sentinel automatically completes the fault discovery and transfer, and informs the application party to achieve high availability.

The sentinel mechanism sets up multiple sentinel nodes (processes) to jointly monitor the health of data nodes.

At the same time, sentinel nodes communicate with each other and exchange the monitoring status of master and slave nodes.

Every second, each Sentinel sends a ping command to the entire cluster: Master Master server, Slave Slave server, and other Sentinel processes for heartbeat detection.

This is an important basis for sentry to determine whether a node is healthy or not. It involves two new concepts: subjective and objective logoff.

1. Subjective logoff: A sentinel node determines that the primary node is subjective logoff when it goes down.

2. Objective offline: Only half of the sentinels judged that the master node was down subjectively, and only when multiple sentinels exchanged the subjective judgment results, could the master node be judged to be objectively offline.

3. Principle: Basically, whichever sentry node decides that the master node is offline objectively first will initiate Raft algorithm (election algorithm) in each sentry node. Finally, the sentry node who is voted as the leader completes the automatic master/slave switching process.

Redis Replication (Replication)

To solve the problem of single point database, Redis will copy multiple copies of data and deploy them to other nodes. Through replication, Redis achieves high availability and redundant backup of data to ensure high reliability of data and services.

1. Data Replication Principle (Procedure)

① Send the sync command from the database to the primary database.

② After receiving the synchronization command, the primary database saves the snapshot and creates an RDB file.

(3) After the primary database completes the hold snapshot, it sends the RDB file to the secondary database, and the secondary database receives and loads the file.

④ The primary database sends all write commands of the buffer to the secondary server.

⑤ After the above processing, each write command executed by the master database will be sent to the slave database.

Note: After Redis2.8, incremental replication is performed after master/slave disconnection and reconnection based on the latest command offset prior to disconnection.

What is the difference between Redis master-slave replication, sentinel, and cluster

1. Master-slave mode: Read/write separation, backup, a Master can have multiple Slaves.

2. Sentinel: Monitoring, automatic transfer. When the sentinel finds that the master server is suspended, it elects a new master server from the slave.

3. Cluster: In order to solve the problem of limited Redis capacity in a single machine, data is allocated to multiple machines according to certain rules. The memory /QPS is not limited to a single machine, which can benefit from the high scalability of distributed cluster.

This article is published by OpenWrite!