一年又一年,字節(jié)跳動(dòng) Lark(飛書) 研發(fā)團(tuán)隊(duì)又雙叒叕開始招新生啦土陪!
【內(nèi)推碼】:GTPUVBA
【內(nèi)推鏈接】:https://job.toutiao.com/s/JRupWVj
【招生對(duì)象】:20年9月后~21年8月前 畢業(yè)的同學(xué)
【報(bào)名時(shí)間】:6.16-7.16(提前批簡歷投遞只有一個(gè)月抓住機(jī)會(huì)哦7孔省)
【畫重點(diǎn)】:提前批和正式秋招不矛盾!面試成功,提前鎖定Offer毒坛;若有失利有勾,額外獲得一次面試機(jī)會(huì)疹启,正式秋招開啟后還可再次投遞。
章節(jié)目錄
Redis詳解1.安裝及使用
Redis詳解2.數(shù)據(jù)結(jié)構(gòu)
Redis詳解3.發(fā)布訂閱
Redis詳解4.事務(wù)
Redis詳解5.數(shù)據(jù)持久化
Redis詳解6.主從模式
Redis詳解7.哨兵模式
Redis詳解8.Cluster模式
1 簡介
Redis有三種集群模式:主從模式蔼卡、“哨兵”模式喊崖、Cluster集群模式。本節(jié)我們首先介紹主從模式雇逞。
在需要擴(kuò)展讀請(qǐng)求的時(shí)候荤懂,或者在需要寫入臨時(shí)數(shù)據(jù)的時(shí)候,用戶可以通過設(shè)置額外的Redis從服務(wù)器來保存數(shù)據(jù)集的副本塘砸。在接收到主服務(wù)器發(fā)送的數(shù)據(jù)初始副本之后节仿,客戶端每次向主服務(wù)器進(jìn)行寫入時(shí),從服務(wù)器都會(huì)實(shí)時(shí)地得到更新掉蔬。在部署好主從服務(wù)器之后廊宪,客戶端就可以向任意一個(gè)從服務(wù)器發(fā)送讀請(qǐng)求了,而不必再像之前一樣女轿,總是把每個(gè)讀請(qǐng)求都發(fā)送給主服務(wù)器箭启。
2 Redis復(fù)制的啟動(dòng)過程
image.png
3 主從的配置
單機(jī)配置一主多從
- 主服務(wù)器通過默認(rèn)的redis.conf啟動(dòng)redis-server
- 復(fù)制主服務(wù)器的redis.conf為兩個(gè)新的文件redis_slave1.conf和redis_slave2.conf
- 分別添加如下配置
# 端口
# 或者port 6381
port 6382
# AOF和快照文件文件夾
# dir /usr/local/var/db/redis_slave1/
dir /usr/local/var/db/redis_slave2/
# 從節(jié)點(diǎn)要跟隨的主節(jié)點(diǎn)
slaveof 127.0.0.1 6379
# 如果設(shè)置了密碼,就要設(shè)置
masterauth master-password
- 從服務(wù)器分別通過redis_slave1.conf和redis_slave2.conf啟動(dòng)
單機(jī)一主多從結(jié)果
# 主節(jié)點(diǎn)
32314:M 13 Feb 2019 18:19:34.807 * Replica 127.0.0.1:6381 asks for synchronization
32314:M 13 Feb 2019 18:19:34.807 * Full resync requested by replica 127.0.0.1:6381
32314:M 13 Feb 2019 18:19:34.807 * Starting BGSAVE for SYNC with target: disk
32314:M 13 Feb 2019 18:19:34.807 * Background saving started by pid 33175
33175:C 13 Feb 2019 18:19:34.808 * DB saved on disk
32314:M 13 Feb 2019 18:19:34.838 * Background saving terminated with success
32314:M 13 Feb 2019 18:19:34.839 * Synchronization with replica 127.0.0.1:6381 succeeded
32314:M 13 Feb 2019 18:22:01.275 * Replica 127.0.0.1:6382 asks for synchronization
32314:M 13 Feb 2019 18:22:01.275 * Full resync requested by replica 127.0.0.1:6382
32314:M 13 Feb 2019 18:22:01.275 * Starting BGSAVE for SYNC with target: disk
32314:M 13 Feb 2019 18:22:01.276 * Background saving started by pid 33436
33436:C 13 Feb 2019 18:22:01.277 * DB saved on disk
32314:M 13 Feb 2019 18:22:01.359 * Background saving terminated with success
32314:M 13 Feb 2019 18:22:01.360 * Synchronization with replica 127.0.0.1:6382 succeeded
# 從節(jié)點(diǎn)
33174:S 13 Feb 2019 18:19:34.806 * MASTER <-> REPLICA sync started
33174:S 13 Feb 2019 18:19:34.806 * Non blocking connect for SYNC fired the event.
33174:S 13 Feb 2019 18:19:34.807 * Master replied to PING, replication can continue...
33174:S 13 Feb 2019 18:19:34.807 * Partial resynchronization not possible (no cached master)
33174:S 13 Feb 2019 18:19:34.807 * Full resync from master: deb0cb0abde947bba19c5224a3664e27c90a6b65:0
33174:S 13 Feb 2019 18:19:34.839 * MASTER <-> REPLICA sync: receiving 175 bytes from master
33174:S 13 Feb 2019 18:19:34.839 * MASTER <-> REPLICA sync: Flushing old data
33174:S 13 Feb 2019 18:19:34.839 * MASTER <-> REPLICA sync: Loading DB in memory
33174:S 13 Feb 2019 18:19:34.839 * MASTER <-> REPLICA sync: Finished with success
33174:S 13 Feb 2019 18:19:34.839 * Background append only file rewriting started by pid 33176
33174:S 13 Feb 2019 18:19:34.863 * AOF rewrite child asks to stop sending diffs.
33176:C 13 Feb 2019 18:19:34.863 * Parent agreed to stop sending diffs. Finalizing AOF...
33176:C 13 Feb 2019 18:19:34.863 * Concatenating 0.00 MB of AOF diff received from parent.
33176:C 13 Feb 2019 18:19:34.863 * SYNC append only file rewrite performed
33174:S 13 Feb 2019 18:19:34.909 * Background AOF rewrite terminated with success
33174:S 13 Feb 2019 18:19:34.910 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
33174:S 13 Feb 2019 18:19:34.910 * Background AOF rewrite finished successfully
不同機(jī)器上配置一主多從
如果redis-server在不同的機(jī)器上谈喳,只需要以下兩個(gè)配置即可
# 從節(jié)點(diǎn)要跟隨的主節(jié)點(diǎn)
slaveof 127.0.0.1 6379
# 如果設(shè)置了密碼册烈,就要設(shè)置
masterauth master-password
4 通過命令設(shè)置從服務(wù)器
- 可以通過向運(yùn)行中的從服務(wù)器發(fā)送SLAVEOF命令來將其設(shè)置為從服務(wù)器。
- 如果用戶使用的是 SLAVEOF配置選項(xiàng),那么Redis在啟動(dòng)時(shí)首先會(huì)載入當(dāng)前可用的任何快照文件或者AOF文件赏僧,然后連接主服務(wù)器并執(zhí)行上述的復(fù)制過程大猛。如果用戶使用的是SLAVEOF命令,那么Redis會(huì)立即嘗試連接主服務(wù)器淀零,并在連接成功之后挽绩,開始上述復(fù)制過程。
5 其他細(xì)節(jié)
- 采用異步復(fù)制
- 從服務(wù)器在與主服務(wù)器進(jìn)行初始連接時(shí)驾中,數(shù)據(jù)庫中原有的所有數(shù)據(jù)都將丟失唉堪,并被替換成主服務(wù)器發(fā)來的數(shù)據(jù)。
- Reds不支持主主復(fù)制肩民,被互相設(shè)置為主服務(wù)器的兩個(gè)Redis實(shí)例只會(huì)持續(xù)地占用大量處理器資源并且連續(xù)不斷地嘗試與對(duì)方進(jìn)行通信唠亚,根據(jù)客戶端連接的服務(wù)器的不同,客戶端的請(qǐng)求可能會(huì)得到不一致的數(shù)據(jù)持痰,或者完全得不到數(shù)據(jù)灶搜。
- 當(dāng)多個(gè)從服務(wù)器嘗試連接同一個(gè)主服務(wù)器的時(shí)候,可能出現(xiàn)兩種情況:如果步驟3尚未執(zhí)行工窍,所有從服務(wù)器都會(huì)接收到相同的快照文件和相同的緩沖區(qū)寫命令割卖;如果已經(jīng)正在執(zhí)行或已經(jīng)執(zhí)行,當(dāng)主服務(wù)器與較早進(jìn)行連接的從服務(wù)器執(zhí)行完復(fù)制所需的5個(gè)步驟之后患雏,主服務(wù)器會(huì)與新連接的從服務(wù)器執(zhí)行一次新的步驟1至步驟5鹏溯。
- 從服務(wù)器也可以有自己從服務(wù)器。
- 可以通過
INFO
命令查看配置的詳情淹仑,如下所示:
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=2086,lag=0
slave1:ip=127.0.0.1,port=6382,state=online,offset=2086,lag=1