memcached server failover
I'm running into an odd issue with pecl/memcached client. In my setup, I have 3 memcached servers. When I stop (this is an ec2 instance) one of the memcached servers to simulate a complete failure, the "get" operation takes 4 seconds to complete. How do I force it to timeout earlier?
Here are some code snippets:
$this->memcache = new Memcached;
$this->memcache->setOption(Memcached::OPT_DISTRIBUTION ,Memcached::DISTRIBUTION_CONSISTENT);
$this->memcache->setOption(Memcached::OPT_LIB开发者_运维百科KETAMA_COMPATIBLE ,TRUE);
...
foreach($CFG->data_memcache_servers as $server){
if (!$this->memcache->addserver($server,11211)){
throw new Exception('Unable to connect to memcache server');
}
}
...
$data = $this->memcache->get($key);
I'm experiencing the same issue, with all timeouts set to 50ms, a set() on a server without memcached (or memcached stopped), a set() or a get() take 21 seconds.
It seems to be a bug in libmemcached, as we can see here: https://bugs.launchpad.net/libmemcached/+bug/778777 (and many other websites)
I'm working on Debian, libmemcached is 0.40, and the bug seems to be at least until 0.49 (for auto eviction of bad servers).
Debian unstable has 0.44, which responds correctly to the CONNECT_TIMEOUT value.
Try this addserver
syntaxe
addserver($server, 11211, true, 10, 1, -1, false);
PECL Memcached prior 2.0 doesn't support failover/timeout-relevant parameters in addServer(). If you are stuck with version 1.0.x (as shipped in Ubuntu 10.04 LTS for instance), this is a simple way of providing failover support from a single main server to a single failover server:
$m = new Memcached();
$m->addServer(MEMBASE_HOST, MEMBASE_PORT);
// Immediately check server connection
$m->get('onlinecheck_' . uniqid());
if (in_array($m->getResultCode(), array(Memcached::RES_ERRNO, Memcached::RES_UNKNOWN_READ_FAILURE)))
{
// Main server not available - Failing over
$m = new Memcached();
$m->addServer(MEMBASE_FAILOVER_HOST, MEMBASE_FAILOVER_PORT);
}
精彩评论