Skip to content
标签
欢迎扫码关注公众号

Redis - ERR 'RENAME' command keys must in same slot

在测试环境(Redis 是单机版)使用 RENAME 功能时是好的,到了生产环境(阿里云的 Redis 集群版)报了如下错误:

ERR 'RENAME' command keys must in same slot.
channel: [id: 0x31b56a88, L:/10.0.3.34:46962 - R:r-xxxxxxxxxxxxxxxxxx.redis.rds.aliyuncs.com/xxx.xxx.xxx.xxx:6379]
command: (RENAME),
promise: java.util.concurrent.CompletableFuture@1e13ae01[Not completed, 1 dependents],
params: [[99, 108, 111, 99, 107, 45, 105, 110, 58, 102, ...], [99, 108, 111, 99, 107, 45, 105, 110, 58, 102, ...]];
nested exception is org.redisson.client.RedisException: ERR 'RENAME' command keys must in same slot.
channel: [id: 0x31b56a88, L:/10.0.3.34:46962 - R:r-xxxxxxxxxxxxxxxxxx.redis.rds.aliyuncs.com/xxx.xxx.xxx.xxx:6379]
command: (RENAME),
promise: java.util.concurrent.CompletableFuture@1e13ae01[Not completed, 1 dependents],
params: [[99, 108, 111, 99, 107, 45, 105, 110, 58, 102, ...], [99, 108, 111, 99, 107, 45, 105, 110, 58, 102, ...]]

根据错误消息,执行 RENAME 的两个 Key 必须处于相同的插槽。我这里使用的两个 Key 的后缀不一样。

为了保证两个缓存 Key 处于同一个插槽,可以将两个 Key 的公共部分使用 {} 包含起来,此时 Redis 仅会根据大括号中间的部分来决定这个缓存 Key 处于哪个插槽(没有大括号时使用整个 Key 的名字)。

关于插槽的更多信息可以参考“技多不压身”同学的博客,下面是部分节选:

哈希槽的概念

Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,Redis 先对 key(有效值) 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。
Redis 集群没有使用一致性哈希, 而是引入了哈希槽的概念。

key 有效值:

  1. 如果 key 中包含了 { 符号,且在 { 符号后存在 } 符号,并且 {} 之间至少有一个字符,则有效部分是指 {} 之间的部分;
    • key={hello}_taotao 的有效部分是 hello
  2. 如果不满足上一条情况,整个 key 都是有效部分;
    • key=hello_taotao 的有效部分是全部 hello_taotao

哈希槽的优点:

  1. 可以方便的添加或移除节点。
  2. 当需要增加节点时,只需要把其他节点的某些哈希槽挪到新节点就可以;
  3. 当需要移除节点时,只需要把移除节点上的哈希槽挪到其他节点就行了;

在这一点上,我们以后新增或移除节点的时候不用先停掉所有的 Redis 服务。

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.