防止Redis網絡閃斷進行全量同步
當網絡抖動時Redis 會發生主從全量同步,如果數據量大的話,加上傳輸時間,Reload時間會讓業務長時間異常,可以從以下參數上進行調整,有效防止網絡閃斷帶來的風險。
1.repl-timeout
redis里面的repl-timeout參數值太小也將會導致復制不成功.
redis配置文件中對repl-timeout的參數解釋如下:
# The following option sets the replication timeout for:
#
# 1) Bulk transfer I/O during SYNC, from the point of view of slave.
# 2) Master timeout from the point of view of slaves (data, pings).
# 3) Slave timeout from the point of view of masters (REPLCONF ACK pings).
三種情況認為復制超時:
1)slave角度,如果在repl-timeout時間內沒有收到master SYNC傳輸的rdb snapshot數據,
2)slave角度,在repl-timeout沒有收到master發送的數據包或者ping。
3)master角度,在repl-timeout時間沒有收到REPCONF ACK確認信息。
當redis檢測到repl-timeout超時(默認值60s),將會關閉主從之間的連接,redis slave發起重新建立主從連接的請求。
對於內存數據集比較大的系統,可以增大repl-timeout參數。
2.slave ping period
redis slave會定期從master發送ping命令,時間間隔repl-ping-slave-period指定。
因而,設置參數時, repl-timeout > repl-ping-slave-period。
# Slaves send PINGs to server in a predefined interval. The default value is 10 seconds.
# repl-ping-slave-period 10
# It is important to make sure that this value is greater than the values pecified for repl-ping-slave-period otherwise a timeout will be detected every time there is low traffic between the master and the slave.
3.repl-backlog-size
當主服務器進行命令傳播的時候,maser不僅將所有的數據更新命令發送到所有slave的replication buffer,還會寫入replication backlog。當斷開的slave重新連接上master的時候,slave將會發送psync命令(包含復制的偏移量offset),請求partial resync。如果請求的offset不存在,那么執行全量的sync操作,相當於重新建立主從復制。
為了避免網絡不穩定造成的全量同步.
修改參數如下:
config set repl-timeout 240
config set repl-backlog-size 524288000