Redis Cluster集群方式

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

1. 部署方式

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

Redis Cluster集群方式_第1张图片

2. 如何负载均衡

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

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

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

slot = CRC16(key) & 16383

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

redis节点 负责的槽位
节点1 0-5461
节点2 5462-10922
节点3 10923-16383

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

redis节点 负责的槽位
节点1 0-5461
节点2 5462-10922
节点3 10923-13680
节点4 13681-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 集群节点数量)的值。

你可能感兴趣的:(redis,redis)