为什么 Redis 的 hash slot 设置为 16384

Redis 面试 About 1,269 words

问题

Redis使用CRC16算法(CRC16(key) % 16384)可以产生2^16-1=65535个值,但为什么哈希槽设置成了16384

原因

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

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.
  1. 正常的心跳数据包带有节点的完整配置。而16k大小的槽数量所需要耗费的内存为2k,但如果使用65k个槽,这部分空间将达到8k,心跳信息就会很庞大。
  2. Redis集群中主节点的数量基本不可能超过1000个。

16k个槽当主节点为1000的时候,是刚好比较合理的,既保证了每个节点有足够的哈希槽,又可以很好的利用bitmap。 注意:在小型群集中,bitmap将难以压缩,因为当N较小时,bitmap将有slot/N个比特位,在btimap中占很大一部分比特位。

查看 key 的 hash slot

cluster keyslot mykey

输出

192.168.100.1:6380> CLUSTER KEYSLOT mykey
(integer) 14687
Views: 2,800 · Posted: 2021-03-20

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh