Home >
Database >
Redis >
Example analysis of master-slave replication, sentry, and clustering in Redis
Example analysis of master-slave replication, sentry, and clustering in Redis
WBOY
Release: 2023-05-29 13:52:54
forward
848 people have browsed it
1. Redis master-slave replication
1. Overview of master-slave replication
Master-slave replication copies data from one Redis server to other Redis servers process. 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 recommendation: Redis video tutorial]
2. The role of master-slave replication
● Data redundancy: Master-slave replication realizes data Hot backup is a data redundancy method other than 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 a Slave machine process is started, it will send data to the Master machine Send a "sync command" command 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. When a failure occurs A new Master is selected through the voting mechanism and all Slaves are connected 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: Sentinel will constantly check whether the master node and slave node are operating normally. ● 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. 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 Composed of nodes, the sentinel node is a special redis node that does not store data. ● Data node: Both the master node and the slave node are data nodes.
5. How the sentinel mode works
The startup of the sentinel depends on the master-slave mode, so the master-slave mode must be installed Let's do the sentry mode next. 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 have lost contact with the master node, they will vote, and more than half of the votes will be cast. It will be considered that there is indeed a problem with this Master, and then the sentinel room will be notified, and then 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. The sentinel node will send a ping command once every second to perform heartbeat detection and send the results to the master node, slave nodes and other sentinel nodes. 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) and no reply to the sentinel The slave node to which the ping responds.
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.
[root@node1 redis6006]# redis-cli -p 6001 -c
#加-c参数,节点之前就可以互相跳转
127.0.0.1:6001> cluster slots
#查看节点的哈希槽编号范围
1) 1) (integer) 0
#哈希槽起始编号
2) (integer) 5460
#哈希槽终止编号
3) 1) "127.0.0.1"
2) (integer) 6001
#node节点主
3) "18e59f493579facea29abf90ca4050f566d66339"
4) 1) "127.0.0.1"
2) (integer) 6004
#node节点从
3) "2635bf6a0c286ef910ec5da03dbdc7cde308c588"
2) 1) (integer) 10923
2) (integer) 16383
3) 1) "127.0.0.1"
2) (integer) 6003
3) "51460d417eb56537e5bd7e8c9581c66fdd817b3c"
4) 1) "127.0.0.1"
2) (integer) 6006
3) "51a75667dcf21b530e69a3242a3e9f81f577168d"
3) 1) (integer) 5461
2) (integer) 10922
3) 1) "127.0.0.1"
2) (integer) 6002
3) "6381d68c06ddb7ac43c8f7d7b8da0644845dcd59"
4) 1) "127.0.0.1"
2) (integer) 6005
3) "375ad927116d3aa845e95ad5f0586306e7ff3a96"
127.0.0.1:6001> set num 1
OK
127.0.0.1:6001> get num
"1"
127.0.0.1:6001> keys *
1) "num"
127.0.0.1:6001> quit
[root@node1 redis6006]# redis-cli -p 6002 -c
127.0.0.1:6002> keys *
#6002端口无键值对
(empty list or set)
127.0.0.1:6002> get num
-> Redirected to slot [2765] located at 127.0.0.1:6001
"1"
#6002端口获取到num键位于6001端口,切换到6001端口并显示键值
127.0.0.1:6001> set key1 11111
-> Redirected to slot [9189] located at 127.0.0.1:6002
OK
#6001端口创建键值对,将其存至6002端口,并切换至6002端口
127.0.0.1:6002>
Copy after login
The above is the detailed content of Example analysis of master-slave replication, sentry, and clustering 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