有没有一种方法可以使特定密钥在集群模式下位于特定Redis实例上? [英] Is there a way to make a specific key locate on a specific redis instance in cluster mode?

查看:145
本文介绍了有没有一种方法可以使特定密钥在集群模式下位于特定Redis实例上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的多重锁位于不同的redis实例上。



我发现redission可以指定一个实例来执行命令,但是如果命令与密钥相关,指定的实例会将命令传输到另一个实例。



能给我一些建议吗?

解决方案

可以,但这并不简单。首先,Redis在密钥中使用花括号来确定其分片部分,因此您可以决定修改密钥并将其发送到任意分片。



现在,您需要做两件事:



  1. 一种知道哪个字符串映射到哪个插槽的方法,因此可以强制使用分片字符串


第一个很简单-集群插槽会为您提供该地图,只需对其进行解析即可。



第二个比较棘手-但幸运的是我已经完成了这项工作。我创建了一个表,该表将最短的字母数字字符串映射到Redis集群中的每个16384插槽。我用的不是C,但是您可以轻松地将其转换为任何内容。 https://gist.github.com/dvirsky/93f43277317f629bb06e858946416f7e



给定键和所需的节点,您的算法将是:


  1. 看一下插槽图,并获取该节点上的插槽范围。


  2. 选择该节点范围内的插槽。


  3. 在分片表中查找该插槽的条目。


  4. 将其拍打关键上带有大括号的字符串。例如将 foo 转换为 foo {e4x}


就是这样!使用此键的任何命令都将路由到该分片。



它的伪python版本:

 #构建此表来自集群插槽或集群节点
插槽= {
'127.0.0.1:7000':[(0,1045),(2000,2100)]
...
}

#从C示例构建此表:
sharding_table = [ 06S, Qi, 5L5, 4Iu, 4gY,....]

def retarget_key(键,节点):
range = slot [node]

sharding_key = shading_table [ranges [0] [0]]
返回'%s {%s}'%(键,分片键)


I want to make my multi-locks locate on different redis instances.

I find out that redission can specify an instance to execute command on, but if the command is key-related, the instance specified will transmit the command to another instance.

Can you give me some advice?

解决方案

You can, but it's not trivial. First of all, Redis uses curly braces in the key to determine the sharding part of it, so you can decide to modify a key and send it to an arbitrary shard.

Now, you need two things:

  1. A map of what shard or slot-range resides in which redis instance.

  2. A way to know which string maps to which slot, so you can force a "sharding string" on your key to route it to a specific shard.

The first one is easy - CLUSTER SLOTS will give you that map, just parse it.

The second one is more tricky - but luckily I've done this work already. I have created a table of the shortest possible alphanumeric string mapping to each of the 16384 slots in Redis cluster. I'ts in C but you can easily convert it to whatever. https://gist.github.com/dvirsky/93f43277317f629bb06e858946416f7e

So your algorithm would be, given a key and a desired node:

  1. Look at the slot map and take the slot ranges that reside on that node.

  2. Select a slot within that node's range.

  3. Look in the sharding table for the entry of that slot.

  4. Slap that string with curly braces on the key. e.g. convert foo to foo{e4x}.

And that's it! Any command using this key will be routed to that shard.

Pseudo-python version of it:

# Build this table from CLUSTER SLOTS or CLUSTER NODES
slots = {
   '127.0.0.1:7000': [(0, 1045),(2000,2100)]
   ...
 }

# Build this table from the C example:
sharding_table = ["06S", "Qi", "5L5", "4Iu", "4gY", ....]

def retarget_key(key, node):
    ranges = slots[node] 

    sharding_key = shading_table[ranges[0][0]]
    return '%s{%s}' % (key, sharding_key)

这篇关于有没有一种方法可以使特定密钥在集群模式下位于特定Redis实例上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆