gpt4 book ai didi

redis - 分片 Redis 中的搜索和事务

转载 作者:IT王子 更新时间:2023-10-29 06:15:45 27 4
gpt4 key购买 nike

问题涉及redis的分片配置。我已经用 Java 实现了一个小型测试应用程序,它以 user:userID 的形式在 Jedis 上创建了 100.000 个用户哈希。每个散列都有元素:姓名、电话、部门、用户 ID。我还使用键 phone:phone number 创建了简单的键值对,其中包含 userID,其电话号码是 ID,并为每个部门设置了为该特定部门工作的 userID。后两种类型我仅用于搜索。这些结构和搜索类似于Searching in values of a redis db .

简而言之,数据结构:

user:userID->{name, department, phone, userID}
department:department->([userID1, userID2,....])
phone:phone->userID

搜索用例:

  1. 根据 key (即用户 ID)访问用户哈希
  2. 搜索有电话号码的用户
  3. 搜索一个部门的所有用户

单实例分片配置中一切正常,但我会有以下问题:

  • 在单实例配置中,可以查找电话带有宽卡的号码例如使用 KEY 方法,但这不是在分片配置中可用。怎么可能寻找第一部分已知的 key ?
  • 用户 ID 是从一个 zset 中生成的,它的分数增加了用户身份。这可以在单个实例的事务中完成配置 但事务似乎不支持分片即使参与的 key 在 jedis 上的配置同一个实例。 如何解决这个问题,如果多个客户端线程也可以创建用户?

也提前感谢您的回复。

最佳答案

对于您问题的第一部分:

这里没有魔法,如果你想搜索你所有的分片,你必须遍历所有分片。 Jedis 没有此方法,但您可以扩展 ShardedJedis 以添加它(未经测试):

    public Set<String> keys(String pattern) {
HashSet<String> found = new HashSet<String>();

for (Jedis jedis : getAllShards()) {
found.addAll(jedis.keys(pattern));
}

return found;
}

对于你问题的第二部分:

据我所知,Jedis 在使用分片时不支持事务,即使您确实强制相关键位于同一个分片上(参见 Jedis Advanced Usage)。

这个相同的链接提出了一种可能适用于几种情况的解决方法:

Mixed approach

If you want easy load distribution of ShardedJedis, but still need transactions/pipelining/pubsub etc, you can also mix the normal and the sharded approach: define a master as normal Jedis, the others as sharded Jedis. Then make all the shards slaveof master. In your application, direct your write requests to the master, the read requests to ShardedJedis. Your writes don't scale anymore, but you gain good read distribution, and you have transactions/pipelining/pubsub simply using the master. Dataset should fit in RAM of master. Remember that you can improve performance of the master a lot, if you let the slaves do the persistance for the master!

关于redis - 分片 Redis 中的搜索和事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17616987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com