Redis基础知识(学习笔记5--Redis Cluster)

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

 

posted @ 2024-06-16 22:59  东山絮柳仔  阅读(17)  评论(0编辑  收藏  举报