# 为什么Redis集群有16384个槽

## 引言

ps:CRC16算法产生的hash值有16bit，该算法可以产生2^16-=65536个值。换句话说，值是分布在0~65535之间。那作者在做mod运算的时候，为什么不mod65536，而选择mod16384？

https://github.com/antirez/redis/issues/2576

The reason is:

• 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.
• 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.

## 正文

### 基础

127.0.0.1:7000>cluster meet 127.0.0.1:7001


• (1)交换什么数据信息
• (2)数据信息究竟多大
• (3)定期的频率什么样

16384÷8÷1024=2kb

redis集群内节点，每秒都在发ping消息。规律如下

• (1)每秒会随机选取5个节点，找出最久没有通信的节点发送ping消息
• (2)每100毫秒(1秒10次)都会扫描本地节点列表，如果发现节点最近一次接受pong消息的时间大于cluster-node-timeout/2 则立刻发送ping消息

数量=1+10*num（node.pong_received>cluster_node_timeout/2）

### 回答

(1)如果槽位为65536，发送心跳信息的消息头达8k，发送的心跳包过于庞大。

65536÷8÷1024=8kb

(2)redis的集群主节点数量基本不可能超过1000个。

(3)槽位越小，节点少的情况下，压缩比高
Redis主节点的配置信息中，它所负责的哈希槽是通过一张bitmap的形式来保存的，在传输过程中，会对bitmap进行压缩，但是如果bitmap的填充率slots / N很高的话(N表示节点数)，bitmap的压缩率就很低。

ps：文件压缩率指的是，文件压缩前后的大小比。

posted @ 2020-05-09 09:51  YoungDeng  阅读(3305)  评论(1编辑  收藏  举报