Skip to content

Instantly share code, notes, and snippets.

@ukautz
Created February 16, 2017 11:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ukautz/35a7bdede02c12f6b0b829ad42f77547 to your computer and use it in GitHub Desktop.
Save ukautz/35a7bdede02c12f6b0b829ad42f77547 to your computer and use it in GitHub Desktop.
Tuning memcached options for redundancy & fast failover
<?php
$servers = [
['10.0.0.1', 11211],
['10.0.0.1', 11212],
];
$id = 1;
$timeout = 50;
$mc = new \Memcached($id);
$mc->setOptions([
// Assure that dead servers are properly removed and ...
Memcached::OPT_REMOVE_FAILED_SERVERS => true,
// ... retried after a short while
Memcached::OPT_RETRY_TIMEOUT => 1,
// KETAMA must be enabled so sthat replication can be used
Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
// Replicate the data, i.e. write it to both memcached servers
Memcached::OPT_NUMBER_OF_REPLICAS => 1,
// Those values assure that a dead (due to increased latency or really unresponsive) memcached server increased
// dropped fast and the other is used.
Memcached::OPT_POLL_TIMEOUT => $timeout,
Memcached::OPT_SEND_TIMEOUT => $timeout * 1000,
Memcached::OPT_RECV_TIMEOUT => $timeout * 1000,
Memcached::OPT_CONNECT_TIMEOUT => $timeout,
// Those I am not sur. Bnary proto is supposed to be faster. Not blocking sounds legit.
Memcached::OPT_BINARY_PROTOCOL => true,
Memcached::OPT_NO_BLOCK => true,
]);
// init servers
if (!$mc->getServerList()) {
foreach ($servers as $server) {
$mc->addServer($server[0], $server[1]);
}
}
// write & read
$k = "redundant-counter";
$c = $mc->get($k) ?: 1;
$mc->set($k, $c+1);
print "GET: $c\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment