Let's talk about master-slave replication, sentinels, and clusters in Redis
青灯夜游
Release: 2022-01-12 10:17:13
forward
2211 people have browsed it
This article will take you through the master-slave replication, sentinels, and clusters in Redis. I hope it will be helpful to you!
1. Redis master-slave replication
1. Overview of master-slave replication
Master-slave replication refers to copying data from one server to other Redis servers. The former is called the master node (Master), and the latter is called the slave node (Slave); data replication is one-way, and can only be from the master node to the slave node.
By default, each Redis server is a master node; and a master node can have multiple slave nodes, but a slave node can only have one master node. [Related recommendations: Redis video tutorial]
2. The role of master-slave replication
● data Redundancy: Master-slave replication implements hot backup of data, which is a data redundancy method in addition to persistence. ● Failure recovery: When a problem occurs on the master node, the slave node can provide services to achieve rapid failure recovery; it is actually a kind of service redundancy. ● Load balancing: Based on master-slave replication, combined with read-write separation, the master node can provide write services and the slave nodes can provide read services (that is, when writing Redis data, the application connects to the master node , when reading Redis data, apply the connection slave node) to share the server load; especially in the scenario of less writing and more reading, sharing the read load through multiple slave nodes can greatly increase the concurrency of the Redis server. ● Cornerstone of High Availability: In addition to the above functions, master-slave replication is also the basis for the implementation of sentinels and clusters. Therefore, master-slave replication is the basis of Redis high availability.
3. Master-slave replication process
(1) If you start a Slave machine process, Then it will send a "sync command" command to the Master machine to request a synchronous connection. (2) Whether it is the first connection or reconnection, the Master machine will start a background process to save the data snapshot to the data file (perform RDB operation). At the same time, the Master will also record and cache all commands to modify the data. in the data file. (3) After the background process completes the caching operation, the Master machine will send the data file to the Slave machine. The Slave machine will save the data file to the hard disk and then load it into the memory. Then the Master machine will modify the data file. All operations on the data are sent to the Slave machine at the same time. If the Slave fails and causes downtime, it will automatically reconnect when it returns to normal. (4) After the Master machine receives the connection from the Slave machine, it sends its complete data file to the Slave machine. If the Master receives multiple synchronization requests from the Slave at the same time, the Master will start a synchronization request in the background. process to save the data file and then send it to all slave machines to ensure that all slave machines are normal.
Redis installation details can be found in previous blogs: NoSQL Detailed explanation of redis
传入安装包到/opt目录
yum install -y gcc gcc-c++ make
tar zxvf redis-5.0.7.tar.gz -C /opt/
cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis install
cd /opt/redis-5.0.7/utils
./install_server.sh
......
Please select the redis executable path []
#输入/uar/local/redis/bin/redis-server
ln -s /usr/local/redis/bin/* /usr/local/bin/
Based on master-slave replication, Sentinel introduces automatic failover of the master node.
2. Principle of Sentinel Mode
Sentinel is a distributed system used to monitor each server in the master-slave structure Monitor and select a new Master through a voting mechanism when a failure occurs and connect all Slaves to the new Master. Therefore, the number of the entire cluster running Sentinel must not be less than 3 nodes.
3. The role of sentinel mode
● Monitoring: The sentinel will constantly check whether the master node and slave node It works fine. ● Automatic failover: When the master node fails to work properly, Sentinel will start an automatic failover operation. It will upgrade one of the slave nodes of the failed master node to the new master node, and let the other The slave node is changed to replicate the new master node. ● Notification Reminder: Sentinel can send the failover results to the client.
4. The structure of sentinel mode
The sentinel structure consists of two parts, the sentinel node and the data node: ● Sentinel node: The sentinel system consists of one or more nodes. Sentinel nodes are special redis nodes that do not store data. ● Data node: Both the master node and the slave node are data nodes.
5. Working form of sentry mode
The start of sentinel depends on the master-slave mode, so the master must be After the slave mode is installed, go to sentry mode. Sentinel mode needs to be deployed on all nodes. Sentinel mode will monitor whether all Redis working nodes are normal. When there is a problem with the Master, because other nodes lose contact with the master node, Therefore, a vote will be held. If more than half of the votes are cast, it will be considered that there is indeed a problem with this Master, and then the sentinel room will be notified, and one of the Slaves will be selected as the new Master.
6. Failover mechanism
#The sentinel node regularly monitors whether the master node has failed. Each sentinel node will send a ping command to the master node, slave node and other sentinel nodes every 1 second for heartbeat detection. If the master node does not reply within a certain time range or replies with an error message, then the sentinel will think that the master node is subjectively offline (unilaterally). When more than half of the sentinel nodes believe that the master node is subjectively offline, it will be objectively offline.
When the master node fails, the sentinel nodes will implement the election mechanism through the Raft algorithm (election algorithm) to jointly elect a sentinel node as the leader to be responsible for handling the failover and failure of the master node. notify. Therefore, the number of hosts in the Sentinel cluster must not be less than three nodes.
The leader sentinel node performs failover. The process is as follows: ● Upgrade a slave node to the new master node and let other slave nodes point to the new master node; ● If the original master node recovers, it will also become a slave node and point to the new master node; ● Notify the client that the master node has been replaced. It is important to note that objective offline is a concept that is unique to the master node; if the slave node and sentinel node fail, there will be no subsequent objective offline and failover operations after being subjectively offline by the sentinel.
7. Master node election
Filter out unhealthy ones (offline ones), There is no slave node that replies to the sentinel ping response.
Select the slave node with the highest priority configuration in the configuration file (replica-priority, the default value is 100).
Select the slave node with the largest replication offset, that is, the most complete replication.
The above is the detailed content of Let's talk about master-slave replication, sentinels, and clusters in Redis. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn