Home > Database > Redis > body text

Let's talk about the master-slave replication architecture in Redis6 and see what its characteristics are!

青灯夜游
Release: 2021-12-14 10:03:20
forward
1807 people have browsed it

This article will take you to understand the master-slave replication architecture in Redis6, and introduce the characteristics of Redis6 master-slave replication. I hope it will be helpful to everyone!

Let's talk about the master-slave replication architecture in Redis6 and see what its characteristics are!

Introduction to master-slave replication

Master-slave replication refers to copying data from one Redis server to another Redis server. The former is the master node, and the latter becomes the slave node; 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 (or no slave nodes), but a slave node can only have one master node. [Related recommendations: Redis Video Tutorial]

The benefits of using master-slave replication: reading and writing are separated, which can expand the reading capacity of the master node and share the pressure on the master node. For disaster recovery, once the master node goes down, the slave node can be used as the backup of the master node and can be installed at any time.

Architecture Introduction

The slave node copies the data of the master node. After copying, we can do a read-write separation. If it is a single node, application requests are concentrated on the master node, but with the slave node, it can bear part of the read pressure. The master node can perform read and write operations, while the slave node can only perform read operations. This will share the pressure on the master node.

Redis master-slave replication, one master and two slaves architecture environment preparation

After talking about so many concepts, let’s start deploying the master-slave of Redis Let’s copy the architecture. This time we deploy a one-master-two-slave architecture.

#创建文件
mkdir -p /data/redis/master/data
mkdir -p /data/redis/slave1/data
mkdir -p /data/redis/slave2/data

#从节点开启只读模式(默认)
replica-read-only yes

#从节点访问主节点的密码,和requirepass⼀样
masterauth 123456
    
#哪个主节点进⾏复制
replicaof 8.129.113.233 6379
Copy after login

First create a master node, touch a redis.conf file in the data/redis/master/data directory, edit the redis.conf file

bind 0.0.0.0
port 6379
daemonize yes
requirepass "123456"
logfile "/usr/local/redis/log/redis1.log"
dbfilename "xdclass1.rdb"
dir "/usr/local/redis/data"
appendonly yes
appendfilename "appendonly1.aof"
masterauth "123456"
Copy after login

Then create slave node 1, in data Build redis.conf

bind 0.0.0.0
port 6380
daemonize yes
requirepass "123456"
logfile "/usr/local/redis/log/redis2.log"
dbfilename "xdclass2.rdb"
dir "/usr/local/redis/data"
appendonly yes
appendfilename "appendonly2.aof"
replicaof 8.129.113.233 6379
masterauth "123456"
Copy after login

in the /redis/slave1/data directory. Create slave node 2 and build redis.conf

bind 0.0.0.0
port 6381
daemonize yes
requirepass "123456"
logfile "/usr/local/redis/log/redis3.log"
dbfilename "xdclass3.rdb"
dir "/usr/local/redis/data"
appendonly yes
appendfilename "appendonly3.aof"
replicaof 8.129.113.233 6379
masterauth "123456"
Copy after login

in the data/redis/slave2/data directory. Note: Remember to turn off the firewall. , Alibaba Cloud Server remembers to open the network security group.

After creation, start the configured node

Startup method:

#启动主
./redis-server/data/redis/master/data/redis.conf
#启动从1
./redis-server/data/redis/slave1/data/redis.conf
#启动从2
./redis-server/data/redis/slave2/data/redis.conf
Copy after login

Use info replication to view the status of the current node

Master-slave Replication and read-write verification

1.在主节点创建一个key
set name jack
2.在两个从节点测试是否能拿到主节点的数据
get name
3.在从节点set key是失败的,因为从节点只支持读操作
Copy after login

Redis6 master-slave architecture-analysis of replication read-write separation principle

Master-slave replication is divided into two types: one One is to perform full synchronization when the master and slave first connect; the other is to perform incremental synchronization after full synchronization is completed.

Full copy: The master server will start a background process to generate an rdb file from Redis data. The master server will cache all received write commands from the client. When the process is saved in the background, it will The rdb file is passed to the slave server. At this time, the slave server has the data of the master server. After this, the master server will send the cached commands during this period to the slave server through the redis transmission protocol, and then the slave server will use these commands on its own local in turn, eventually achieving data consistency

Incremental replication: The master node will continue to write commands. When the slave completes initialization and starts working, the process of the master server sending write operations to synchronize them to the server is called incremental replication. Incremental replication means that every time the server executes a write command, it sends the same write command to the slave server, and the slave server accepts and executes the received write command.

What are the characteristics of master-slave replication:

Master-slave replication is non-blocking for the master/slave server, and all data is synchronized During this period, external requests can be processed normally. A master node can contain multiple slave nodes, and each slave node can accept connections from other slave nodes. The slave node will not let the key expire. Instead, after the key of the master node expires and is deleted, it will send a delete command to the slave node to delete it.

Accelerated replication: When the node completes resynchronization, you need to create an RDB file on the disk, and then load this file to send data from the server, but what if the disk speed is relatively low? This will cause data inconsistency between the master node and the slave node. In the new version of Redis, disk-less replication is supported, and RBD files are directly sent to the slave server through the network, without using disks as middleware.

If the master-slave connection is disconnected, replication can be continued from the interrupted point after reconnection without resynchronization. After version 2.8, this new feature of resynchronization uses the PSYNC command, while the old one uses the SYNC command

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

The above is the detailed content of Let's talk about the master-slave replication architecture in Redis6 and see what its characteristics are!. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!