Redis cluster multi-key operation

Redis集群没有使用一致性hash,而是引入了 哈希槽(hash slot)的概念。

Redis集群有16384( 214 )个哈希槽,每个key通过CRC16校验后对16384取模来决定放置哪个槽。集群的每个节点负责一部分hash槽。HASH_SLOT = CRC16(key) mod 16384

Redis集群的多键运算要求运算中的所有键都在一个槽中,否则会报 ERR CROSSSLOT Keys in request don't hash to the same slot 错误。

可以在键中使用哈希标签(hash tag)来解决这个问题,简单的讲就是使用花括号({})将key中的某部分字符串括起来,这样就会使用这部分字符串计算取模分槽,就可以确保某些key被分配到一个槽中。

There is an exception for the computation of the hash slot that is used in order to implement hash tags. Hash tags are a way to ensure that multiple keys are allocated in the same hash slot. This is used in order to implement multi-key operations in Redis Cluster.

In order to implement hash tags, the hash slot for a key is computed in a slightly different way in certain conditions. If the key contains a “{…}” pattern only the substring between { and } is hashed in order to obtain the hash slot. However since it is possible that there are multiple occurrences of { or } the algorithm is well specified by the following rules:

  • IF the key contains a { character.
  • AND IF there is a } character to the right of {
  • AND IF there are one or more characters between the first occurrence of { and the first occurrence of }.

Then instead of hashing the key, only what is between the first occurrence of { and the following first occurrence of } is hashed.

—— Redis Cluster Specification

你可能感兴趣的:(----,Redis,DB,-,Databases)