Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction and principle of Redis Master-Slave replication

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "introduction and principle of Redis master-slave replication". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the introduction and principle of Redis master-slave replication".

Overview

In the existing enterprises, 80% of the companies mostly use redis stand-alone services, in the actual scenario, the redis of a single node is prone to risk.

Face problems

1. The machine broke down. We deployed to a Redis server, and in the event of a machine failure, we needed to migrate to another server and ensure that the data was synchronized. And data is the most important, and if you don't care, you basically won't use Redis.

two。 Capacity bottleneck. When we have a need to expand Redis memory, from 16G memory to 64G memory, the stand-alone will certainly not be able to meet. Of course, you can buy a new 128G machine.

Solution.

In order to achieve the larger storage capacity of the distributed database and withstand high concurrent access, we will use the original centralized data

The data of the library is stored on a number of other network nodes.

In order to solve this single node problem, Redis will also deploy multiple copies of data to other nodes for replication to achieve high availability of Redis and redundant backup of data, so as to ensure the high availability of data and services.

Master-slave replication

What is master-slave replication?

Master-slave replication refers to copying the data from one Redis server to another Redis server. The former is called master node (master) and the latter is called slave node (slave). The replication of data is one-way and can only be from master node to slave node.

By default, each Redis server is a master node; and a master node can have multiple slave nodes (or no slave nodes), but a slave node can have only one master node.

The role of master-slave replication

1. Data redundancy: master-slave replication realizes the hot backup of data, which is a way of data redundancy in addition to persistence.

two。 Fault recovery: when there is a problem with the master node, the service can be provided by the slave node to achieve rapid fault recovery; in fact, it is a kind of service redundancy.

3. Load balancing: on the basis of master-slave replication, combined with read-write separation, the master node can provide write services, and the slave node can provide read services (that is, the slave node is connected to the master node when writing Redis data and the slave node is connected when reading Redis data) to share the server load. Especially in the scenario of writing less and reading more, the concurrency of the Redis server can be greatly increased by sharing the read load among multiple slave nodes.

4. Read-write separation: can be used to achieve read-write separation, master library write, slave library read, read-write separation can not only improve the load capacity of the server, but also change the number of slave libraries according to the change of demand.

5. High availability cornerstone: in addition to the above functions, master-slave replication is the basis on which sentinels and clusters can be implemented, so master-slave replication is the basis of high availability for Redis.

Master-slave replication enabled

There are three ways to enable master-slave replication from the slave node:

1. Configuration document: add to the slave server configuration document:

Slaveof

two。 Start command: add after redis-server start command

-- slaveof

3. Client command: after the Redis server starts, execute the command directly through the client:

Slaveof

The Redis instance becomes a slave node.

Through the info replication command, we can see that some of the replicated information master-slave replication principle master-slave replication process can be divided into three stages: connection establishment stage (preparation stage), data synchronization stage, command propagation stage. After the slaveof command is executed from the node, the replication process begins to work. As you can see in the figure below, the replication process is roughly divided into six processes.

This process can also be seen in the log records after master-slave configuration.

1) Save the master node (master) information.

After executing slaveof, Redis prints the following log:

2) the replication-related logic is maintained from within the node (slave) through a scheduled task that runs every second. When the scheduled task finds that there is a new master node, it will try to establish a network connection with that node.

Establish a network connection between the slave node and the master node

The slave node creates a socket socket, and the slave node establishes a socket with port 51234, which is dedicated to receiving replication commands sent by the master node. After a successful connection from the node, print the following log:

If the slave node cannot establish a connection, the scheduled task will retry indefinitely until the connection succeeds or perform slaveof noone cancel replication. About the connection failure, you can view the master_link_down_since_seconds metric by executing info replication at the slave node, which records the system time when the connection failed with the master node. From

When the node fails to connect to the master node, the following log will be printed every second to facilitate the discovery of the problem:

# Error condition on socket for SYNC: {socket_error_reason}

3) send the ping command.

After a successful connection is established, a ping request is sent from the node to communicate for the first time. The main purpose of the ping request is as follows:

Detect the availability of network sockets between master and slave.

Detect whether the primary node can currently accept processing commands.

If the slave node does not receive a pong reply from the master node or times out after sending the ping command, such as the network timeout or the master node is blocking and unable to respond to the command, the slave node will disconnect the replication connection and the next scheduled task will initiate a reconnection

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

4) permission verification. If the requirepass parameter is set for the master node, password authentication is required, and the slave node must be equipped with

Set the masterauth parameter to ensure that the same password as the master node can pass the verification; if the verification fails, the replication will end.

Stop, restart the replication process from the node.

5) synchronize datasets. After the master-slave replication connection communicates normally, the master node will control the scenario where the replication is established for the first time.

Some data are all sent to the slave node, this part of the operation is the longest step.

6) the command continues to copy. When the master node synchronizes the current data to the slave node, the replication establishment process is completed.

Then the master node will continuously send write commands to the slave node to ensure the consistency of the master-slave data.

Thank you for your reading, the above is the content of "the introduction and principle of Redis master-slave replication". After the study of this article, I believe you have a deeper understanding of the introduction and principle of Redis master-slave replication, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report