Memcached 扩展:关键“分组”

发布于 2024-11-29 15:16:08 字数 517 浏览 3 评论 0原文

由于最佳实践是将在单个服务器上经常检索在一起(使用 multiGet)的相关密钥分组以获得最佳性能,因此我有几个关于为此而构建的客户端函数所采用的隐式机制的问题。

我已经看到两种不同的方法来实现我认为使用 libmemcache(特别是 php-memcached)的相同目的。第一种也是最明显的方法是使用 getByKey/setByKey 将密钥映射到服务器,第二种方法是使用选项 OPT_PREFIX_KEY (在 memcached::_construct 下的 php 文档中发布了一个简单的示例),根据文档,该方法是“用于为您的项目密钥创建一个‘域’”。第二种方法的警告是它只能在每个实例的基础上设置,这可能是好事,也可能不是好事。

因此,除非我完全错误,否则这两种方法实际上并没有达到相同的目的;与其他方法相比,采用这种方法有什么明显的好处吗?

当我谈论这个主题时,我的另一个问题是:在一致的散列场景中将密钥映射到服务器有什么影响(如果有的话)?我假设如果一个节点发生故障,自由格式密钥将简单地重新映射到新服务器而不会出现任何问题..

谢谢!

As it is best practice to group related keys that are frequently retrieved together (using multiGet) on a single server for optimum performance, I have a couple questions regarding the implicit mechanics employed by the client functions built for doing this.

I have seen two different approaches for serving what I assume is the same purpose using libmemcache (php-memcached specifically). The first and most obvious approach is to use getByKey/setByKey to map keys to servers and the second is to use the option OPT_PREFIX_KEY (there is a simple example posted in the php documentation under memcached::_construct), which according to the documentation is "used to create a 'domain' for your item keys". The caveat of the second approach is that it can only be set on a per-instance basis, which may or may not be a good thing.

So unless I am completely mistaken, and these two approaches don't actually serve the same purpose; is that any clear benefit for going with approach over the other?

And while I'm on this topic my other question would be: What are the implications, if any, to mapping keys to servers in a consistently hashed scenario? I'm assuming that if a node were to fail, the freeform key would simply be remapped to a new server without any issue..

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒眉千度 2024-12-06 15:16:08

如果这些键实际上几乎总是一起检索,您可能希望将它们一起缓存在单个键/值对中,例如通过对键进行排序和连接并存储以 JSON 或类似格式序列化为字典的值。

回到你的问题:

  • OPT_PREFIX_KEY 几乎与按键对值进行分组无关,它只是为该特定客户端使用的所有键添加前缀,因此“1”变为“foo1”,并使用此新值通过一致散列进行分发,无需任何分组通过“foo”。
  • getByKey/setByKey 做的事情最接近你想要的,因为它可以将不同的密钥传递给 libketama(用于选择服务器)和 memcached 服务器。如果您指定相同的第一个键和不同的第二个键 - 它们最终将位于同一 memcached 服务器上,但不会相互覆盖。

过早的优化是万恶之源

If these keys are really almost always retrieved together you probably want to cache them together in a single key/value pair, for example by sorting and concatenating keys and storing values serialized as a dictionary in JSON or similar format.

Returning to your question:

  • OPT_PREFIX_KEY has almost nothing to do with grouping values by key, it just prefixes all keys used by this particular client, so "1" becomes "foo1" and is distributed by consistent hashing using this new value, without any grouping by "foo".
  • getByKey/setByKey does the closest thing to what you want, since it can pass different keys to libketama (used to choose server) and memcached server. If you specify same first key and different second keys - they will end up on same memcached server, but won't overwrite each other.

Premature optimization is the root of all evil

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文