大家好,我是哪吒。
上一篇分享了離譜!面試為啥都問Redis?趕緊補一下,完成了Redis的初體驗。
今天分享一下圖解Redis,Redis主從複制與Redis哨兵機制相關的知識點,實作快速入門,豐富個人履歷,提高面試level,給自己增加一點談資,秒變面試小達人,BAT不是夢。
一、Redis複制是什麼?
Redis複制就是主從複制,當主伺服器資料發生變化時,自動将新的資料同步到從資料庫。
讀資料庫可以進行讀寫操作,從資料庫一般指用于讀操作。
Redis複制可以保證主資料庫崩潰時可以進行資料恢複。
二、Redis複制能幹嘛?
- 讀寫分離
- 容災恢複
- 資料備份
- 水準擴容支撐高并發
三、Redis複制的缺點
1、複制延遲
由于所有的寫操作都發生在master資料庫,然後同步到slave資料庫中,是以會有一定的資料延遲,當系統負擔過重時,延遲越大,slave機器的增加也會增加資料延遲的時間。
2、master當機
如果master當機了,預設情況下不會将salve資料庫自動更新為master資料庫。
四、樂觀複制政策
Redis采用樂觀複制政策,容忍一段時間内主從資料庫不一緻,但保證最終一緻性。這個政策保證了性能,在複制的時候,主資料庫不會阻塞,可以繼續提供服務。
五、Redis複制常用指令
1、info replication
檢視節點的主從關系和配置資訊。
2、replicaof 主庫Ip 主庫port
在從資料庫的redis.conf中配置。
3、slaveof 主庫IP 主庫port
在運作期間修改slave節點的資訊,如果該資料庫已經是其它主資料庫的從資料庫了,那麼它會停止與其的主從關系,轉而成為新配置的主庫的從資料庫。
4、slaveof no one
使目前資料庫停止與其它資料庫的同步,更新為主資料庫。
六、Redis複制工作流程
1、slave啟動成功後,會連接配接master資料庫,發送一個sync指令,同步資料;如果是第一次連接配接,則會進行一次全量複制,slave自身的資料會被master資料覆寫清除;
2、master資料庫收到sync指令後,通過RDB開始儲存快照,同時将所有接收到的用于修改資料庫的指令緩存起來,master資料庫執行完RDB持久化後,master将RBD檔案和所有緩存的指令發送到所有的slave資料庫,完成一次資料同步;
3、slave收到RDB檔案和指令緩存後,将其加載到記憶體中,進而完成複制初始化;
4、repl-ping-replica-period 10,表示master發出ping包的周期預設是10秒;
5、完成首次資料全量同步後,master繼續将新的收集到的修改指令定期傳給slave資料庫,完成資料同步;
6、如果從機重新開機了,master的backlog中會記錄offset,master會将offset後面的資料複制給slave。
7187:C 14 Mar 22:14:24.106 # nzbc Redis is starting nzbc
7187:C 14 Mar 22:14:24.107 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=7187, just started
7187:C 14 Mar 22:14:24.108 # Configuration loaded
7188:S 14 Mar 22:14:24.110 * Increased maximum number of open files to 10032 (it was originally set to 256).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6380
| `-._ `._ / _.-' | PID: 7188
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
7188:S 14 Mar 22:14:24.120 # Server initialized
7188:S 14 Mar 22:14:24.114 * DB loaded from disk: 0.000 seconds
7188:S 14 Mar 22:14:24.122 * Before turning into a slave, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
7188:S 14 Mar 22:14:24.122 * Ready to accept connections
7188:S 14 Mar 22:14:24.123 * Connecting to MASTER 127.0.0.1:6379
7188:S 14 Mar 22:14:24.123 * MASTER <-> SLAVE sync started
7188:S 14 Mar 22:14:24.123 * Non blocking connect for SYNC fired the event.
7188:S 14 Mar 22:14:24.124 * Master replied to PING, replication can continue...
7188:S 14 Mar 22:14:24.124 * Trying a partial resynchronization (request 9b3cs5w9g6x3004fa9a0999361035b71ecf70ab4:30783).
7188:S 14 Mar 22:14:24.130 * Full resync from master: cb4as85df693ad62f09ce4f486e0d43ec8f36334:0
7188:S 14 Mar 22:14:24.130 * Discarding previously cached master state.
7188:S 14 Mar 22:14:24.163 * MASTER <-> SLAVE sync: receiving 5484 bytes from master
7188:S 14 Mar 22:14:24.165 * MASTER <-> SLAVE sync: Flushing old data
7188:S 14 Mar 22:14:24.165 * MASTER <-> SLAVE sync: Loading DB in memory
7188:S 14 Mar 22:14:24.167 * MASTER <-> SLAVE sync: Finished with success
七、Redis哨兵是什麼?
Redis提供了哨兵sentinel機制來監控Redis的性能,如果主資料庫當機了,根據投票數自動将某一個從資料庫提升為主資料庫,繼續對外提供服務。
1、Redis哨兵的作用
- 主從監控,監控主從資料庫是否運作正常;
- 消息通知,哨兵可以将故障資訊發送給用戶端;
- 故障轉移,如果master異常。哨兵會進行主備切換,将其中一個slave轉為master;
- 配置中心,用戶端通過連接配接哨兵擷取Redis服務叢集的主節點資訊;
2、使用哨兵的注意事項:
八、實戰演練
1、搭建3個哨兵伺服器
監控Redis主從伺服器,不存放資料。
2、sentinel.conf參數選項說明
bind 0.0.0.0
daemonize yes
protected-mode no
port 6391
logfile "/myredis/sentinel1.log"
pidfile /var/run/redis-sentinel6391.pid
dir /myredis
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster 123456
設定要監控的master伺服器,quorum表示至少有幾個哨兵認為客觀下線,同意故障轉移的法定票數,是以哨兵伺服器一般為奇數個。
sentinel monitor <master-name> 127.0.0.1 6379 <quorum>。
master伺服器設定了密碼:
sentinel auth-pass <master-name> <password>。
通過指令,完成哨兵sentinel的啟動,兩種方式,任選其一:
- redis-sentinel /path/to/sentinel.conf
- redis-server /path/to/sentinel.conf --sentinel
九、哨兵運作流程和選舉原理
當一個主從配置中的master失效後,sentinel會選舉出一個新的master用于接替原master的工作,其它slave伺服器自動指向新master,實作資料同步。
1、主觀下線
指定多少毫秒之後,主節點沒有應答哨兵,此時哨兵會主觀上認為主節點已經下線。
sentinel down-after-millisecnds <master-name> <millisecnds>
2、客觀下線
多個哨兵sentinel進行投票,根據投票結果才能确認一個master客觀上已經當機。
3、選舉master三步走:
(1)先選出一個master伺服器
當master資料庫當機後,各個哨兵sentinel節點會進行協商,先通過Raft算法選舉出一個上司者哨兵節點,再由上司者進行master的選舉。
- 根據Redis.conf中,優先級slave-priority或者replica-priority最高的從節點(數字越小優先級越高)。
- 複制偏移位置offset最大的從節點;
- 最小Run ID的從節點
(2)其它slave伺服器連接配接到master伺服器
執行slaveof no one指令會選舉出新的master,并通過slaveof指令将其它從節點成為新master伺服器的從節點。
(3)如果舊master伺服器恢複正常了,也要成為新master伺服器的slave從伺服器。
十、總結
介紹了Redis複制、Redis複制的工作流程、Redis哨兵、Redis的實戰演練、Redis哨兵運作流程和選舉原理,文章通俗易懂,值得回味,哈哈
下一篇繼續,我是哪吒,我們下期見~