简体   繁体   English

PHP memcached 如何确定集群中的哪个节点存储密钥?

[英]How does PHP memcached determine which node in a cluster to store a key on?

I have some data that I want to store in memcached (using the PHP libmemcached client: https://www.php.net/manual/en/intro.memcached.php ).我有一些数据要存储在 memcached 中(使用 PHP libmemcached 客户端: https://www.php.net/manual/en/intro.memcached.ZE1BFD762.621E4093CEE4AC0841 It's something that's hit extremely frequently across my web app.这是在我的 web 应用程序中非常频繁发生的事情。

In order to reduce the amount of traffic to a single memcached node I append a random number between 1 and 10 to the end of the key, in hopes that the client will not store all of the keys on a single node.为了减少单个memcached节点的流量我append一个1到10之间的随机数以key结尾,希望客户端不会把所有的key都存储在单个节点上。

I had assumed that the process of assigning a key was random, but across 15 nodes at least half of the keys went to the same node.我曾假设分配密钥的过程是随机的,但在 15 个节点中,至少有一半的密钥流向了同一个节点。 That makes me think there is something more deterministic about how it decides which node to use for a given key.这让我认为它如何决定将哪个节点用于给定密钥有一些更具确定性的东西。

Does anyone know how it's done?有谁知道它是怎么做的?

It uses a hash.它使用 hash。 In the simplest form, imagine if you run a hash function like MD5 on a key, you can use the first byte to figure out which server it should go to.以最简单的形式,想象一下如果您在密钥上运行 hash function (如 MD5),您可以使用第一个字节来确定它应该从 Z34D1F91FB2E514B8576ZB1A75A89A 到哪个服务器。

This is important, because if 2 servers talk to multiple memcached servers they need to reliable all select the same server for the same key.这很重要,因为如果 2 台服务器与多个 memcached 服务器通信,它们需要为相同的密钥确保所有 select 相同服务器的可靠性。 Random would be bad because it means that a client might try to get() from a different server where the item was stored.随机会很糟糕,因为这意味着客户端可能会尝试从存储项目的不同服务器get()

If you have 15 nodes and more than half of the items were stored in 1 node, you are either: 1. Extremely unlucky or 2. Something is not configured right and some of your servers are marked offline.如果您有 15 个节点,并且超过一半的项目存储在 1 个节点中,那么您是:1. 非常不幸或 2. 配置不正确并且您的某些服务器被标记为离线。

The underlying hash is more complex than a simple 'md5', it uses a 'consistent hashing' algorithm.底层 hash 比简单的“md5”更复杂,它使用“一致哈希”算法。 What this means is if you have 15 nodes, and lose 1, most keys will still resolve to the same server.这意味着如果您有 15 个节点,并且丢失 1 个,大多数密钥仍将解析到同一台服务器。 There's lengthy articles about 'consistent hashing' so it should be easy to get technical details.关于“一致哈希”的文章很长,因此应该很容易获得技术细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM