3 This file contains installation instructions for the 8.x-1.x version of the
4 Drupal Memcache module. Configuration differs between 8.x and 7.x versions
5 of the module, so be sure to follow the 7.x instructions if you are configuring
6 the 7.x-1.x version of this module!
11 - Availability of a memcached daemon: http://memcached.org/
12 - One of the two PECL memcache packages:
13 - http://pecl.php.net/package/memcache (recommended)
14 - http://pecl.php.net/package/memcached
16 For more detailed instructions on installing a memcached daemon or either of the
17 memcache PECL extensions, please see the documentation online at
18 https://www.drupal.org/node/1131458 which includes links to external
19 walk-throughs for various operating systems.
23 These are the steps you need to take in order to use this software. Order
26 1. Make sure you have one of the PECL memcache packages installed.
27 2. Enable the memcache module.
28 You need to enable the module in Drupal before you can configure it to run
29 as the default backend. This is so Drupal knows where to find everything.
30 2. Edit settings.php to configure the servers, clusters and bins that memcache
31 is supposed to use. You do not need to set this if the only memcache backend
32 is localhost on port 11211. By default the main settings will be:
33 $settings['memcache']['servers'] = ['127.0.0.1:11211' => 'default'];
34 $settings['memcache']['bins'] = ['default' => 'default'];
35 $settings['memcache']['key_prefix'] = '';
36 7. Edit settings.php to make memcache the default cache class, for example:
37 $settings['cache']['default'] = 'cache.backend.memcache';
38 8. If you wish to arbitrarily send cache bins to memcache, then you can do the
39 following. E.g. for the cache_render bin:
40 $settings['cache']['bins']['render'] = 'cache.backend.memcache';
42 ## ADVANCED CONFIGURATION ##
44 ### Multiple memcache backends ###
46 $settings['memcache']['servers'] = [
47 '127.0.0.1:11211' => 'default', // Default host and port
48 '127.0.0.1:11212' => 'default', // Default host with port 11212
49 '127.0.0.2:11211' => 'default', // Default port, different IP
50 'server1.com:11211' => 'default', // Default port with hostname
51 'unix:///path/to/socket' => 'default', 'Unix socket'
54 ### Multiple servers, bins and clusters ###
56 $settings['memcache'] = [
58 'server1:port' => 'default',
59 'server2:port' => 'default',
60 'server3:port' => 'cluster1',
61 'serverN:port' => 'clusterN',
62 'unix:///path/to/socket' => 'clusterS',
65 'default' => 'default',
73 The bin/cluster/server model can be described as follows:
75 - Servers are memcached instances identified by host:port.
77 - Clusters are groups of servers that act as a memory pool. Each cluster can
78 contain one or more servers.
80 - Multiple bins can be assigned to a cluster.
82 - The default cluster is 'default'.
84 - If a bin can not be found it will map to 'default'.
86 ### Stampede Protection ###
88 Memcache includes stampede protection for rebuilding expired and invalid cache
89 items. To enable stampede protection, add the following config in settings.php:
91 $settings['memcache']['stampede_protection'] = TRUE;
93 To avoid lock stampedes, it is important that you enable the memcache lock
94 implementation when enabling stampede protection -- enabling stampede protection
95 without enabling the Memcache lock implementation can cause worse performance.
97 Only change the following values if you're sure you know what you're doing,
98 which requires reading the memcachie.inc code.
100 The value passed to Drupal\Core\Lock\LockBackendInterface::wait(), defaults to 5:
101 $settings['memcache']['stampede_wait_time'] = 5;
103 The maximum number of calls to Drupal\Core\Lock\LockBackendInterface::wait() due
104 to stampede protection during a single request, defaults to 3:
105 $settings['memcache']['stampede_wait_limit'] = 3;
107 When adjusting these variables, be aware that:
108 - wait_time * wait_limit is designed to default to a number less than
109 standard web server timeouts (i.e. 15 seconds vs. apache's default of
114 If you want to have multiple Drupal installations share memcached instances,
115 you need to include a unique prefix for each Drupal installation in the memcache
116 config in settings.php:
118 $settings['memcache']['key_prefix'] = 'something_unique';
120 ### Key Hash Algorithm
122 Note: if the length of your prefix + key + bin combine to be more than 250
123 characters, they will be automatically hashed. Memcache only supports key
124 lengths up to 250 bytes. You can optionally configure the hashing algorithm
125 used, however sha1 was selected as the default because it performs quickly with
128 $settings['memcache']['key_hash_algorithm'] = 'sha1';
130 Visit http://www.php.net/manual/en/function.hash-algos.php to learn more about
131 which hash algorithms are available.
133 ### Memcache Distribution ###
135 To use this module with multiple memcached servers, it is important that you set
136 the hash strategy to consistent. This is controlled in the PHP extension, not the
139 If using PECL memcache:
140 Edit /etc/php.d/memcache.ini (path may changed based on package/distribution) and
142 memcache.hash_strategy=consistent
144 You need to reload apache httpd after making that change.
146 If using PECL memcached:
147 Memcached options can be controlled in settings.php. Consistent distribution is
148 the default in this case but could be set using:
150 $setting['memcache']['options'] = [
151 Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
156 Memcache locks can be enabled through the services.yml file.
159 # Replaces the default lock backend with a memcache implementation.
161 class: Drupal\Core\Lock\LockBackendInterface
162 factory: memcache.lock.factory:get
164 # Replaces the default persistent lock backend with a memcache implementation.
166 class: Drupal\Core\Lock\LockBackendInterface
167 factory: memcache.lock.factory:getPersistent
169 ## Cache Container on bootstrap ##
170 By default Drupal starts the cache_container on the database, in order to override that you can use the following code on your settings.php file. Make sure that the $class_load->addPsr4 is poiting to the right location of memcache (on this case modules/contrib/memcache/src)
172 $memcache_exists = class_exists('Memcache', FALSE);
173 $memcached_exists = class_exists('Memcached', FALSE);
174 if ($memcache_exists || $memcached_exists) {
175 $class_loader->addPsr4('Drupal\\memcache\\', 'modules/contrib/memcache/src');
177 // Define custom bootstrap container definition to use Memcache for cache.container.
178 $settings['bootstrap_container_definition'] = [
182 'class' => 'Drupal\Core\Database\Connection',
183 'factory' => 'Drupal\Core\Database\Database::getConnection',
184 'arguments' => ['default'],
187 'class' => 'Drupal\Core\Site\Settings',
188 'factory' => 'Drupal\Core\Site\Settings::getInstance',
190 'memcache.config' => [
191 'class' => 'Drupal\memcache\DrupalMemcacheConfig',
192 'arguments' => ['@settings'],
194 'memcache.backend.cache.factory' => [
195 'class' => 'Drupal\memcache\DrupalMemcacheFactory',
196 'arguments' => ['@memcache.config']
198 'memcache.backend.cache.container' => [
199 'class' => 'Drupal\memcache\DrupalMemcacheFactory',
200 'factory' => ['@memcache.backend.cache.factory', 'get'],
201 'arguments' => ['container'],
203 'lock.container' => [
204 'class' => 'Drupal\memcache\Lock\MemcacheLockBackend',
205 'arguments' => ['container', '@memcache.backend.cache.container'],
207 'cache_tags_provider.container' => [
208 'class' => 'Drupal\Core\Cache\DatabaseCacheTagsChecksum',
209 'arguments' => ['@database'],
211 'cache.container' => [
212 'class' => 'Drupal\memcache\MemcacheBackend',
213 'arguments' => ['container', '@memcache.backend.cache.container', '@lock.container', '@memcache.config', '@cache_tags_provider.container'],
219 ## TROUBLESHOOTING ##
223 Failed to set key: Failed to set key: cache_page-......
226 Upgrade your PECL library to PECL package (2.2.1) (or higher).
229 Zlib compression at the php.ini level and Memcache conflict.
230 See http://drupal.org/node/273824
234 A module offering a UI for memcache is included. It provides aggregated and
235 per-page statistics for memcache.
239 ### Memcached PECL Extension Support ###
241 We also support the Memcached PECL extension. This extension backends
242 to libmemcached and allows you to use some of the newer advanced features in
245 NOTE: It is important to realize that the memcache php.ini options do not impact
246 the memcached extension, this new extension doesn't read in options that way.
247 Instead, it takes options directly from Drupal. Because of this, you must
248 configure memcached in settings.php. Please look here for possible options:
250 https://secure.php.net/manual/en/memcached.constants.php
252 An example configuration block is below, this block also illustrates our
253 default options (selected through performance testing). These options will be
254 set unless overridden in settings.php.
256 $settings['memcache']['options'] = [
257 Memcached::OPT_COMPRESSION => FALSE,
258 Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
261 These are as follows:
263 * Turn off compression, as this takes more CPU cycles than it's worth for most
265 * Turn on consistent distribution, which allows you to add/remove servers
268 Other options you could experiment with:
269 + Memcached::OPT_BINARY_PROTOCOL => TRUE,
270 * This enables the Memcache binary protocol (only available in Memcached
271 1.4 and later). Note that some users have reported SLOWER performance
272 with this feature enabled. It should only be enabled on extremely high
273 traffic networks where memcache network traffic is a bottleneck.
274 Additional reading about the binary protocol:
275 https://raw.githubusercontent.com/memcached/old-wiki/master/MemcacheBinaryProtocol.wiki
276 Note: The information on the link above will eventually be ported to
277 the new wiki under https://github.com/memcached/memcached/wiki.
279 + Memcached::OPT_TCP_NODELAY => TRUE,
280 * This enables the no-delay feature for connecting sockets; it's been
281 reported that this can speed up the Binary protocol (see above). This
282 tells the TCP stack to send packets immediately and without waiting for
283 a full payload, reducing per-packet network latency (disabling "Nagling").
285 It's possible to enable SASL authentication as documented here:
286 http://php.net/manual/en/memcached.setsaslauthdata.php
287 https://code.google.com/p/memcached/wiki/SASLHowto
289 SASL authentication requires a memcached server with SASL support (version 1.4.3
290 or greater built with --enable-sasl and started with the -S flag) and the PECL
291 memcached client version 2.0.0 or greater also built with SASL support. Once
292 these requirements are satisfied you can then enable SASL support in the Drupal
293 memcache module by enabling the binary protocol and setting
294 memcache_sasl_username and memcache_sasl_password in settings.php. For example:
296 $settings['memcache']['sasl'] = [
297 'username' => 'user',
298 'password' => 'password',
301 // When using SASL, Memcached extension needs to be used
302 // because Memcache extension doesn't support it.
303 $settings['memcache']['extension'] = 'Memcached';
304 $settings['memcache']['options'] = [
305 \Memcached::OPT_BINARY_PROTOCOL => TRUE,