深入剖析Redis系列(三)-Redis叢集模式搭建與原理
前言
在 Redis 3.0
之前,使用 哨兵 ( sentinel
)機制來監控各個節點之間的狀態。 Redis Cluster
是 Redis
的 分散式解決方案 ,在 3.0
版本正式推出,有效地解決了 Redis
在 分散式 方面的需求。當遇到 單機記憶體 、 併發 、 流量 等瓶頸時,可以採用 Cluster
架構方案達到 負載均衡 的目的。

本文將從 叢集方案 、 資料分佈 、 搭建叢集 、 節點通訊 、 叢集伸縮 、 請求路由 、 故障轉移 、 叢集運維 等幾個方面介紹 Redis Cluster
。
正文
1. Redis叢集方案
Redis Cluster
叢集模式通常具有 高可用 、 可擴充套件性 、 分散式 、 容錯 等特性。 Redis
分散式方案一般有兩種:
1.1 客戶端分割槽方案
客戶端 就已經決定資料會被 儲存 到哪個 redis
節點或者從哪個 redis
節點 讀取資料 。其主要思想是採用 雜湊演算法 將 Redis
資料的 key
進行雜湊,通過 hash
函式,特定的 key
會 對映 到特定的 Redis
節點上。

客戶端分割槽方案 的代表為 Redis Sharding
, Redis Sharding
是 Redis Cluster
出來之前,業界普遍使用的 Redis
多例項叢集 方法。 Java
的 Redis
客戶端驅動庫 Jedis
,支援 Redis Sharding
功能,即 ShardedJedis
以及 結合快取池 的 ShardedJedisPool
。
- 優點
不使用 第三方中介軟體 , 分割槽邏輯 可控, 配置 簡單,節點之間無關聯,容易 線性擴充套件 ,靈活性強。
- 缺點
客戶端 無法 動態增刪 服務節點,客戶端需要自行維護 分發邏輯 ,客戶端之間 無連線共享 ,會造成 連線浪費 。
1.2. 代理分割槽方案
客戶端 傳送請求到一個 代理元件 , 代理 解析 客戶端 的資料,並將請求轉發至正確的節點,最後將結果回覆給客戶端。
- 優點 :簡化 客戶端 的分散式邏輯, 客戶端 透明接入,切換成本低,代理的 轉發 和 儲存 分離。
- 缺點 :多了一層 代理層 ,加重了 架構部署複雜度 和 效能損耗 。

代理分割槽 主流實現的有方案有 Twemproxy
和 Codis
。
1.2.1. Twemproxy
Twemproxy
也叫 nutcraker
,是 twitter
開源的一個 redis
和 memcache
的 中間代理伺服器 程式。 Twemproxy
作為 代理 ,可接受來自多個程式的訪問,按照 路由規則 ,轉發給後臺的各個 Redis
伺服器,再原路返回。 Twemproxy
存在 單點故障 問題,需要結合 Lvs
和 Keepalived
做 高可用方案 。

- 優點 :應用範圍廣,穩定性較高,中間代理層 高可用 。
- 缺點 :無法平滑地 水平擴容/縮容 ,無 視覺化管理介面 ,運維不友好,出現故障,不能 自動轉移 。
1.2.2. Codis
Codis
是一個 分散式 Redis
解決方案,對於上層應用來說,連線 Codis-Proxy
和直接連線 原生的 Redis-Server
沒有的區別。 Codis
底層會 處理請求的轉發 ,不停機的進行 資料遷移 等工作。 Codis
採用了無狀態的 代理層 ,對於 客戶端 來說,一切都是透明的。

- 優點
實現了上層 Proxy
和底層 Redis
的 高可用 , 資料分片 和 自動平衡 ,提供 命令列介面 和 RESTful API
,提供 監控 和 管理 介面,可以動態 新增 和 刪除 Redis
節點。
- 缺點
部署架構 和 配置 複雜,不支援 跨機房 和 多租戶 ,不支援 鑑權管理 。
1.3. 查詢路由方案
客戶端隨機地 請求任意一個 Redis
例項,然後由 Redis
將請求 轉發 給 正確 的 Redis
節點。 Redis Cluster
實現了一種 混合形式 的 查詢路由 ,但並不是 直接 將請求從一個 Redis
節點 轉發 到另一個 Redis
節點,而是在 客戶端 的幫助下直接 重定向 ( redirected
)到正確的 Redis
節點。

- 優點
無中心節點 ,資料按照 槽 儲存分佈在多個 Redis
例項上,可以平滑的進行節點 擴容/縮容 ,支援 高可用 和 自動故障轉移 ,運維成本低。
- 缺點
嚴重依賴 Redis-trib
工具,缺乏 監控管理 ,需要依賴 Smart Client
( 維護連線 , 快取路由表 , MultiOp
和 Pipeline
支援)。 Failover
節點的 檢測過慢 ,不如 中心節點 ZooKeeper
及時。 Gossip
訊息具有一定開銷。無法根據統計區分 冷熱資料 。
2. 資料分佈
2.1. 資料分佈理論
分散式資料庫 首先要解決把 整個資料集 按照 分割槽規則 對映到 多個節點 的問題,即把 資料集 劃分到 多個節點 上,每個節點負責 整體資料 的一個 子集 。

資料分佈通常有 雜湊分割槽 和 順序分割槽 兩種方式,對比如下:

由於 Redis Cluster
採用 雜湊分割槽規則 ,這裡重點討論 雜湊分割槽 。常見的 雜湊分割槽 規則有幾種,下面分別介紹:
2.1.1. 節點取餘分割槽
使用特定的資料,如 Redis
的 鍵 或 使用者 ID
,再根據 節點數量 N
使用公式: hash(key)% N
計算出 雜湊值 ,用來決定資料 對映 到哪一個節點上。

- 優點
這種方式的突出優點是 簡單性 ,常用於 資料庫 的 分庫分表規則 。一般採用 預分割槽 的方式,提前根據 資料量 規劃好 分割槽數 ,比如劃分為 512
或 1024
張表,保證可支撐未來一段時間的 資料容量 ,再根據 負載情況 將 表 遷移到其他 資料庫 中。擴容時通常採用 翻倍擴容 ,避免 資料對映 全部被 打亂 ,導致 全量遷移 的情況。
- 缺點
當 節點數量 變化時,如 擴容 或 收縮 節點,資料節點 對映關係 需要重新計算,會導致資料的 重新遷移 。
2.1.2. 一致性雜湊分割槽
一致性雜湊 可以很好的解決 穩定性問題 ,可以將所有的 儲存節點 排列在 收尾相接 的 Hash
環上,每個 key
在計算 Hash
後會 順時針 找到 臨接 的 儲存節點 存放。而當有節點 加入 或 退出 時,僅影響該節點在 Hash
環上 順時針相鄰 的 後續節點 。

- 優點
加入 和 刪除 節點隻影響 雜湊環 中 順時針方向 的 相鄰的節點 ,對其他節點無影響。
- 缺點
加減節點 會造成 雜湊環 中部分資料 無法命中 。當使用 少量節點 時, 節點變化 將大範圍影響 雜湊環 中 資料對映 ,不適合 少量資料節點 的分散式方案。 普通 的 一致性雜湊分割槽 在增減節點時需要 增加一倍 或 減去一半 節點才能保證 資料 和 負載的均衡 。
注意 :因為 一致性雜湊分割槽 的這些缺點,一些分散式系統採用 虛擬槽 對 一致性雜湊 進行改進,比如 Dynamo
系統。
2.1.3. 虛擬槽分割槽
虛擬槽分割槽 巧妙地使用了 雜湊空間 ,使用 分散度良好 的 雜湊函式 把所有資料 對映 到一個 固定範圍 的 整數集合 中,整數定義為 槽 ( slot
)。這個範圍一般 遠遠大於 節點數,比如 Redis Cluster
槽範圍是 0 ~ 16383
。 槽 是叢集內 資料管理 和 遷移 的 基本單位 。採用 大範圍槽 的主要目的是為了方便 資料拆分 和 叢集擴充套件 。每個節點會負責 一定數量的槽 ,如圖所示:

當前叢集有 5
個節點,每個節點平均大約負責 3276
個 槽 。由於採用 高質量 的 雜湊演算法 ,每個槽所對映的資料通常比較 均勻 ,將資料平均劃分到 5
個節點進行 資料分割槽 。 Redis Cluster
就是採用 虛擬槽分割槽 。
- 節點1 : 包含
0
到3276
號雜湊槽。 - 節點2 :包含
3277
到6553
號雜湊槽。 - 節點3 :包含
6554
到9830
號雜湊槽。 - 節點4 :包含
9831
到13107
號雜湊槽。 - 節點5 :包含
13108
到16383
號雜湊槽。
這種結構很容易 新增 或者 刪除 節點。如果 增加 一個節點 6
,就需要從節點 1 ~ 5
獲得部分 槽 分配到節點 6
上。如果想 移除 節點 1
,需要將節點 1
中的 槽 移到節點 2 ~ 5
上,然後將 沒有任何槽 的節點 1
從叢集中 移除 即可。
由於從一個節點將 雜湊槽 移動到另一個節點並不會 停止服務 ,所以無論 新增刪除 或者 改變 某個節點的 雜湊槽的數量 都不會造成 叢集不可用 的狀態.
2.2. Redis的資料分割槽
Redis Cluster
採用 虛擬槽分割槽 ,所有的 鍵 根據 雜湊函式 對映到 0~16383
整數槽內,計算公式: slot = CRC16(key)& 16383
。每個節點負責維護一部分槽以及槽所對映的 鍵值資料 ,如圖所示:

2.2.1. Redis虛擬槽分割槽的特點
- 解耦 資料 和 節點 之間的關係,簡化了節點 擴容 和 收縮 難度。
- 節點自身 維護槽的 對映關係 ,不需要 客戶端 或者 代理服務 維護 槽分割槽元資料 。
- 支援 節點 、 槽 、 鍵 之間的 對映查詢 ,用於 資料路由 、 線上伸縮 等場景。
2.3. Redis叢集的功能限制
Redis
叢集相對 單機 在功能上存在一些限制,需要 開發人員 提前瞭解,在使用時做好規避。
-
key
批量操作 支援有限。
類似 mset
、 mget
操作,目前只支援對具有相同 slot
值的 key
執行 批量操作 。對於 對映為不同 slot
值的 key
由於執行 mget
、 mget
等操作可能存在於多個節點上,因此不被支援。
-
key
事務操作 支援有限。
只支援 多 key
在 同一節點上 的 事務操作 ,當多個 key
分佈在 不同 的節點上時 無法 使用事務功能。
-
key
作為 資料分割槽 的最小粒度
不能將一個 大的鍵值 物件如 hash
、 list
等對映到 不同的節點 。
- 不支援 多資料庫空間
單機 下的 Redis
可以支援 16
個數據庫( db0 ~ db15
), 叢集模式 下只能使用 一個 資料庫空間,即 db0
。
- 複製結構 只支援一層
從節點 只能複製 主節點 ,不支援 巢狀樹狀複製 結構。
3. Redis叢集搭建
Redis-Cluster
是 Redis
官方的一個 高可用 解決方案, Cluster
中的 Redis
共有 2^14(16384)
個 slot
槽 。建立 Cluster
後, 槽 會 平均分配 到每個 Redis
節點上。
下面介紹一下本機啟動 6
個 Redis
的 叢集服務 ,並使用 redis-trib.rb
建立 3主3從 的 叢集 。搭建叢集工作需要以下三個步驟:
3.1. 準備節點
Redis
叢集一般由 多個節點 組成,節點數量至少為 6
個,才能保證組成 完整高可用 的叢集。每個節點需要 開啟配置 cluster-enabled yes
,讓 Redis
執行在 叢集模式 下。
Redis
叢集的節點規劃如下:

注意 :建議為叢集內 所有節點 統一目錄,一般劃分三個目錄: conf
、 data
、 log
,分別存放 配置 、 資料 和 日誌 相關檔案。把 6
個節點配置統一放在 conf
目錄下。
3.1.1. 建立redis各例項目錄
$ sudo mkdir -p /usr/local/redis-cluster $ cd /usr/local/redis-cluster $ sudo mkdir conf data log $ sudo mkdir -p data/redis-6379 data/redis-6389 data/redis-6380 data/redis-6390 data/redis-6381 data/redis-6391
3.1.2. redis配置檔案管理
根據以下 模板 配置各個例項的 redis.conf
,以下只是搭建叢集需要的 基本配置 ,可能需要根據實際情況做修改。
# redis後臺執行 daemonize yes # 繫結的主機埠 bind 127.0.0.1 # 資料存放目錄 dir /usr/local/redis-cluster/data/redis-6379 # 程序檔案 pidfile /var/run/redis-cluster/${自定義}.pid # 日誌檔案 logfile /usr/local/redis-cluster/log/${自定義}.log # 埠號 port 6379 # 開啟叢集模式,把註釋#去掉 cluster-enabled yes # 叢集的配置,配置檔案首次啟動自動生成 cluster-config-file /usr/local/redis-cluster/conf/${自定義}.conf # 請求超時,設定10秒 cluster-node-timeout 10000 # aof日誌開啟,有需要就開啟,它會每次寫操作都記錄一條日誌 appendonly yes
- redis-6379.conf
daemonize yes bind 127.0.0.1 dir /usr/local/redis-cluster/data/redis-6379 pidfile /var/run/redis-cluster/redis-6379.pid logfile /usr/local/redis-cluster/log/redis-6379.log port 6379 cluster-enabled yes cluster-config-file /usr/local/redis-cluster/conf/node-6379.conf cluster-node-timeout 10000 appendonly yes
- redis-6389.conf
daemonize yes bind 127.0.0.1 dir /usr/local/redis-cluster/data/redis-6389 pidfile /var/run/redis-cluster/redis-6389.pid logfile /usr/local/redis-cluster/log/redis-6389.log port 6389 cluster-enabled yes cluster-config-file /usr/local/redis-cluster/conf/node-6389.conf cluster-node-timeout 10000 appendonly yes
- redis-6380.conf
daemonize yes bind 127.0.0.1 dir /usr/local/redis-cluster/data/redis-6380 pidfile /var/run/redis-cluster/redis-6380.pid logfile /usr/local/redis-cluster/log/redis-6380.log port 6380 cluster-enabled yes cluster-config-file /usr/local/redis-cluster/conf/node-6380.conf cluster-node-timeout 10000 appendonly yes
- redis-6390.conf
daemonize yes bind 127.0.0.1 dir /usr/local/redis-cluster/data/redis-6390 pidfile /var/run/redis-cluster/redis-6390.pid logfile /usr/local/redis-cluster/log/redis-6390.log port 6390 cluster-enabled yes cluster-config-file /usr/local/redis-cluster/conf/node-6390.conf cluster-node-timeout 10000 appendonly yes
- redis-6381.conf
daemonize yes bind 127.0.0.1 dir /usr/local/redis-cluster/data/redis-6381 pidfile /var/run/redis-cluster/redis-6381.pid logfile /usr/local/redis-cluster/log/redis-6381.log port 6381 cluster-enabled yes cluster-config-file /usr/local/redis-cluster/conf/node-6381.conf cluster-node-timeout 10000 appendonly yes
- redis-6391.conf
daemonize yes bind 127.0.0.1 dir /usr/local/redis-cluster/data/redis-6391 pidfile /var/run/redis-cluster/redis-6391.pid logfile /usr/local/redis-cluster/log/redis-6391.log port 6391 cluster-enabled yes cluster-config-file /usr/local/redis-cluster/conf/node-6391.conf cluster-node-timeout 10000 appendonly yes
3.2. 環境準備
3.2.1. 安裝Ruby環境
$ sudo brew install ruby
3.2.2. 準備rubygem redis依賴
$ sudo gem install redis Password: Fetching: redis-4.0.2.gem (100%) Successfully installed redis-4.0.2 Parsing documentation for redis-4.0.2 Installing ri documentation for redis-4.0.2 Done installing documentation for redis after 1 seconds 1 gem installed
3.2.3. 拷貝redis-trib.rb到叢集根目錄
redis-trib.rb
是 redis
官方推出的管理 redis
叢集 的工具,整合在 redis
的原始碼 src
目錄下,將基於 redis
提供的 叢集命令 封裝成 簡單 、 便捷 、 實用 的 操作工具 。
$ sudo cp /usr/local/redis-4.0.11/src/redis-trib.rb /usr/local/redis-cluster
檢視 redis-trib.rb
命令環境是否正確,輸出如下:
$ ./redis-trib.rb Usage: redis-trib <command> <options> <arguments ...> createhost1:port1 ... hostN:portN --replicas <arg> checkhost:port infohost:port fixhost:port --timeout <arg> reshardhost:port --from <arg> --to <arg> --slots <arg> --yes --timeout <arg> --pipeline <arg> rebalancehost:port --weight <arg> --auto-weights --use-empty-masters --timeout <arg> --simulate --pipeline <arg> --threshold <arg> add-nodenew_host:new_port existing_host:existing_port --slave --master-id <arg> del-nodehost:port node_id set-timeouthost:port milliseconds callhost:port command arg arg .. arg importhost:port --from <arg> --copy --replace help(show this help) For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
redis-trib.rb
是 redis
作者用 ruby
完成的。 redis-trib.rb
命令列工具 的具體功能如下:
| 命令 | 作用 | |:-------|:-------| | create | 建立叢集 | | check | 檢查叢集 | | info | 檢視叢集資訊 | | fix | 修復叢集 | | reshard | 線上遷移slot | | rebalance | 平衡叢集節點slot數量 | | add-node | 將新節點加入叢集 | | del-node | 從叢集中刪除節點 | | set-timeout | 設定叢集節點間心跳連線的超時時間 | | call | 在叢集全部節點上執行命令 | | import | 將外部redis資料匯入叢集 |
3.3. 安裝叢集
3.3.1. 啟動redis服務節點
執行如下命令啟動 6
臺 redis
節點:
sudo redis-server conf/redis-6379.conf sudo redis-server conf/redis-6389.conf sudo redis-server conf/redis-6380.conf sudo redis-server conf/redis-6390.conf sudo redis-server conf/redis-6381.conf sudo redis-server conf/redis-6391.conf
啟動完成後, redis
以叢集模式啟動,檢視各個 redis
節點的程序狀態:
$ ps -ef | grep redis-server 01908104:59下午 ??0:00.01 redis-server *:6379 [cluster] 01911104:59下午 ??0:00.01 redis-server *:6389 [cluster] 01914104:59下午 ??0:00.01 redis-server *:6380 [cluster] 01917104:59下午 ??0:00.01 redis-server *:6390 [cluster] 01920104:59下午 ??0:00.01 redis-server *:6381 [cluster] 01923104:59下午 ??0:00.01 redis-server *:6391 [cluster]
在每個 redis
節點的 redis.conf
檔案中,我們都配置了 cluster-config-file
的檔案路徑,叢集啟動時, conf
目錄會新生成 叢集 節點配置檔案。檢視檔案列表如下:
$ tree -L 3 . . ├── appendonly.aof ├── conf │├── node-6379.conf │├── node-6380.conf │├── node-6381.conf │├── node-6389.conf │├── node-6390.conf │├── node-6391.conf │├── redis-6379.conf │├── redis-6380.conf │├── redis-6381.conf │├── redis-6389.conf │├── redis-6390.conf │└── redis-6391.conf ├── data │├── redis-6379 │├── redis-6380 │├── redis-6381 │├── redis-6389 │├── redis-6390 │└── redis-6391 ├── log │├── redis-6379.log │├── redis-6380.log │├── redis-6381.log │├── redis-6389.log │├── redis-6390.log │└── redis-6391.log └── redis-trib.rb 9 directories, 20 files
3.3.2. redis-trib關聯叢集節點
按照 從主到從 的方式 從左到右 依次排列 6
個 redis
節點。
$ sudo ./redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6389 127.0.0.1:6390 127.0.0.1:6391
叢集建立後, redis-trib
會先將 16384
個 雜湊槽 分配到 3
個 主節點 ,即 redis-6379
, redis-6380
和 redis-6381
。然後將各個 從節點 指向 主節點 ,進行 資料同步 。
>>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 Adding replica 127.0.0.1:6390 to 127.0.0.1:6379 Adding replica 127.0.0.1:6391 to 127.0.0.1:6380 Adding replica 127.0.0.1:6389 to 127.0.0.1:6381 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: ad4b9ffceba062492ed67ab336657426f55874b7 127.0.0.1:6379 slots:0-5460 (5461 slots) master M: df23c6cad0654ba83f0422e352a81ecee822702e 127.0.0.1:6380 slots:5461-10922 (5462 slots) master M: ab9da92d37125f24fe60f1f33688b4f8644612ee 127.0.0.1:6381 slots:10923-16383 (5461 slots) master S: 25cfa11a2b4666021da5380ff332b80dbda97208 127.0.0.1:6389 replicates ad4b9ffceba062492ed67ab336657426f55874b7 S: 48e0a4b539867e01c66172415d94d748933be173 127.0.0.1:6390 replicates df23c6cad0654ba83f0422e352a81ecee822702e S: d881142a8307f89ba51835734b27cb309a0fe855 127.0.0.1:6391 replicates ab9da92d37125f24fe60f1f33688b4f8644612ee
然後輸入 yes
, redis-trib.rb
開始執行 節點握手 和 槽分配 操作,輸出如下:
Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join.... >>> Performing Cluster Check (using node 127.0.0.1:6379) M: ad4b9ffceba062492ed67ab336657426f55874b7 127.0.0.1:6379 slots:0-5460 (5461 slots) master 1 additional replica(s) M: ab9da92d37125f24fe60f1f33688b4f8644612ee 127.0.0.1:6381 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 48e0a4b539867e01c66172415d94d748933be173 127.0.0.1:6390 slots: (0 slots) slave replicates df23c6cad0654ba83f0422e352a81ecee822702e S: d881142a8307f89ba51835734b27cb309a0fe855 127.0.0.1:6391 slots: (0 slots) slave replicates ab9da92d37125f24fe60f1f33688b4f8644612ee M: df23c6cad0654ba83f0422e352a81ecee822702e 127.0.0.1:6380 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 25cfa11a2b4666021da5380ff332b80dbda97208 127.0.0.1:6389 slots: (0 slots) slave replicates ad4b9ffceba062492ed67ab336657426f55874b7 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
執行 叢集檢查 ,檢查各個 redis
節點佔用的 雜湊槽 ( slot
)的個數以及 slot
覆蓋率 。 16384
個槽位中, 主節點 redis-6379
、 redis-6380
和 redis-6381
分別佔用了 5461
、 5461
和 5462
個槽位。
3.3.3. redis主節點的日誌
可以發現,通過 BGSAVE
命令, 從節點 redis-6389
在 後臺 非同步地從 主節點 redis-6379
同步資料。
$ cat log/redis-6379.log 1907:C 05 Sep 16:59:52.960 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1907:C 05 Sep 16:59:52.961 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=1907, just started 1907:C 05 Sep 16:59:52.961 # Configuration loaded 1908:M 05 Sep 16:59:52.964 * Increased maximum number of open files to 10032 (it was originally set to 256). 1908:M 05 Sep 16:59:52.965 * No cluster configuration found, I'm ad4b9ffceba062492ed67ab336657426f55874b7 1908:M 05 Sep 16:59:52.967 * Running mode=cluster, port=6379. 1908:M 05 Sep 16:59:52.967 # Server initialized 1908:M 05 Sep 16:59:52.967 * Ready to accept connections 1908:M 05 Sep 17:01:17.782 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH 1908:M 05 Sep 17:01:17.812 # IP address for this node updated to 127.0.0.1 1908:M 05 Sep 17:01:22.740 # Cluster state changed: ok 1908:M 05 Sep 17:01:23.681 * Slave 127.0.0.1:6389 asks for synchronization 1908:M 05 Sep 17:01:23.681 * Partial resynchronization not accepted: Replication ID mismatch (Slave asked for '4c5afe96cac51cde56039f96383ea7217ef2af41', my replication IDs are '037b661bf48c80c577d1fa937ba55367a3692921' and '0000000000000000000000000000000000000000') 1908:M 05 Sep 17:01:23.681 * Starting BGSAVE for SYNC with target: disk 1908:M 05 Sep 17:01:23.682 * Background saving started by pid 1952 1952:C 05 Sep 17:01:23.683 * DB saved on disk 1908:M 05 Sep 17:01:23.749 * Background saving terminated with success 1908:M 05 Sep 17:01:23.752 * Synchronization with slave 127.0.0.1:6389 succeeded
3.3.4. redis叢集完整性檢測
使用 redis-trib.rb check
命令檢測之前建立的 兩個叢集 是否成功, check
命令只需要給出叢集中 任意一個節點地址 就可以完成 整個叢集 的 檢查工作 ,命令如下:
$ ./redis-trib.rb check 127.0.0.1:6379
當最後輸出如下資訊,提示叢集 所有的槽 都已分配到節點:
[OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
小結
本文介紹了 Redis
叢集解決方案 , 資料分佈 和 叢集搭建 。叢集方案包括 客戶端分割槽 方案, 代理分割槽 方案 和 查詢路由 方案。 資料分佈 部分簡單地對 節點取餘 分割槽, 一致性雜湊 分割槽以及 虛擬槽 分割槽進行了闡述和對比。最後對使用 Redis-trib
搭建了一個 三主三從 的 虛擬槽 叢集示例。
參考
《Redis 開發與運維》
歡迎關注技術公眾號: 零壹技術棧
ofollow,noindex"> http:// weixin.qq.com/r/VDgkPNH E1YyqrZVf921G (二維碼自動識別)
本帳號將持續分享後端技術乾貨,包括虛擬機器基礎,多執行緒程式設計,高效能框架,非同步、快取和訊息中介軟體,分散式和微服務,架構學習和進階等學習資料和文章。