Home > Database > Redis > body text

Let's talk about master-slave replication in redis

青灯夜游
Release: 2021-10-08 10:56:36
forward
1697 people have browsed it

This article will talk about the master-slave replication in redis, and introduce the specific usage, precautions and sentinel mode. I hope it will be helpful to everyone!

Let's talk about master-slave replication in redis

1: Overview

Redis master-slave replication is currently the most commonly used read-write separation method, with a master attached to one or more slaves. Slave,

Master is responsible for writing operations. The main problem to solve is to share the pressure of redis reading and improve the efficiency of data reading and writing. [Related recommendations: Redis video tutorial]

Lets talk about master-slave replication in redis

2: Specific use

1. Specify the slave library to connect to the main library:

Host is the connection address, and port is the port number of the connection address. Of course, you can also copy multiple redis.conf files yourself and start multiple redis services with different ports by modifying their port number, thread number and other information.

SLAVEOF [host] [port]

2. The main library is responsible for writing (reading is also possible), and the slave library can only read:

主库中写的数据能在从库中获取:6381为主库,6380为从库
Copy after login

Lets talk about master-slave replication in redis

3. Make the slave library become the master library again:

SLAVEOF no one

Three: Notes:

(1 ) Under normal circumstances, after the master library goes offline, the slave library will wait for the master library to reconnect and still maintain the slave library status

(2) After the slave library goes offline, it needs to reconnect to the master library before it can function as Slave library, otherwise it will be the main library

Four: Sentinel mode

Overview: In order to enable other slave libraries to re-select a main library to continue service operation when the main library service is offline

Function: When the main library service goes offline, the sentinel will monitor that the main library is offline, and conduct a voting operation to make one of the slave libraries become the main library to replace the original main library.

1. Create a sentinel.conf file in the directory where the redis service is started, and edit this configuration file with vim

` sentinel monitor host6379 127.0.0.1 6381 1  `
Copy after login

2. Through redis -sentinel [File Directory]/sentinel.confStart Sentinel

Note: In sentinel mode, when the original main library is reconnected, the sentinel will add its operations to the new main library, that is, as A slave library of the new master library exists.

Five: Principle:

 连接建立-->数据同步-->命令持续传播
 
 在从节点执行 slaveof 命令后,复制过程便开始运作,下面图示大概可以看到,\
Copy after login

It can be seen from the figure that the copying process is roughly divided into 6 processes

Lets talk about master-slave replication in redis

Main This process can also be seen from the log records after configuration

1) Save the master node (master) information.
After executing slaveof, Redis will print the following log:

Lets talk about master-slave replication in redis

2) The slave node (slave) maintains replication-related logic through scheduled tasks that run every second. When the scheduled task After discovering the existence of a new master node, it will try to establish a network connection with the node\

Lets talk about master-slave replication in redis

The slave node will establish a network connection with the master node

The slave node will be established A socket socket. The slave node establishes a socket with port 51234, which is specially used to accept replication commands sent by the master node. After the slave node successfully connects, the following log is printed:

Lets talk about master-slave replication in redis

If the slave node cannot establish a connection, the scheduled task will retry indefinitely until the connection is successful or slaveof no one is executed to cancel the replication. Regarding connection failure, you can execute info replication on the slave node to view the master_link_down_since_seconds indicator, which records the system time when the connection to the master node failed. When the slave node fails to connect to the master node, the following log will be printed every second to facilitate problem discovery:

# Error condition on socket for SYNC: {socket_error_reason}
Copy after login

3) Send the ping command.
After the connection is successfully established, the slave node sends a ping request for the first communication. The main purpose of the ping request is as follows:
·Detect whether the network socket between the master and slave is available.
·Detect whether the master node can currently accept processing commands.
If after sending the ping command, the slave node does not receive the pong reply from the master node or times out, such as the network times out or the master node is blocked and cannot respond to the command, the slave node will disconnect the replication connection, and the next scheduled task will initiate a reconnection. . \

Lets talk about master-slave replication in redis

Lets talk about master-slave replication in redis

The ping command sent from the node returns successfully, Redis prints the following log, and continues the subsequent replication process: \

Lets talk about master-slave replication in redis

4) Permission verification. If the requirepass parameter is set on the master node, password verification is required. The slave node must configure the masterauth parameter to ensure that the password is the same as the master node to pass the verification; if the verification fails, the replication will be terminated and the slave node will reinitiate the replication process.

5) Synchronize data sets. After the master-slave replication connection communicates normally, when replication is established for the first time, the master node will send all the data it holds to the slave node. This part of the operation is the longest step.

6) The command is copied continuously. When the master node synchronizes the current data to the slave node, the replication establishment process is completed. Next, the master node will continuously send write commands to the slave nodes to ensure master-slave data consistency.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of Let's talk about master-slave replication in redis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template