Redis Master-Slave Copy Interpretation

Table of contents

Basic overview

effect

How to configure master-slave replication

Command configuration (Slaveof) 

Profile configuration

Disadvantages of master-slave replication

Master-slave replication principle

Master-Slave Replication FAQ

Command supplement (info replication)


Basic overview

Master-slave replication refers to copying data from one Redis server to other Redis servers. The former is called the master node (Master/Leader), and the latter is called the slave node (Slave/Follower). The replication of data is one-way! It can only be copied from the master node to the slave node (the master node is the main node for writing, and the slave node is the main node for writing . Read as main)

Data replication is one-way, only from the master node to the slave node.

By default, each Redis server is a master node. A master node can have 0 or more slave nodes, but each slave node can only have one master node.

effect

1. Data backup : Master-slave replication realizes hot backup of data, which is a data redundancy method in addition to persistence.
2. Fault recovery : When a problem occurs on the master node, the slave node can provide services to achieve rapid fault recovery; it is actually a kind of service
redundancy.
3. 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, and when reading Redis data, the application connects to the slave node) 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.
4. Cornerstone of high availability (cluster) : 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.

How to configure master-slave replication

Command configuration (Slaveof) 

The Redis Slaveof command can turn the current server into a slave server of the specified server. If the current server is already a slave server of a master server, executing SLAVEOF host port will cause the current server to stop synchronizing with the old master server, discard the old data set, and start synchronizing with the new master server.

In addition, executing the command SLAVEOF NO ONE on a slave server will cause the slave server to turn off the replication function and transition from the slave server back to the master server. The original synchronized data set will not be discarded.

Taking advantage of the feature "SLAVEOF NO ONE will not discard the synchronized data set", when the master server fails, the slave server can be used as the new master server, thereby achieving uninterrupted operation.

slaveof no one

Stop the current database from synchronizing with other databases and convert it to the main database

redis 127.0.0.1:6379> SLAVEOF 127.0.0.1 6379
OK
 
redis 127.0.0.1:6379> SLAVEOF NO ONE
OK

Return value: Always returns OK.

Executing the slaveof command  in  the redis-cli  client   will only take effect on the current environment and will become invalid after restarting. To take effect permanently,  slaveof  configuration needs to be added to the redis.conf  configuration file  .

Profile configuration

Edit redis-conf configuration file

If you need to start multiple services, you need to configure multiple configuration files. Each configuration file should modify the following information: (port number, pid file name, log file name, rdb file name)

cp redis.conf redis_master79.conf
cp redis.conf redis_sub80.conf 
cp redis.conf redis_sub81.conf 
ls
# redis.conf  redis_master79.conf  redis_sub80.conf  redis_sub81.conf

force redis_master79.conf  

# redis_master79.conf 【做主节点】
port 6379
logfile "6379.log"
pidfile /var/run/redis_6379.pid
dbfilename dump6379.rdb

vi redis_sub80.conf 

# redis_sub80.conf 【做从节点1】
port 6380
logfile "6380.log"
pidfile /var/run/redis_6380.pid
dbfilename dump6380.rdb

vi redis_sub81.conf  

# redis_sub81.conf 【做从节点2】
port 6381
logfile "6381.log"
pidfile /var/run/redis_6381.pid
dbfilename dump6381.rdb

Start redis according to the specified configuration file 

# redis-server /usr/local/bin/kconfig/redis_master79.conf
# redis-server /usr/local/bin/kconfig/redis_sub80.conf
# redis-server /usr/local/bin/kconfig/redis_sub81.conf
# ps -ef |grep redis
polkitd    4770   4750  0 10:27 ?        00:00:00 redis-server *:6379
root       5717      1  0 10:37 ?        00:00:00 redis-server *:6380
root       5724      1  0 10:37 ?        00:00:00 redis-server *:6381
root       5729   4366  0 10:37 pts/0    00:00:00 grep --color=auto redis

Disadvantages of master-slave replication

Copy delay, signal attenuation. Since all write operations are performed on the Master first and then synchronized to the Slave, there is a certain delay in synchronizing from the Master to the Slave machine. When the system is very busy, the delay problem will be more serious and the number of Slave machines increases. It will also make the problem worse.

If the master hangs up, a master will not be automatically reselected, and the remaining slaves cannot execute write commands. Every time the master hangs up, manual intervention is the only option.

Master-slave replication principle

  • When the slave connects to the master server, the slave server sends a data synchronization message to the master service - full replication
  • The main server receives the synchronization message sent from the slave server, persists the main service data into an rbd file, sends the rdb file to the slave server, and the slave server gets the rdb for reading.
  • Each time the master server performs a write operation, it synchronizes data with the slave server (here, the master server takes the initiative) - incremental replication
     

Master-Slave Replication FAQ

1. Can the slave machine execute write commands?

The slave cannot execute write commands

 2. The problem of the slave entry point, that is, should the slave start copying from the beginning or start from the entry point?

Assumptions:

  Master starts and writes to k3

  slave1 starts at the same time as the master, and then writes to k3

  Slave2 starts after writing to k3, then the previous ones will also be copied over.

3. What happens after the host is shut down? Does the slave become the master or is it on standby?

The slave machine does not move and is on standby. The slave machine data can be used normally; wait for the host machine to restart and return. 

4. After the host is shut down, will the master-slave relationship still exist after restarting (configuration file)? Can the slave machine copy successfully?

The master-slave relationship is still there and can be copied smoothly.

Command supplement (info replication)

Replication related information:

parameter name meaning
role The value is "master" if the instance is not a slave of any node, or "slave" if the instance is synchronizing data from a node. Note that a slave node can be the master node of another slave node

master node 

127.0.0.1:6379> info replication
# Replication
# 角色
role:master
# 从节点的连接数
connected_slaves:2
# 从节点详细信息 IP PORT 状态 命令(单位:字节长度)偏移量 延迟秒数
# 主节点每次处理完写操作,会把命令的字节长度累加到master_repl_offset中。
# 从节点在接收到主节点发送的命令后,会累加记录子什么偏移量信息slave_repl_offset,同时,也会每秒钟上报自身的复制偏移量到主节点,以供主节点记录存储。
# 在实际应用中,可以通过对比主从复制偏移量信息来监控主从复制健康状况。
slave0:ip=192.168.10.102,port=6379,state=online,offset=23866,lag=0
slave1:ip=192.168.10.103,port=6379,state=online,offset=23866,lag=0
# master启动时生成的40位16进制的随机字符串,用来标识master节点
master_replid:acc2aaa1f0bb0fd79d7d3302f16bddcbe4add423
master_replid2:0000000000000000000000000000000000000000
# master 命令(单位:字节长度)已写入的偏移量
master_repl_offset:23866
second_repl_offset:-1
# 0/1:关闭/开启复制积压缓冲区标志(2.8+),主要用于增量复制及丢失命令补救
repl_backlog_active:1
# 缓冲区最大长度,默认 1M
repl_backlog_size:1048576
# 缓冲区起始偏移量
repl_backlog_first_byte_offset:1
# 缓冲区已存储的数据长度
repl_backlog_histlen:23866

slave node 

127.0.0.1:6379> info replication
# Replication
# 角色
role:slave
# 主节点详细信息
master_host:192.168.10.101
master_port:6379
# slave端可查看它与master之间同步状态,当复制断开后表示down
master_link_status:up
# 主库多少秒未发送数据到从库
master_last_io_seconds_ago:1
# 从服务器是否在与主服务器进行同步 0否/1是
master_sync_in_progress:0
# slave复制命令(单位:字节长度)偏移量
slave_repl_offset:24076
# 选举时,成为主节点的优先级,数字越大优先级越高,0 永远不会成为主节点
slave_priority:100
# 从库是否设置只读,0读写/1只读
slave_read_only:1
# 连接的slave实例个数
connected_slaves:0
# master启动时生成的40位16进制的随机字符串,用来标识master节点
master_replid:acc2aaa1f0bb0fd79d7d3302f16bddcbe4add423
# slave切换master之后,会生成了自己的master标识,之前的master节点的标识存到了master_replid2的位置
master_replid2:0000000000000000000000000000000000000000
# master 命令(单位:字节长度)已写入的偏移量
master_repl_offset:24076
# 主从切换时记录主节点的命令偏移量+1,为了避免全量复制
second_repl_offset:-1
# 0/1:关闭/开启复制积压缓冲区标志(2.8+),主要用于增量复制及丢失命令补救
repl_backlog_active:1
# 缓冲区最大长度,默认 1M
repl_backlog_size:1048576
# 缓冲区起始偏移量
repl_backlog_first_byte_offset:1
# 缓冲区已存储的数据长度
repl_backlog_histlen:24076

Guess you like

Origin blog.csdn.net/m0_62436868/article/details/132542787