为什么 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:
- 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.
- 正常的心跳数据包带有节点的完整配置。而
16k
大小的槽数量所需要耗费的内存为2k
,但如果使用65k
个槽,这部分空间将达到8k
,心跳信息就会很庞大。 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: 3,158 · Posted: 2021-03-20
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...