redis的主从复制过程
2021/9/19 19:05:18
本文主要是介绍redis的主从复制过程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
redis主从之间的复制分为两部分:
- 全量复制
- 增量复制
全量复制
redis之间在第一次实现主从关系时会进行全量复制,通过观察第一次主从关系的建立时的主从日志文件,主从关系首次建立时复制过程如下:
master 18
1401:M 19 Sep 2021 23:49:47.566 * Replica 10.0.0.82:6379 asks for synchronization 1401:M 19 Sep 2021 23:49:47.566 * Full resync requested by replica 10.0.0.82:6379 收到请求全量同步 1401:M 19 Sep 2021 23:49:47.566 * Starting BGSAVE for SYNC with target: disk 1401:M 19 Sep 2021 23:49:47.567 * Background saving started by pid 1466 后台fork子进程执行bgsave 1466:C 19 Sep 2021 23:49:47.568 * DB saved on disk 1466:C 19 Sep 2021 23:49:47.568 * RDB: 0 MB of memory used by copy-on-write 1401:M 19 Sep 2021 23:49:47.598 * Background saving terminated with success 1401:M 19 Sep 2021 23:49:47.598 * Synchronization with replica 10.0.0.82:6379 succeeded
slave 28
1269:S 19 Sep 2021 23:49:47.469 * Ready to accept connections 1269:S 19 Sep 2021 23:49:47.469 * Connecting to MASTER 10.0.0.81:6379 1269:S 19 Sep 2021 23:49:47.469 * MASTER <-> REPLICA sync started 1269:S 19 Sep 2021 23:49:47.470 * Non blocking connect for SYNC fired the event. 1269:S 19 Sep 2021 23:49:47.470 * Master replied to PING, replication can continue... 主从关系检测 1269:S 19 Sep 2021 23:49:47.472 * Partial resynchronization not possible (no cached master) no部分复制 1269:S 19 Sep 2021 23:49:47.473 * Full resync from master: e0e82f16a47b8955d76937b317bf2f7925c9b856:0 从master全量复制:master_replid 1269:S 19 Sep 2021 23:49:47.504 * MASTER <-> REPLICA sync: receiving 187 bytes from master 1269:S 19 Sep 2021 23:49:47.504 * MASTER <-> REPLICA sync: Flushing old data 1269:S 19 Sep 2021 23:49:47.504 * MASTER <-> REPLICA sync: Loading DB in memory 1269:S 19 Sep 2021 23:49:47.513 * MASTER <-> REPLICA sync: Finished with success
全量复制过程如下:
- slave向master发送psync指令
MASTER <-> REPLICA sync started
- master将数据同步的相关消息(runid、offset)回应给slave
Full resync requested by replica 10.0.0.82:6379
- slave保存同步消息
- master的redis主进程fork出子进程执行bgsave(非阻塞)将内存中数据快照保存为rdb文件;在bgsave的同时将新写入的数据保存到buffer中
- master发送RDB
- master发送buffer
- slave清楚原有旧数据
Flushing old data
- load RDB
Loading DB in memory
- load buffer
master根据slave的runid来判断是否进行全量同步,因为redis每次重启的runid都不相同,且每次redis由master改为slave状态时都会清楚就数据。
在bgsave时,可能由于buffer过小,写入数据将buffer写满后冲刷buffer中的旧数据,容易造成主从数据不同不;从而造成复制失败或者循环的全量复制;所以buffer 需要根据不同环境合理设置其大小;
repl-timeout x 允许从节点的最大断开时间
repl-backlog-size y
其大小设置一般为大于 master的每秒写入量*允许从节点的最大断开时间
增量复制
如果由于网络原因造成原因造成主从断开,期间有数据写入master,再次形成主从时则会形成增量复制。
[root@81 etc]# iptables -A INPUT -s 10.0.0.82 -j REJECT 断开主从 127.0.0.1:6379> set haha hehe 断开期间写入数据到master OK [root@81 etc]# iptables -F 恢复主从
观察master、slave日志如下
master 18
1401:M 20 Sep 2021 00:42:23.762 # Connection with replica 10.0.0.82:6379 lost. 失联 1401:M 20 Sep 2021 00:44:46.245 * Replica 10.0.0.82:6379 asks for synchronization 1401:M 20 Sep 2021 00:44:46.246 * Partial resynchronization request from 10.0.0.82:6379 accepted. Sending 127 bytes of backlog starting from offset 4408. 接受从的增量同步
slave 28
1269:S 20 Sep 2021 00:44:45.139 * MASTER <-> REPLICA sync started 1269:S 20 Sep 2021 00:44:45.139 # Error condition on socket for SYNC: Connection refused 1269:S 20 Sep 2021 00:44:46.154 * Connecting to MASTER 10.0.0.81:6379 1269:S 20 Sep 2021 00:44:46.154 * MASTER <-> REPLICA sync started 1269:S 20 Sep 2021 00:44:46.155 * Non blocking connect for SYNC fired the event. 1269:S 20 Sep 2021 00:44:46.156 * Master replied to PING, replication can continue... 1269:S 20 Sep 2021 00:44:46.158 * Trying a partial resynchronization (request e0e82f16a47b8955d76937b317bf2f7925c9b856:4408). 尝试部分同步(请求 master_replid) 1269:S 20 Sep 2021 00:44:46.159 * Successful partial resynchronization with master. 1269:S 20 Sep 2021 00:44:46.159 * MASTER <-> REPLICA sync: Master accepted a Partial Resynchronization.
增量复制过程如下
- connection lost
- slave connection to master
- master发送同步信息到slave
- 进行部分同步,master传输主从断开期间的积压数据backlog
这篇关于redis的主从复制过程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-24Redis资料:新手入门快速指南
- 2024-12-24Redis资料:新手入门教程与实践指南
- 2024-12-24Redis资料:新手入门教程与实践指南
- 2024-12-07Redis高并发入门详解
- 2024-12-07Redis缓存入门:新手必读指南
- 2024-12-07Redis缓存入门:新手必读教程
- 2024-12-07Redis入门:新手必备的简单教程
- 2024-12-07Redis入门:新手必读的简单教程
- 2024-12-06Redis入门教程:从安装到基本操作
- 2024-12-06Redis缓存入门教程:轻松掌握缓存技巧