> 文章列表 > Redis第二十四讲 Redis集群如何确保数据能被插入到同一个哈希槽与集群的哈希槽为什么是16384

Redis第二十四讲 Redis集群如何确保数据能被插入到同一个哈希槽与集群的哈希槽为什么是16384

Redis第二十四讲 Redis集群如何确保数据能被插入到同一个哈希槽与集群的哈希槽为什么是16384

为什么哈希槽是16384

Redis 集群并没有使用一致性hash,而是引入了哈希槽的概念。 Redis 集群有16384个哈希槽,每个key通过CRC16校验后对16384取模来决定放置哪个槽,集群的每个节点负责一部分hash槽。 但为什么哈希槽的数量是16384(2^14)个呢?

  • 在redis节点发送心跳包时需要把所有的槽放到这个心跳包里,以便让节点知道当前集群信息,16384=16k,在发送心跳包时使用bitmap压缩后是2k( 2 * 8 (8 bit) * 1024 (1k) = 2K ),也就是说使用2k的空间创建了16k的槽数。

github上作者给出的解释: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