Redis Cluster集群方式

本文解析了Redis Cluster的部署架构,关键在于3主3从保证可靠性。负载均衡通过hash槽机制,节点间槽位手动配置。节点下线判断涉及主观和客观状态。同时探讨了16384桶限制的原因。

原文地址:https://kang.fun/redis-cluster
转载请注明出处

1. 部署方式

至少为3主3从,以保证可靠性,比如下面这种部署架构:

img

2. 如何负载均衡

cluster采用hash槽的方式进行key和redis实例的匹配,共2^14个槽,即16384个槽。

集群中,每个redis主节点都负责一部分槽,从节点仅冷备作用。

当redis设置值时,槽公式如下:

slot = CRC16(key) & 16383

每个节点和槽的映射关系手动配置,如:

redis节点负责的槽位
节点10-5461
节点25462-10922
节点310923-16383

增删节点,都要将槽重新分配,比如新增节点4,那么可以将节点3的一部分槽分给节点4,分配后映射关系如下:

redis节点负责的槽位
节点10-5461
节点25462-10922
节点310923-13680
节点413681-16383

3. 如何判定节点下线

集群中的每个节点都会定期向其他节点发送ping命令,如果接受ping消息的节点在指定时间内没有回复pong,则发送ping的节点就把接受ping的节点标记为主观下线

如果集群半数以上的主节点都将主节点A标记为主观下线,则节点A将被标记为客观下线(通过节点的广播)即下线。

4. Redis集群最大节点个数是多少?

Redis集群预分好16384个桶(哈希槽:2^14 = 16384)

为什么是16384个桶而不是65535(2^16)呢?

官方解答

The reason is:

  1. Normal heartbeat packets carry the full configuration of a node, that can be replaced in an idempotent way with the old in order to update an old config. This means they contain the slots configuration for a node, in raw form, that uses 2k of space with16k slots, but would use a prohibitive 8k of space using 65k slots.
  2. At the same time it is unlikely that Redis Cluster would scale to more than 1000 mater nodes because of other design tradeoffs.

So 16k was in the right range to ensure enough slots per master with a max of 1000 maters, but a small enough number to propagate the slot configuration as a raw bitmap easily. Note that in small clusters the bitmap would be hard to compress because when N is small the bitmap would have slots/N bits set that is a large percentage of bits set.

首先,16k的桶,压缩成bitmap是2k的数据大小(2 * 8 (8 bit) * 1024(1k) = 16K),65k的桶是8k的数据大小(8 * 8 (8 bit) * 1024(1k) =65K)。

redis心跳每次都会携带所有槽的信息,且通常redis集群下不会超过1000个节点,所以65535相对没那么有必要。

所以16384是一个相对平衡(心跳传输数据大小 vs 集群节点数量)的值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值