步驟 | 詳細(xì)描述 |
---|---|
保存主節(jié)點(diǎn)信息 | 執(zhí)行slaveof后從節(jié)點(diǎn)只保存主節(jié)點(diǎn)的地址信息便直接返回 |
主從建立socket連接 | 從節(jié)點(diǎn)(slave)內(nèi)部通過每秒運(yùn)行的定時(shí)任務(wù)維護(hù)復(fù)制相關(guān)邏輯,當(dāng)定時(shí)任務(wù)發(fā)現(xiàn)存在新的主節(jié)點(diǎn)后,會(huì)嘗試與該節(jié)點(diǎn)建立網(wǎng)絡(luò)連接;從節(jié)點(diǎn)會(huì)建立一個(gè)socket套接字,專門用于接受住節(jié)點(diǎn)發(fā)送的復(fù)制命令;如果從節(jié)點(diǎn)無法建立連接,定時(shí)任務(wù)會(huì)無限重試直到連接成功或者執(zhí)行 slaveof no one 取消復(fù)制 |
發(fā)送ping命令 | 連接建立成功后從節(jié)點(diǎn)發(fā)送ping請求進(jìn)行首次通信,ping請求的目的:檢測主從之間套接字是否可用;檢測主節(jié)點(diǎn)當(dāng)前是否可接受處理命令.如果發(fā)送ping命令后,從節(jié)點(diǎn)沒有收到主節(jié)點(diǎn)的pong回復(fù)或者超時(shí),比如網(wǎng)絡(luò)超時(shí)或者主節(jié)點(diǎn)正在阻塞無法響應(yīng)命令,從節(jié)點(diǎn)會(huì)端口復(fù)制連接,下次定時(shí)任務(wù)會(huì)發(fā)起重連 |
權(quán)限驗(yàn)證 | 如果主節(jié)點(diǎn)設(shè)置了requirepass 參數(shù),則需要密碼驗(yàn)證,從節(jié)點(diǎn)必須配置masterauth參數(shù)保證與主節(jié)點(diǎn)相同的密碼才能通過驗(yàn)證;如果驗(yàn)證失敗復(fù)制將終止,從節(jié)點(diǎn)重新發(fā)起復(fù)制流程 |
同步數(shù)據(jù)集 | 主從復(fù)制連接正常通信后,對(duì)于首次建立復(fù)制的場景,主節(jié)點(diǎn)會(huì)把持有的數(shù)據(jù)全部發(fā)送給從節(jié)點(diǎn). |
命令持續(xù)復(fù)制 | 當(dāng)主節(jié)點(diǎn)把當(dāng)前的數(shù)據(jù)同步給從節(jié)點(diǎn)后,變成了復(fù)制的建立流程,接下來主節(jié)點(diǎn)會(huì)持續(xù)地把寫命令發(fā)送給從節(jié)點(diǎn),保證主從數(shù)據(jù)一致性 |
127.0.0.1:6381> slaveof 127.0.0.1 6380 Redis5.0.0 改為 : replicaof masterip> masterport>
6381 啟動(dòng)
查看info replication
數(shù)據(jù)同步
類型 | 描述 |
---|---|
全量復(fù)制 | 一般用于初次復(fù)制場景,Redis早期支持的復(fù)制功能只有全量復(fù)制,它會(huì)把主節(jié)點(diǎn)全部數(shù)據(jù)一次性發(fā)送給從節(jié)點(diǎn),當(dāng)數(shù)據(jù)量較大時(shí),會(huì)對(duì)主從節(jié)點(diǎn)和網(wǎng)絡(luò)造成很大的開銷 |
部分復(fù)制 | 用于處理在主從復(fù)制中因網(wǎng)絡(luò)閃斷等原因造成的數(shù)據(jù)丟失場景,當(dāng)從節(jié)點(diǎn)再次連上主節(jié)點(diǎn)后,如果條件允許,主節(jié)點(diǎn)會(huì)補(bǔ)發(fā)丟失數(shù)據(jù)給從節(jié)點(diǎn)。因?yàn)檠a(bǔ)發(fā)的數(shù)據(jù)遠(yuǎn)遠(yuǎn)小于全量數(shù)據(jù),可以有效避免全量復(fù)制的過高開銷 |
復(fù)制偏移量
參數(shù) | 描述 |
---|---|
master_repl_offset | 參與復(fù)制的主從節(jié)點(diǎn)都會(huì)維護(hù)自身復(fù)制偏移量。主節(jié)點(diǎn)(master)在處理完寫入命令后,會(huì)把命令的字節(jié)長度做累加記錄,統(tǒng)計(jì)信息在info replication中的master_repl_offset指標(biāo)中 |
slave0 | 從節(jié)點(diǎn)(slave) 每秒鐘上報(bào)自身的復(fù)制偏移量給主節(jié)點(diǎn),因此主節(jié)點(diǎn)也會(huì)保存從節(jié)點(diǎn)的復(fù)制偏移量 |
slave_repl_offset | 從節(jié)點(diǎn)在接收到主節(jié)點(diǎn)發(fā)送的命令后,也會(huì)累加記錄自身的偏移量。 |
復(fù)制積壓緩沖區(qū)
參數(shù) | 描述 |
---|---|
repl_backlog_active:1 | 開啟復(fù)制緩沖區(qū) |
repl_backlog_size:1048576 | 緩沖區(qū)最大長度 |
repl_backlog_first_byte_offset:1 | 起始偏移量,計(jì)算當(dāng)前緩沖區(qū)可用范圍 |
repl_backlog_histlen:2301 | 已保存數(shù)據(jù)的有效長度 |
master_replid | 主節(jié)點(diǎn)實(shí)例的master_replid相同 |
master_replid2 | 未發(fā)生切換,即主實(shí)例未發(fā)生過變化,所以初始值為0 |
psync 命令
從節(jié)點(diǎn)使用psync命令完成部分復(fù)制和全量復(fù)制功能
30227:M 05 Aug 2019 18:52:44.698 * Replica 127.0.0.1:6381 asks for synchronization 30227:M 05 Aug 2019 18:52:44.698 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for 'e7d71fb600183a175afadbd1354e97edddb2541a', my replication IDs are 'e24f6e42917e7c162ec45a713b0ee3872005ee8b' and '0000000000000000000000000000000000000000')
6381 從節(jié)點(diǎn)打印分析
31771:S 06 Aug 2019 12:21:40.213 * DB loaded from disk: 0.000 seconds 31771:S 06 Aug 2019 12:21:40.213 * Before turning into a replica, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer. #啟動(dòng)成功 31771:S 06 Aug 2019 12:21:40.213 * Ready to accept connections # 開始連接主節(jié)點(diǎn) 31771:S 06 Aug 2019 12:21:40.214 * Connecting to MASTER 127.0.0.1:6380 # 開始同步 31771:S 06 Aug 2019 12:21:40.214 * MASTER -> REPLICA sync started 31771:S 06 Aug 2019 12:21:40.214 * Non blocking connect for SYNC fired the event. 31771:S 06 Aug 2019 12:21:40.214 * Master replied to PING, replication can continue... # 嘗試增量同步 31771:S 06 Aug 2019 12:21:40.214 * Trying a partial resynchronization (request 668b25f85e84c5900e1032e4b5e1f038f01cfa49:5895). # 全量同步 31771:S 06 Aug 2019 12:21:40.215 * Full resync from master: c88cd043d66193e867929d9d5fadc952954371e5:0 31771:S 06 Aug 2019 12:21:40.215 * Discarding previously cached master state. 31771:S 06 Aug 2019 12:21:40.240 * MASTER -> REPLICA sync: receiving 224 bytes from master 31771:S 06 Aug 2019 12:21:40.241 * MASTER -> REPLICA sync: Flushing old data 31771:S 06 Aug 2019 12:21:40.241 * MASTER -> REPLICA sync: Loading DB in memory 31771:S 06 Aug 2019 12:21:40.241 * MASTER -> REPLICA sync: Finished with success
全量復(fù)制
31651:M 06 Aug 2019 11:08:40.802 * Starting BGSAVE for SYNC with target: disk 31651:M 06 Aug 2019 11:08:40.802 * Background saving started by pid 31676 31676:C 06 Aug 2019 11:08:40.805 * DB saved on disk 31676:C 06 Aug 2019 11:08:40.806 * RDB: 0 MB of memory used by copy-on-write 31651:M 06 Aug 2019 11:08:40.886 * Background saving terminated with success 31651:M 06 Aug 2019 11:08:40.886 * Synchronization with replica 127.0.0.1:6381 succeeded
31645:S 06 Aug 2019 11:08:40.886 * MASTER -> REPLICA sync: receiving 224 bytes from master
client-output-buffer-limit replica 256mb 64mb 60
31651:M 06 Aug 2019 11:08:40.886 * Synchronization with replica 127.0.0.1:6381 succeeded
31645:S 06 Aug 2019 11:08:40.886 * MASTER -> REPLICA sync: Flushing old data
31645:S 06 Aug 2019 11:08:40.886 * MASTER -> REPLICA sync: Loading DB in memory 31645:S 06 Aug 2019 11:08:40.886 * MASTER -> REPLICA sync: Finished with success
標(biāo)識(shí) | 含義 |
---|---|
M | 當(dāng)前為主節(jié)點(diǎn)日志 |
S | 當(dāng)前為從節(jié)點(diǎn)日志 |
C | 子進(jìn)程日志 |
部分復(fù)制
31767:M 06 Aug 2019 14:13:26.096 # Connection with replica 127.0.0.1:6381 lost.
從節(jié)點(diǎn)打?。? 31934:S 06 Aug 2019 14:20:54.745 * MASTER -> REPLICA sync started 31934:S 06 Aug 2019 14:20:54.745 * Non blocking connect for SYNC fired the event. 31934:S 06 Aug 2019 14:20:54.745 * Master replied to PING, replication can continue... 31934:S 06 Aug 2019 14:20:54.745 * Trying a partial resynchronization (request c88cd043d66193e867929d9d5fadc952954371e5:9996). 31934:S 06 Aug 2019 14:20:54.746 * Successful partial resynchronization with master. 31934:S 06 Aug 2019 14:20:54.746 * MASTER -> REPLICA sync: Master accepted a Partial Resynchronization. 主節(jié)點(diǎn)打?。? 31767:M 06 Aug 2019 14:21:49.065 * Replica 127.0.0.1:6381 asks for synchronization 31767:M 06 Aug 2019 14:21:49.066 * Partial resynchronization request from 127.0.0.1:6381 accepted. Sending 0 bytes of backlog starting from offset 10066.
31938:S 06 Aug 2019 14:21:49.065 * Trying a partial resynchronization (request c88cd043d66193e867929d9d5fadc952954371e5:10066).
31938:S 06 Aug 2019 14:21:49.066 * Successful partial resynchronization with master. 31938:S 06 Aug 2019 14:21:49.066 * MASTER -> REPLICA sync: Master accepted a Partial Resynchronization.
31767:M 06 Aug 2019 14:21:49.065 * Replica 127.0.0.1:6381 asks for synchronization 31767:M 06 Aug 2019 14:21:49.066 * Partial resynchronization request from 127.0.0.1:6381 accepted. Sending 0 bytes of backlog starting from offset 10066.
心跳
異步復(fù)制
讀寫分離
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
標(biāo)簽:南寧 伊春 河源 泰州 拉薩 畢節(jié) 甘南 定州
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Redis全量復(fù)制與部分復(fù)制示例詳解》,本文關(guān)鍵詞 Redis,全量,復(fù)制,與,部分,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。