Redis基礎知識(學習筆記5--Redis Cluster)

东山絮柳仔發表於2024-06-16

1.Redis Cluster 是Reids 自己本身提供的Redis 叢集方案。

【此圖來源於 https://www.bilibili.com/video/BV1Gs4y1Q7Ls?p=6&vd_source=0e347fbc6c2b049143afaa5a15abfc1c】

2.Redis 是去中心化的,叢集由多個redis節點組成,每個節點負責整個叢集的一部分資料,每個節點負責的資料多少可能不一樣。節點之間相互連線組成一個對能的叢集,他們呢之間透過一個特殊的二進位制協議交換叢集資訊。

3.Redis Cluster 將所有的資料劃分為16384個槽位,每個節點負責其中一部分槽位。

4.將key對映到hash slot的演算法如下:

HASH_SLOT = CRC16(key) mod 16384

取key進行CRC16計算之後對16384取模運算得到key所在的slot,由於redis cluster在啟動時會對每一臺master節點分配slot空間,那麼當前slot的值在哪臺master節點的slot空間範圍內,key就儲存在哪臺節點。

【圖片來自 《Redis cluster叢集Hash Tag原理分析》 https://www.51cto.com/article/757391.html

5.正常情況下,每個hash slot只會由一個節點提供服務。

當Redis Cluster 的客戶端來連線叢集時,會得到一份叢集的槽位配置資訊。這樣客戶端要查詢某個Key時,可以直接定位到目標節點。(這一點與Codis不同,Codis需要透過Proxy來定位目標節點,Reids Cluster 則直接定位)。

6.資料傾斜的問題

(1) 一種方案時,發現手動遷移;例如一些商品促銷前,檢查下,發現資料集中了,或者嚴重傾斜,可以考慮手動遷移。

(2)另一種方案,自動形式,採用 hash tag方案;在原key上面,新增以{}包圍的字首名。用不同的字首名,來進行對映分配。

7. hash tag 的官方解釋

Hash tags
There is an exception for the computation of the hash slot that is used in order to implement hash tags. 
Hash tags are a way to ensure that multiple keys are allocated in the same hash slot.
This is used in order to implement multi-key operations in Redis Cluster. To implement hash tags, the hash slot for a key is computed in a slightly different way in certain conditions.
If the key contains a "{...}" pattern only the substring between { and } is hashed in order to obtain the hash slot.
However since it is possible that there are multiple occurrences of { or } the algorithm is well specified by the following rules: IF the key contains a { character. AND IF there is a } character to the right of {. AND IF there are one or more characters between the first occurrence of { and the first occurrence of }. Then instead of hashing the key, only what is between the first occurrence of { and the following first occurrence of } is hashed. Examples: The two keys {user1000}.following and {user1000}.followers will hash to the same hash slot since only the substring user1000 will be hashed in order to compute the hash slot. For the key foo{}{bar} the whole key will be hashed as usually since the first occurrence of { is followed by } on the right without characters in the middle. For the key foo{{bar}}zap the substring {bar will be hashed, because it is the substring between the first occurrence of { and the first occurrence of } on its right. For the key foo{bar}{zap} the substring bar will be hashed, since the algorithm stops at the first valid or invalid (without bytes inside) match of { and }. What follows from the algorithm is that if the key starts with {}, it is guaranteed to be hashed as a whole. This is useful when using binary data as key names.

hash tags 是用於計算雜湊槽時的一個例外,是一種確保多個鍵分配到同一個雜湊槽中的方法。這是為了在Redis叢集中實現多鍵操作而使用的。為了實現hash tags,在某些情況下,會以稍微不同的方式計算key的雜湊槽。如果key包含"{...}"模式,則僅對{和}之間的子字串進行雜湊以獲取雜湊槽。但由於可能存在多個{或}出現,因此該演算法遵循以下規則:

  • 如果key包含字元 {
  • 並且如果 } 字元位於 { 的右側
  • 並且在第一個 { 和第一個 } 之間存在一個或多個字元

對於符合上述規則的key,則不會對整個key進行雜湊處理,而只會對第一次出現 { 和隨後第一次出現 } 之間的內容進行雜湊。否則,對整個key進行雜湊處理。

8.可能下線(PFail)與確定下線(Fail)

因為Redis Cluster 是去中心化的,一個節點認為某個節點失聯了並不代表所有的節點都認為它失聯了,所以叢集還得經過一次協商,只有當大多數節點都認為某個節點失聯了,叢集才認為該節點需要進行主從切換來容錯。

Redis叢集節點採用Gossip協議來廣播自己的狀態以及改變對整個叢集的認知。比如一個節點發現某個節點失聯了(PFail,即Possibly Fail),它會將這條資訊向整個叢集廣播,其它節點就可以收到這點的失聯資訊。如果收到某個節點失聯的節點數量(PFail Count)已經達到了叢集的大多數,就可以標記該失聯節點為確定下線狀態(Fail),然後向整個叢集廣播,強迫其它節點也接受該節點已經下線的事實,並立刻對該節點進行主從切換。

9.網路抖動的容忍性

Redis Cluster 提供了一個選項配置 cluster-node-timeout,表示當某個節點持續timeout的時間失聯時,才可以認定該節點出現故障,需要進行主從切換。

另外還有一個選項 cluster-slave-validity-factor作為倍乘係數放大這個超時時間來寬鬆容錯的緊急程度。如果這個係數為零,那麼主從切換是不會抗拒網路抖動的。如果這個係數大於1,它就成為了主從切換的鬆弛係數。

10.叢集常用命令

info replication:返回關於 Redis 主從複製的詳細資訊

cluster info:獲取 Redis 叢集的狀態和統計資訊

cluster nodes:獲取關於叢集中所有節點的詳細資訊。

cluster countkeysinslot <槽位數字編號>:該槽位是否被佔用。1佔用2未佔用

cluster keyslot <keyname> 該key應該存放在哪個槽位上

cluster failover : 將當前redis的身份從slave變成master,原來的master變成slave

1.30張圖 講清楚Redis Cluster

https://cloud.tencent.com/developer/article/2226847

2.Redis cluster specification

https://redis.io/docs/latest/operate/oss_and_stack/reference/cluster-spec/

3.Redis Cluster 深入探究

https://zhuanlan.zhihu.com/p/198963336

4.【譯】Redis叢集規範 (Redis Cluster Specification)

https://www.jianshu.com/p/8a2d810402a9

5.Redis-叢集(cluster)

https://www.cnblogs.com/mingbo-1/p/17992458

6.Redis——叢集(cluster)

https://www.cnblogs.com/caoweixiong/p/14242613.html

相關文章