偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

圖解Redis,Redis主從復(fù)制與Redis哨兵機制

數(shù)據(jù)庫 Redis
本篇介紹了Redis復(fù)制、Redis復(fù)制的工作流程、Redis哨兵、Redis的實戰(zhàn)演練、Redis哨兵運行流程和選舉原理,

大家好,我是哪吒。

上一篇分享了??離譜!面試為啥都問Redis?趕緊補一下??,完成了Redis的初體驗。

今天分享一下Redis主從復(fù)制與Redis哨兵機制相關(guān)的知識點,實現(xiàn)快速入門,豐富個人簡歷,提高面試level,給自己增加一點談資,秒變面試小達(dá)人,BAT不是夢。

圖片

一、Redis復(fù)制是什么?

Redis復(fù)制就是主從復(fù)制,當(dāng)主服務(wù)器數(shù)據(jù)發(fā)生變化時,自動將新的數(shù)據(jù)同步到從數(shù)據(jù)庫。

讀數(shù)據(jù)庫可以進行讀寫操作,從數(shù)據(jù)庫一般指用于讀操作。

Redis復(fù)制可以保證主數(shù)據(jù)庫崩潰時可以進行數(shù)據(jù)恢復(fù)。

二、Redis復(fù)制能干嘛?

  1. 讀寫分離
  2. 容災(zāi)恢復(fù)
  3. 數(shù)據(jù)備份
  4. 水平擴容支撐高并發(fā)

三、Redis復(fù)制的缺點

1、復(fù)制延遲

由于所有的寫操作都發(fā)生在master數(shù)據(jù)庫,然后同步到slave數(shù)據(jù)庫中,所以會有一定的數(shù)據(jù)延遲,當(dāng)系統(tǒng)負(fù)擔(dān)過重時,延遲越大,slave機器的增加也會增加數(shù)據(jù)延遲的時間。

2、master宕機

如果master宕機了,默認(rèn)情況下不會將salve數(shù)據(jù)庫自動升級為master數(shù)據(jù)庫。

四、樂觀復(fù)制策略

Redis采用樂觀復(fù)制策略,容忍一段時間內(nèi)主從數(shù)據(jù)庫不一致,但保證最終一致性。這個策略保證了性能,在復(fù)制的時候,主數(shù)據(jù)庫不會阻塞,可以繼續(xù)提供服務(wù)。

五、Redis復(fù)制常用命令

1、info replication

查看節(jié)點的主從關(guān)系和配置信息。

2、replicaof 主庫Ip 主庫port

在從數(shù)據(jù)庫的redis.conf中配置。

3、slaveof 主庫IP 主庫port

在運行期間修改slave節(jié)點的信息,如果該數(shù)據(jù)庫已經(jīng)是其它主數(shù)據(jù)庫的從數(shù)據(jù)庫了,那么它會停止與其的主從關(guān)系,轉(zhuǎn)而成為新配置的主庫的從數(shù)據(jù)庫。

4、slaveof no one

使當(dāng)前數(shù)據(jù)庫停止與其它數(shù)據(jù)庫的同步,升級為主數(shù)據(jù)庫。

六、Redis復(fù)制工作流程

1、slave啟動成功后,會連接master數(shù)據(jù)庫,發(fā)送一個sync命令,同步數(shù)據(jù);如果是第一次連接,則會進行一次全量復(fù)制,slave自身的數(shù)據(jù)會被master數(shù)據(jù)覆蓋清除。

2、master數(shù)據(jù)庫收到sync命令后,通過RDB開始保存快照,同時將所有接收到的用于修改數(shù)據(jù)庫的命令緩存起來,master數(shù)據(jù)庫執(zhí)行完RDB持久化后,master將RBD文件和所有緩存的命令發(fā)送到所有的slave數(shù)據(jù)庫,完成一次數(shù)據(jù)同步。

3、slave收到RDB文件和命令緩存后,將其加載到內(nèi)存中,從而完成復(fù)制初始化。

4、repl-ping-replica-period 10,表示master發(fā)出ping包的周期默認(rèn)是10秒。

5、完成首次數(shù)據(jù)全量同步后,master繼續(xù)將新的收集到的修改命令定期傳給slave數(shù)據(jù)庫,完成數(shù)據(jù)同步。

6、如果從機重啟了,master的backlog中會記錄offset,master會將offset后面的數(shù)據(jù)復(fù)制給slave。

7187:C 14 Mar 22:14:24.106 # nzbc Redis is starting nzbc
7187:C 14 Mar 22:14:24.107 # Redis versinotallow=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機制來監(jiān)控Redis的性能,如果主數(shù)據(jù)庫宕機了,根據(jù)投票數(shù)自動將某一個從數(shù)據(jù)庫提升為主數(shù)據(jù)庫,繼續(xù)對外提供服務(wù)。

1、Redis哨兵的作用

圖片

  1. 主從監(jiān)控,監(jiān)控主從數(shù)據(jù)庫是否運行正常;
  2. 消息通知,哨兵可以將故障信息發(fā)送給客戶端;
  3. 故障轉(zhuǎn)移,如果master異常。哨兵會進行主備切換,將其中一個slave轉(zhuǎn)為master;
  4. 配置中心,客戶端通過連接哨兵獲取Redis服務(wù)集群的主節(jié)點信息;

2、使用哨兵的注意事項:

圖片

八、實戰(zhàn)演練

1、搭建3個哨兵服務(wù)器

監(jiān)控Redis主從服務(wù)器,不存放數(shù)據(jù)。

圖片

2、sentinel.conf參數(shù)選項說明

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

圖片

設(shè)置要監(jiān)控的master服務(wù)器,quorum表示至少有幾個哨兵認(rèn)為客觀下線,同意故障轉(zhuǎn)移的法定票數(shù),因此哨兵服務(wù)器一般為奇數(shù)個。

sentinel monitor <master-name> 127.0.0.1 6379 <quorum>。

master服務(wù)器設(shè)置了密碼:

sentinel auth-pass <master-name> <password>。

通過命令,完成哨兵sentinel的啟動,兩種方式,任選其一:

  1. redis-sentinel /path/to/sentinel.conf
  2. redis-server /path/to/sentinel.conf --sentinel

九、哨兵運行流程和選舉原理

當(dāng)一個主從配置中的master失效后,sentinel會選舉出一個新的master用于接替原master的工作,其它slave服務(wù)器自動指向新master,實現(xiàn)數(shù)據(jù)同步。

1、主觀下線

指定多少毫秒之后,主節(jié)點沒有應(yīng)答哨兵,此時哨兵會主觀上認(rèn)為主節(jié)點已經(jīng)下線。

sentinel down-after-millisecnds <master-name> <millisecnds>

2、客觀下線

多個哨兵sentinel進行投票,根據(jù)投票結(jié)果才能確認(rèn)一個master客觀上已經(jīng)宕機。

3、選舉master三步走:

(1)先選出一個master服務(wù)器

圖片

當(dāng)master數(shù)據(jù)庫宕機后,各個哨兵sentinel節(jié)點會進行協(xié)商,先通過Raft算法選舉出一個領(lǐng)導(dǎo)者哨兵節(jié)點,再由領(lǐng)導(dǎo)者進行master的選舉。

  1. 根據(jù)Redis.conf中,優(yōu)先級slave-priority或者replica-priority最高的從節(jié)點(數(shù)字越小優(yōu)先級越高)。
  2. 復(fù)制偏移位置offset最大的從節(jié)點。
  3. 最小Run ID的從節(jié)點。

(2)其它slave服務(wù)器連接到master服務(wù)器

執(zhí)行slaveof no one命令會選舉出新的master,并通過slaveof命令將其它從節(jié)點成為新master服務(wù)器的從節(jié)點。

(3)如果舊master服務(wù)器恢復(fù)正常了,也要成為新master服務(wù)器的slave從服務(wù)器。

十、總結(jié)

介紹了Redis復(fù)制、Redis復(fù)制的工作流程、Redis哨兵、Redis的實戰(zhàn)演練、Redis哨兵運行流程和選舉原理,文章通俗易懂,值得回味,哈哈。

本文轉(zhuǎn)載自微信公眾號「哪吒編程」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系哪吒編程公眾號。

責(zé)任編輯:姜華 來源: 哪吒編程
相關(guān)推薦

2023-09-24 14:32:15

2023-12-25 08:02:09

2024-07-16 08:38:06

2012-07-20 09:11:51

2020-09-04 06:35:28

Redis復(fù)制哨兵

2024-12-09 00:00:09

2018-07-06 09:58:38

Redis高可用主從復(fù)制

2023-11-12 00:10:07

Redis高可用

2019-12-05 10:00:03

架構(gòu)Redis服務(wù)器

2025-03-19 10:00:56

2021-01-12 08:03:19

Redis數(shù)據(jù)系統(tǒng)

2014-07-04 10:41:19

redis數(shù)據(jù)庫緩存

2020-04-14 21:12:42

Redis集群Linux

2023-10-26 07:47:53

Redis哨兵集群

2022-06-08 16:55:56

服務(wù)器Redis架構(gòu)

2022-02-11 08:41:19

WindowsRedis集群

2019-09-03 15:45:31

Redis分片集群

2020-04-03 14:05:10

面試RedisJava

2023-11-07 12:56:35

Redis主從復(fù)制

2021-01-11 14:45:35

Redis數(shù)據(jù)庫命令
點贊
收藏

51CTO技術(shù)棧公眾號