pecl::memcache的哈希策略
本文按署名·非商业用途·保持一致授权作者:
,发表于2008年12月27日14时48分
测试代码的过程是这样(查看测试代码):
#加入10台server(我在本机测试,所以用的是10个不同的端口来模拟10个server)。
#写入1W条数据,不同的key,根据哈希策略,会存储到不同的server。
#计算出这1W条数据在10台服务器的分布,即下面的分布数据。
#加入2台新的server,这时候就有了12台server。于是根据哈希策略,就有部分的key会map到新的server上。
#计算这1W条数据还有多少条可以获取,也就是还有多少条数据没有被改变对应的server。即下面的存活率。
哈希策略,指的是memcache.hash_strategy。目前有standard模式和consistent模式。standard模式其实就是%,即取模。而consistent,就比较复杂,可以看这里:http://tech.idv2.com/2008/07/24/memcached-004/。
哈希函数,也就是memcache.hash_function,目前有crc32和fnv。
一个key首先是被哈希函数转换为哈希串,然后用哈希策略找到这个哈希串所对应的server。废话少说了,看测试结果吧。
—————————————————-
测试数目:10000
——-
哈希策略:consistent
哈希函数:crc32
分布:
port:11211:9.34%
port:11212:11.48%
port:11213:10.71%
port:11214:9.22%
port:11215:7.15%
port:11216:8.17%
port:11217:11.58%
port:11218:9.71%
port:11219:10.81%
port:11220:11.82%
存活率:83.7%
——-
哈希策略:consistent
哈希函数:fnv
分布:
port:11211:3.16%
port:11212:16.41%
port:11213:14.28%
port:11214:6.51%
port:11215:13.81%
port:11216:10.18%
port:11217:8.81%
port:11218:11.41%
port:11219:11.37%
port:11220:4.05%
存活率:74.85%
——-
哈希策略:standard
哈希函数:crc32
分布:
port:11211:9.77%
port:11212:10.01%
port:11213:9.61%
port:11214:9.71%
port:11215:10.65%
port:11216:10.2%
port:11217:9.9%
port:11218:9.93%
port:11219:10.06%
port:11220:10.15%
存活率:16.88%
——-
哈希策略:standard
哈希函数:fnv
分布:
port:11211:9.93%
port:11212:10.39%
port:11213:9.65%
port:11214:10.18%
port:11215:9.66%
port:11216:10.13%
port:11217:10%
port:11218:10.57%
port:11219:9.67%
port:11220:9.81%
存活率:16.75%
——————————————————–
首先看分布。
当哈希策略为consistent,而哈希函数为fnv时,分布极为不均。最高和最低居然有超过10个百分比的差距,根本无法用于生产环境。而其他的3个配置策略,则相差无几。令我惊讶的是standard模式下,fnv居然比crc32还要略胜一筹。
然后看存活率。
其实对于小型应用,甚至中等规模的应用,例如国内的VeryCD和豆瓣,存活率可能也不是这么重要,不就是几个cache嘛。但是想象一下facebook之类的巨无霸。如果加入一台新的server导致大量的缓存失效(上面的standard模式有超过80%数据的失效),太可怕了。当然我不知道facebook的规模,无法知道这会带来多大的影响。
插一句:相对于VeryCD,豆瓣可能对这个存活率比较重视,因为他们的session是存储在memcache里的,客户端只有一个session id。如果session失效,那么就会导致用户的登录退出。而VeryCD,除了session id,还把一个用来认证的串存储在客户端的cookie里。如果session失效,会尝试使用这个认证串来重新登录,重建session。
拉回来说存活率。从结果可以看出来,consistent的存活率远远高于standard,而consistent+crc32是存活率最高的策略。同时由于consistent+fnv的分布太不均,虽然存活率上它和consistent+crc32相差无几,也不得不放弃了。
所以,对于切换存活率有一定要求的网站来说,pecl::memcache的最佳哈希策略是:
memcache.hash_strategy=consistent
memcache.hash_function=crc32
参考:
pecl::memcache的配置参数,http://www.php.net/manual/en/memcache.ini.php

2009-04-22 00:02:33
请问你这个测试的结果是否已经用于现行系统,是否正常,给个后续的文章吧.
2009-04-22 00:06:44
等了好几个月了~~~期待后续~~~
2009-04-22 00:29:05
pecl::memcached里有个libketama_compatible,里面似乎是md5+consistent,ketama的默认.php手册推荐.不知道这个效果怎么样.
Memcached::OPT_LIBKETAMA_COMPATIBLE
Enables or disables compatibility with libketama-like behavior. When enabled, the item key hashing algorithm is set to MD5 and distribution is set to be weighted consistent hashing distribution. This is useful because other libketama-based clients (Python, Ruby, etc.) with the same server configuration will be able to access the keys transparently.
Note: It is highly recommended to enable this option if you want to use consistent hashing, and it may be enabled by default in future releases.
2009-05-08 10:04:53
我没做过memcached的测试
2009-05-11 20:10:57
这是什么看不懂!
2009-10-24 15:21:23
期待续文
2010-09-30 16:13:21
[...] Memcache,请参考下列链接: http://www.surfchen.org/archives/348 [...]
2010-12-15 16:12:07
[...] 原文地址:http://www.surfchen.org/archives/348 此条目发表在 PHP&Zend, web架构&负载&性能 分类目录,贴了 memcache 标签。将固定链接加入收藏夹。 ← php的memcache配置大全 [...]