Redis CRC16校验 2.Redis的CRC16应用

CRC16与slot关系:

redis的RCR16与slot的hash槽就分不开了

hash槽最大是16384,意味着可以有这么多个node节点,但官方推荐最大的节点数量为1000

HASH_SLOT = CRC16(key) mod 16384

why redis-cluster use 16384 slots?

author's :

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.

作者的大概意思是,没有人会把redis搞成1000以上的node节点,然后16k和64k是对于物理空间的取舍,属于一个平衡关系,想搞更大的,也可以

Redis集群是在多个Redis节点之间进行数据共享,它不支持“multi-key”操作(即执行的命令需要在多个Redis节点之间移动数据,比如Set类型的并集、交集等(除非这些key属于同一个node),即Cluster不能进行跨Nodes操作。如下

127.0.0.1:6379> smembers set1

-> Redirected to slot [3037] located at 10.9.33.204:6101
1) "d"
2) "b"
3) "g"
4) "c"
5) "a"
6) "f"
7) "e"
(1.08s)
127.0.0.1:6379> smembers set2
-> Redirected to slot [15294] located at 10.9.33.204:6103
1) "b"
2) "c"
3) "f"
4) "g"
5) "h"
6) "i"
7) "a"
127.0.0.1:6379> sunion set1 set2
(error) CROSSSLOT Keys in request don't hash to the same slot

Redis为了兼容multi-key操作,提供了“hash tags”操作,每个key可以包含自定义的“tags”,在存储的时候根据tags计算此key应该映射到哪个node上。通过“hash tags”可以强制某些keys被保存到同一个节点上,便于进行“multi key”操作。基本上如果关键字包含“{...}”,那么在{和}之间的字符串被hash,然而可能有多个匹配的{或}该算法由以下规则规定:如果key包含{,在{的右边有一个},并在第一次出现{与第一次出现}之间有一个或者多个字符串,那么就作为key进行hash。例如,{user1000}.following和{user1000}.followed就在同一个hash slot;foo{}{bar}整个字符被hash,foo{{bar}},{bar被hash;foo{bar}{zap},bar被hash。如下所示:

127.0.0.1:6379> set {user1000}.following 1000
127.0.0.1:6379> set {user1000}.followed 1000
127.0.0.1:6379> keys *
4) {user1000}.following
6) {user1000}.followed

Redis CRC16校验 2.Redis的CRC16应用_第1张图片

 

特殊说明一点,在resharding期间,原来同一个slot的keys被迁移到不同的node中,multi-key操作可能不可用。

你可能感兴趣的:(Redis)