When it comes to high availability of redis server, how to ensure that the backup machine is a complete backup of the original server? Sentinels and reproductions are needed.
- Sentinel:It can manage multiple redis servers. It provides monitoring, reminding and automatic failover functions.
- Replication:It is responsible for enabling a redis server to be equipped with multiple backup servers.
Redis uses these two functions to ensure high availability of redis.
Sentinel is a very important component of redis cluster architecture. Sentinel mainly solves the problem of human intervention when master-slave replication fails.
1. Main functions of redis sentry
(1) Cluster monitoring:Responsible for monitoring whether redis master and slave processes work normally
(2) Message notification:If a redis instance fails, the sentry is responsible for sending a message to the administrator as an alarm
(3) Failover:If the master node hangs up, it will be automatically transferred to the slave node
(4) Configuration center:Notify client client of new master address if failover occurs
2. High availability of redis sentry
Principle: when the primary node fails, redis sentinel will automatically complete the failure discovery and transfer, and notify the application party to achieve high availability.
The sentinel mechanism establishes multiple sentinel nodes (processes) to jointly monitor the operation of data nodes.
At the same time, sentinel nodes also communicate with each other to exchange the monitoring status of master-slave nodes.
Every second, every sentinel will send a ping command to the whole cluster: master master master server + Slave Slave server + other sentinel processes for heartbeat detection.
This is the important basis for sentinels to judge whether nodes are normal or not, involving two new concepts:Subjective offline and objective offline.
1. Subjective offline:A sentinel node determines that the downing of the primary node is a subjective downline.
2. Objective offline:Only when half of sentinel nodes subjectively determine that the primary node is down, when multiple sentinel nodes exchange subjective judgment results, the objective offline of the primary node will be determined.
3. principle:Basically, which sentinel node is the first to determine the objective offline of this master node will initiate the vote mechanism raft algorithm (election algorithm) in each sentinel node, and finally the sentinel node that is voted as the leader will complete the process of master-slave automatic switching.
In order to solve the problem of single point database, redis will deploy multiple copies of data replication to other nodes. Through replication, redis can achieve high availability, redundant data backup and high reliability of data and services.
1. Principle of data replication (execution steps)
① Send a sync command from the database to the main database.
② After receiving the synchronization command, the master database will save the snapshot and create an RDB file.
③ When the master database finishes keeping the snapshot, the RDB file is sent to the slave database, and the slave database receives and loads the file.
④ The master database sends all write commands of the buffer to the slave server for execution.
⑤ After the above processing, each write command executed by the master database will be sent to the slave database.
Note: after redis2.8, the master-slave will perform incremental replication based on the latest command offset before disconnection.
What’s 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 main server is hung, he will re elect a main server from the slave.
3. cluster:In order to solve the problem of limited capacity of a single redis, data is allocated to multiple machines according to certain rules. Memory / QPS is not limited to a single machine, and it can benefit from the high scalability of distributed clusters.