开发者

PHP pecl/memcached extension slow when setting option for consistent hashing

Using the newer PHP pecl/memcached extension. Calls to Memcached::setOption() like;

$m = new Memcached();
$m->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);

are costing between 150 to 500ms - just in making the call to setOption() and as we're not using persistent connections but rather doing this on every request, it hurts.

Delving deeper, setting Memcached::OPT_DISTRIBUTION to Memcached::DISTRIBUTION_CONSISTENT ends up calling update_continuum() in libmemcached which appears to be fairly intensive, although 开发者_JAVA百科we're only passing a list of 15 memcached servers in, so somewhat surprising to see it take between 150 to 500ms to rebuild the continuum data structure.

Could it be setting this option is only suitable for persistent connections, where it's called only once while making the initial connection? Or is this a bug libmemcached?

Using the newer pecl/memcached extension 1.0.1 with libmemcached 0.38

Thanks.


libmemcached 0.38 is fairly old at this point. So is pecl/memcached 1.0.1. Could you try pecl/memcached 2.0.0b1 release from github?


Same issue with pecl/memcached 2.2.0 (latest from PECL as of writing). The only solution for me was to switch from DISTRIBUTION_CONSISTENT to:

$memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$memcached->setOption(Memcached::OPT_REMOVE_FAILED_SERVERS, true);

It seems to work fine:

var_dump($memcached->getServerByKey($key)['host']);
// string '192.168.56.1' (length=12) <== dead host

$memcached->set($key, $result, 3600);

var_dump($memcached->getServerByKey($key)['host']);
// string '127.0.0.1' (length=9) <== working host

var_dump($memcached->getLastErrorMessage());
// string 'SUCCESS' (length=7)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜