简体   繁体   中英

Memcached lib failover like memcache lib

I'm using php memcached (not memcache) but I have a problem about failover.

For example, I had two servers of memcached but when one of server crash I want that memcached continue to operate with just one.

With lib memcache that works but not with memcached. I tried something like that

<?php

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

$m->addServer('server1', 11211);
$m->addServer('server2', 11211);

var_dump($m->set('1234','test'));
var_dump($m->get('1234'));

$m->flush();

$m = new Memcache();

$m->addServer('server1', 11211);
$m->addServer('server2', 11211);

var_dump($m->set('1234','test'));
var_dump($m->get('1234'));

$m->flush();

?>

When both memcached are up:

bool(true)
string(4) "test"
bool(true)
string(4) "test"

But when I stop server2:

bool(false)
bool(false)

Notice: MemcachePool::set(): Server server2 (tcp 11211, udp 0) failed with: Connection refused (111) in /root/memcached.php on line 22

Call Stack:
    0.0013     637584   1. {main}() /root/memcached.php:0
    0.0220     653104   2. MemcachePool->set() /root/memcached.php:22

bool(true)
string(4) "test"

On memcached the IO are directly blocked and return false whereas on memcache there is a notice but IO works.

PS: I tried setOption(Memcached::OPT_DISTRIBUTION,Memcached::DISTRIBUTION_CONSISTENT); but does not work

Option $m->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true); allows that :)

You lack one option:

$m->setOption(Memcached::OPT_DISTRIBUTION,Memcached::DISTRIBUTION_CONSISTENT);
$m->setOption(Memcached::OPT_REMOVE_FAILED_SERVERS, true);

Else the «set()» call will result in «SERVER HAS FAILED AND IS DISABLED UNTIL TIMED RETRY», but as the server isn't removed from the list, it will fail for every set() on the same server.

Note that using this configuration, when you set() on a bad server, it is removed AND the data is correctly set() on a good server (keys of the bad server are distributed across all remaining servers).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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