Skip to content

Commit

Permalink
Use proper redundancy options to make a failover seamless
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Stark committed Oct 14, 2019
1 parent 2ada3b7 commit 5f0cff8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fortrabbit/yii-memcached",
"description": "Memcached Session & Cache for Craft3 / Yii2 on fortrabbit",
"version": "1.0.0",
"version": "1.1.0",
"type": "yii2-extension",

"keywords": ["yii2", "craftcms", "caching", "cache", "memcached", "memcache", "session"],
Expand Down
42 changes: 39 additions & 3 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
class Bootstrap implements BootstrapInterface
{

const TIMEOUT_IN_MILLISECONDS = 50;

/**
* Bootstrapper
*
Expand All @@ -31,6 +33,7 @@ public function bootstrap($app)
'class' => MemCache::class,
'persistentId' => getenv('MEMCACHE_PERSISTENT') ? 'cache' : null,
'servers' => $this->getMemcachedServers(),
'options' => $this->getOptions(),
'useMemcached' => true
]);
} catch (InvalidConfigException $e) {
Expand All @@ -51,13 +54,46 @@ protected function getMemcachedServers(): array
$servers[] = [
'host' => getenv('MEMCACHE_HOST' . $num),
'port' => getenv('MEMCACHE_PORT' . $num),
'retryInterval' => 2,
'status' => true,
'timeout' => 2,
'weight' => 1,
];
}

return $servers;
}

/**
* @return array
*/
protected function getOptions(): array
{
if (!extension_loaded('memcached')) {
return [];
}

return [

// Assure that dead servers are properly removed and ...
\Memcached::OPT_REMOVE_FAILED_SERVERS => true,

// ... retried after a short while (here: 2 seconds)
\Memcached::OPT_RETRY_TIMEOUT => 2,

// KETAMA must be enabled so that 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 => self::TIMEOUT_IN_MILLISECONDS, // milliseconds
\Memcached::OPT_SEND_TIMEOUT => self::TIMEOUT_IN_MILLISECONDS * 1000, // microseconds
\Memcached::OPT_RECV_TIMEOUT => self::TIMEOUT_IN_MILLISECONDS * 1000, // microseconds
\Memcached::OPT_CONNECT_TIMEOUT => self::TIMEOUT_IN_MILLISECONDS, // milliseconds

// Further performance tuning
\Memcached::OPT_NO_BLOCK => true,
];
}
}
2 changes: 2 additions & 0 deletions src/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
// session config
ini_set('session.save_handler', 'memcached');
ini_set('session.save_path', implode(',', $handlers));
ini_set('memcached.sess_locking', 0);

if (getenv('MEMCACHE_COUNT') == 2) {
ini_set('memcached.sess_number_of_replicas', 1);
ini_set('memcached.sess_consistent_hash', 1);
ini_set('memcached.sess_binary', 1);
ini_set('memcached.sess_remove_failed_servers', 1);
}
}

0 comments on commit 5f0cff8

Please sign in to comment.