Redis master-slave replication and read-write separation

1 core principle

When starting one slave node, it will send a PSYNCcommand to master node.

If this is a slave nodereconnection master node, it master nodewill only be 复制给slave部分缺少的数据; otherwise, if it is slave nodethe first connection to the master node, it will be triggered once full resynchronization.

At the beginning full resynchronization, one ,masterwill be started 后台线程, a copy RDB快照文件will be generated , and all received from the client will be also included 写命令缓存在内存中. After the RDB file is generated, masterthe RDB 发送给slavewill be written to by the slave first 本地磁盘, and then from 本地磁盘加载到内存中. Then the master will send the cached in memory 写命令to the slave, and the slave will also synchronize the data.

If the slave node has a network failure with the master node and disconnects, it will automatically reconnect. If the master finds that there are multiple slave nodes to reconnect, it will only start an rdb save operation to serve all slave nodes with a copy of the data.

2 Resume uploading

Since redis 2.8, master-slave replication is supported 断点续传. If the network connection is broken during the master-slave replication process, then you can 接着上次复制的地方continue to copy instead of copying from the beginning.

The master node will be a common one in the memory backlog, the master and slave will save one replica offsetand one master id, and the offset is stored in the backlog. If the network connection between the master and the slave is broken, the slave will let the master follow 上次的replica offset开始继续复制.

But if the corresponding offset is not found, it will be executed once resynchronization.

3 Diskless copy

The master directly creates the RDB in the memory and sends it to the slave instead of landing the disk locally.

repl-diskless-sync copy

repl-diskless-sync-delay, Wait for a certain length of time to start copying, because you have to wait for more slaves to reconnect.

4 Read and write separation

In order to meet the business scenario with more reads and less writes, and to maximize user cost savings, Redis can use read-write separation to provide users with transparent, highly available, high-performance, and highly flexible read-write separation services.

5 Master-slave replication

5.1 Resource Information

server IP address
master 192.168.51.4
slave1 192.168.51.5
slave2 192.168.51.6

5.2 Slave machine configuration

All three machines are configured to install redis. For specific installation information, please check the blog post, Redis installation and configuration, and common commands .

The configuration on the two slave machines is the same, and the master machine does not require additional configuration.

5.2.1 Modify the configuration file

[root@localhost init.d]# cd /usr/local/redis
[root@localhost redis]# vi 6379.conf 
# replicaof <masterip> <masterport>
replicaof 192.168.51.4 6379

# masterauth <master-password>
masterauth auskat

# administrative / dangerous commands.
replica-read-only yes
  • replicaof 192.168.51.4 6379 Configure master's IP and port number
  • masterauth auskat configure the password of the master
  • replica-read-only yes The default slave node is in read-only mode

5.2.2 Restart service

[root@localhost redis]# /etc/init.d/redis_init_script stop
[root@localhost redis]# /etc/init.d/redis_init_script start

5.3 Function test

5.3.1 View master

[root@localhost ~]# redis-cli -a auskat
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.51.5,port=6379,state=online,offset=1774,lag=1
slave1:ip=192.168.51.6,port=6379,state=online,offset=1774,lag=1
master_replid:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1774
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1774
127.0.0.1:6379> set aaa aaa

5.3.2 View slave1

[root@localhost redis]# redis-cli 
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.4
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:42
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:42
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:42
127.0.0.1:6379> get aaa
"aaa"
127.0.0.1:6379> set abc 123
(error) READONLY You can't write against a read only replica.

5.3.3 View slave2

[root@localhost redis]# redis-cli 
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.4
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:1760
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1760
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1663
repl_backlog_histlen:98

127.0.0.1:6379> get aaa
"aaa"
127.0.0.1:6379> set abc 123
(error) READONLY You can't write against a read only replica.

6 Related information

  • The blog post is not easy, everyone who has worked so hard to pay attention and praise, thank you

Guess you like

Origin blog.csdn.net/qq_15769939/article/details/113822695