Redis基础知识(学习笔记5--Redis Cluster)
1. 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个槽位,每个节点负责其中一部分槽位。
2.将key映射到hash slot的算法如下:
HASH_SLOT = CRC16(key) mod 16384
取key进行CRC16计算之后对16384取模运算得到key所在的slot,由于redis cluster在启动时会对每一台master节点分配slot空间,那么当前slot的值在哪台master节点的slot空间范围内,key就存储在哪台节点。
CRC16() 是一种带有校验功能的、具有良好分散功能的、特殊的hash算法函数。
【图片来自 《Redis cluster集群Hash Tag原理分析》 https://www.51cto.com/article/757391.html】
补充一点:其实Redis 中计算槽点的公式不是上面的那个,而是:
slot = CRC(key) & 16383
如果计算 a%b,如果b是2的整数次幂,那么a%b=a&(b-1)。位运算的效率更高。
3.正常情况下,每个hash slot只会由一个节点提供服务。
当Redis Cluster 的客户端来连接集群时,会得到一份集群的槽位配置信息。这样客户端要查找某个Key时,可以直接定位到目标节点。(这一点与Codis不同,Codis需要通过Proxy来定位目标节点,Reids Cluster 则直接定位)。
4.数据倾斜的问题
(1) 一种方案时,发现手动迁移;例如一些商品促销前,检查下,发现数据集中了,或者严重倾斜,可以考虑手动迁移。
(2)另一种方案,自动形式,采用 hash tag方案;在原key上面,添加以{}包围的前缀名。用不同的前缀名,来进行映射分配。
5. 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进行散列处理。
6.可能下线(PFail)与确定下线(Fail)
因为Redis Cluster 是去中心化的,一个节点认为某个节点失联了并不代表所有的节点都认为它失联了,所以集群还得经过一次协商,只有当大多数节点都认为某个节点失联了,集群才认为该节点需要进行主从切换来容错。
Redis集群节点采用Gossip协议来广播自己的状态以及改变对整个集群的认知。比如一个节点发现某个节点失联了(PFail,即Possibly Fail),它会将这条信息向整个集群广播,其它节点就可以收到这点的失联信息。如果收到某个节点失联的节点数量(PFail Count)已经达到了集群的大多数,就可以标记该失联节点为确定下线状态(Fail),然后向整个集群广播,强迫其它节点也接受该节点已经下线的事实,并立刻对该节点进行主从切换。
7.网络抖动的容忍性
Redis Cluster 提供了一个选项配置 cluster-node-timeout,表示当某个节点持续timeout的时间失联时,才可以认定该节点出现故障,需要进行主从切换。
另外还有一个选项 cluster-slave-validity-factor作为倍乘系数放大这个超时时间来宽松容错的紧急程度。如果这个系数为零,那么主从切换是不会抗拒网络抖动的。如果这个系数大于1,它就成为了主从切换的松弛系数。
8.集群常用命令
info replication:返回关于 Redis 主从复制的详细信息 cluster info:获取 Redis 集群的状态和统计信息 cluster nodes:获取关于集群中所有节点的详细信息,有IP信息;端口信息;角色信息(master or slave);连接状态(connected or disconnected)。 cluster countkeysinslot <槽位数字编号>:该槽位是否被占用。1占用2未占用。注意:指定查询当前node节点上的slot槽位的信息,
跨节点查询不支持,返回(integer)0
cluster getkeysinsloot <槽位数字编号> count :返回该槽位指定count个key cluster keyslot <keyname> 该key应该存放在哪个槽位上 cluster failover : 将当前redis的身份从slave变成master,原来的master变成slave
9. 集群的启动
step 1 Start each instance;
step 2 Create a Redis Cluster
例如
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005--cluster-replicas 1
The option --cluster-replicas 1
means that we want a replica for every master created.---即一主对应一从的架构。
step 3 成功创建的返回标志
Finally, if everything has gone well, you'll see a message like this:
[OK] All 16384 slots covered
补充,如果关闭集群怎么关闭呢? --其实只要关闭每个node节点即可。
10. 集群的连接
例如端口为7000
$ redis-cli -c -p 7000
与之前单机连接相比的唯一区别就是增加了参数 -c。
11. 集群的动态扩容
添加节点
例如
redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000
used the add-node command specifying the address of the new node as first argument, and the address of a random existing node in the cluster as second argument.
127.0.0.1:7000代表既有集群中的任意一个redis实例,用来代表集群。127.0.0.1:7006是待加入的redis实例。注意二者的顺序。
start a resharding
例如
redis-cli --cluster reshard 127.0.0.1:7000
127.0.0.1:7000代表既有集群中的任意一个redis实例。
注意,resharding 开始前,需要设置以下问题
How many slots do you want to move (from 1 to 16384)?
what is the receiving node ID?
Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source ndes IDs.
Do you want to proceed with the proposed reshard plan (yes/no)?
为指定master节点添加从节点
例如
redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000 --cluster-slave --cluster-master-id 3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e
127.0.0.1:7000代表既有集群中的任意一个redis实例,用来代表集群。127.0.0.1:7006是待加入的redis实例。注意二者的顺序。3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e 为集群中的主节点(待匹配的主节点)
Remove a node
如果是replica node
redis-cli --cluster del-node 127.0.0.1:7000 `<node-id>`
如果是master节点,需要先通过reshard将这个节点上的槽点全部移走。
即You can remove a master node in the same way as well, however in order to remove a master node it must be empty. If the master is not empty you need to reshard data away from it to all the other master nodes before.
12. cluster集群使用的一些限制
- 仅支持0号数据库
- 批量key操作支持有限
- 分区仅限于key
- 事务支持有限
- 不支持分级管理
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
https://www.cnblogs.com/caoweixiong/p/14242613.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库