Optimising use and using multiple memcache servers in a pool
Mikael Johansson
mikael at synd.info
Sat Feb 3 10:45:19 UTC 2007
Hi,
Alan Jay wrote:
> Is there an *overhead* opening up a pool of memcached servers for each
> transaction which is significant in comparison to opening a single server.
No, when using the Memcache::addServer() method the connection is not
established until actually needed. In your example you use
memcache_connect() to get hold of the pool, this has the side effect of
forcing a connection to the first server even though it might not be
used. A better way to build the pool would be:
$memcache = new Memcache();
$memcache->addServer($ds_server1, 11211, 1, 20);
$memcache->addServer($ds_server2, 11211, 1, 20);
$memcache->addServer($ds_server3, 11211, 1, 80);
...
This avoids having to create _any_ connection until a request
specifically lands on that server. After this, if you still want to use
the procedural API you can use the $memcache object as:
$buffer = memcache_get($memcache, 'some_key');
> (do something to deal with the server being down?) probably is something using
> memcahce_set_server_params (possibly).
When fetching/setting a key, a server is selected from the pool by
hashing the key and modding the hash value with the number of servers
(times their weight) in the pool, thus the order of your calls to
addServer() and each servers specific weight is significant.
Modifying the order of the servers, adding/removing a server, modifying
the weight of a server all changes the way the selection algorithm works
and might cause all your keys to hash to a different server. When a
server goes down, it might therefore be advisable to keep it in the pool
with the same weight, but disable it so requests either fail immediately
(and data is fetched from DB) or reroute the request to some other
server in a deterministic manner.
This is what the "retry_interval" and "status" parameters to
Memcache::addServer() and Memcache::setServerParams() are for. Also, the
INI setting "memcache.allow_failover" controls whether failover should
be attempted, i.e. should a request to a failed server be rerouted to
another server.
//Mikael
More information about the memcached
mailing list