解决memcache连接奇慢问题一例

来源:互联网 发布:淘宝客服的重要性 编辑:程序博客网 时间:2024/04/27 12:54

最近用xdebug观察线上程序的运行时间统计,发现往日里跑起来像飞的memcache居然是系统中拖后腿的耗时大户,连接时间特长。

运行环境

  • webserver是apache + php
  • php memcache extension版本是3.0.2,当时是最新的beta版…
  • 有4个memcache server可供使用
  • 代码中会利用php的Memcache::addServer依次连接四个memcache,长连接方式

现象

完成四次addServer一共需要300ms以上,但是一旦连接上,获取单个item飞快,时间在3ms以下。
更可恶的问题在于,虽然执行了四次Memcache::addServer,但是实际使用的始终是最后一个memcache,这实在让人崩溃。

问题解决

使用了一点搜索技巧,在pecl.php.net上找到了类似的bug: First get slow when using multiple memcached servers

这个bug的描述如下:

We are monitoring memcached performance and noticed that when we added a second memcached via Memcache::addServer the first get request is always slower than the subsequent ones although we are using persitent memcached connections. Switching from crc32 to fnv hashing didn’t help either. Is that delay explainable

看起来是最新的memcache extension有一些问题,尝试将这个扩展降级成最新的稳定版2.2.6,然后重启apache看看,memcache连接过慢的问题果然已经解决。

结论

吃螃蟹果然是要付出代价的。。

原创粉丝点击