您好,登錄后才能下訂單哦!
Redis 主從復(fù)制
步驟 | 詳細(xì)描述 |
---|---|
保存主節(jié)點(diǎn)信息 | 執(zhí)行slaveof后從節(jié)點(diǎn)只保存主節(jié)點(diǎn)的地址信息便直接返回 |
主從建立socket連接 | 從節(jié)點(diǎn)(slave)內(nèi)部通過(guò)每秒運(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套接字,專(zhuān)門(mén)用于接受住節(jié)點(diǎn)發(fā)送的復(fù)制命令;如果從節(jié)點(diǎn)無(wú)法建立連接,定時(shí)任務(wù)會(huì)無(wú)限重試直到連接成功或者執(zhí)行 slaveof no one 取消復(fù)制 |
發(fā)送ping命令 | 連接建立成功后從節(jié)點(diǎn)發(fā)送ping請(qǐng)求進(jìn)行首次通信,ping請(qǐng)求的目的:檢測(cè)主從之間套接字是否可用;檢測(cè)主節(jié)點(diǎn)當(dāng)前是否可接受處理命令.如果發(fā)送ping命令后,從節(jié)點(diǎn)沒(méi)有收到主節(jié)點(diǎn)的pong回復(fù)或者超時(shí),比如網(wǎng)絡(luò)超時(shí)或者主節(jié)點(diǎn)正在阻塞無(wú)法響應(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)相同的密碼才能通過(guò)驗(yàn)證;如果驗(yàn)證失敗復(fù)制將終止,從節(jié)點(diǎn)重新發(fā)起復(fù)制流程 |
同步數(shù)據(jù)集 | 主從復(fù)制連接正常通信后,對(duì)于首次建立復(fù)制的場(chǎng)景,主節(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ù)制的建立流程,接下來(lái)主節(jié)點(diǎn)會(huì)持續(xù)地把寫(xiě)命令發(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ù)同步
類(lèi)型 | 描述 |
---|---|
全量復(fù)制 | 一般用于初次復(fù)制場(chǎng)景,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ò)造成很大的開(kāi)銷(xiāo) |
部分復(fù)制 | 用于處理在主從復(fù)制中因網(wǎng)絡(luò)閃斷等原因造成的數(shù)據(jù)丟失場(chǎng)景,當(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ù)制的過(guò)高開(kāi)銷(xiāo) |
復(fù)制偏移量
參數(shù) | 描述 |
---|---|
master_repl_offset | 參與復(fù)制的主從節(jié)點(diǎn)都會(huì)維護(hù)自身復(fù)制偏移量。主節(jié)點(diǎn)(master)在處理完寫(xiě)入命令后,會(huì)把命令的字節(jié)長(zhǎng)度做累加記錄,統(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 | 開(kāi)啟復(fù)制緩沖區(qū) |
repl_backlog_size:1048576 | 緩沖區(qū)最大長(zhǎng)度 |
repl_backlog_first_byte_offset:1 | 起始偏移量,計(jì)算當(dāng)前緩沖區(qū)可用范圍 |
repl_backlog_histlen:2301 | 已保存數(shù)據(jù)的有效長(zhǎng)度 |
master_replid | 主節(jié)點(diǎn)實(shí)例的master_replid相同 |
master_replid2 | 未發(fā)生切換,即主實(shí)例未發(fā)生過(guò)變化,所以初始值為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 # 開(kāi)始連接主節(jié)點(diǎn) 31771:S 06 Aug 2019 12:21:40.214 * Connecting to MASTER 127.0.0.1:6380 # 開(kāi)始同步 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ù)制
讀寫(xiě)分離
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。