Acquia’s Memcached solution is comprised of the following four major pieces: The Memcached daemon, the PHP Memcached library, the Memcache API and Integration module, and Drupal core.
For introductory information about Memcached, see Using Memcached. For instructions on how to enable Memcached, see Enabling Memcached on Acquia Cloud.
The Memcached daemon is installed as a service with several components that work together to ensure stability and uptime for a website.
Memcached is a key-value store, which is a data storage paradigm designed for storing, retrieving, and managing associative arrays—commonly known as a hash. This contrasts with relational database object models that enable advanced concepts such as mapping and data types.
The Memcached service includes the following commands for handling data:
get
- Retrieve an existing item from storage.set
- Save a new item to storage.flush_all
- Mark an item as expired
so that it cannot be
retrieved. This method pushes the current item to the front of the
line, to be replaced by the next cacheable item. It does not free the
memory used by that item.delete
- Remove an item from storage.stats
- Query the service for aggregated data about the objects
saved to storage.Note
Environments on Cloud Next technologies have access to alternatives to mcstat with these syntaxes:
acquia-memcache stats
acquia-memcache flush_all
When the Memcached service is started, it reserves memory for object storage (by default, 64 MB). The memory allocated is divided into 1MB pages, which by default provides 64 pages. Memcached initializes a memory organization concept called a slab class on startup. A slab defines the number of items (chunks) which can be inserted into a 1MB memory page. By dividing 1MB by that number of chunks, you can determine that slab’s chunk size.
By default, Memcached creates 42 slabs and assigns a single page to each. Each successive slab holds fewer, larger chunks based on the factor (by default, 1.25). In the following sample representation of slabs, notice that the item size (or chunk size) increases by a factor of 1.25 from the second to the third slab:
# Item_Size Max_age Pages Count Full? Evicted Evict_Time OOM
2 120B 1168824s 1 170 no 0 0 0
3 152B 1831103s 1 196 no 0 0 0
4 192B 1847211s 1 988 yes 30 9183 0
...
36 246.8K 27236s 6 21 yes 7 2676 0
37 308.5K 1224s 2 3 yes 91 29174 0
...
40 602.5K 1696s 11 4 yes 0 0 0
41 753.1K 64s 4 1 yes 6 35 0
42 1024.0K 44944s 5 1 yes 20 1 20
Memcached saves items by evaluating an item’s size, and then writing it to a slab. In the previous example, note that slab 36 contains items that range in size from 246.8KB to less than 308.5KB.
When a slab cannot fit a new item and there is additional memory available to Memcached, a new page will be assigned to the slab. In the previous example, slab 36 can contain at most four items, but, depending on the items’ size, may contain only three items:
When the set()
method is called, Memcached determines the size of
the object, and then locates the slab with the appropriate memory
allocation.
Each Memcached node is configured to manage its memory with Least Recently Used (LRU) prioritization. The nodes oversee their local slab arrays, producing a list of candidates for removal. When a new item is assigned to a full slab and there are no free pages to assign to that slab, the full slab evicts the LRU item from the same slab. This is both healthy and accepted behavior—assuming that the objects being evicted are outdated and due to be safely removed.
Memcached does not internally provide mechanisms for adding capacity through additional processes. Instead, this functionality is handled entirely by an external client, such as the PECL Memcache extension for PHP.
An item cached by Memcached is stored in a specific location on a single infrastructure, even if your subscription includes multiple Memcached infrastructure.
If a Memcached infrastructure fails, its cached items will no longer be available, and requests for these items will be rerouted to the database layer of your application. Memcached cannot detect failed infrastructure, and will continue to route requests to all infrastructure, resulting in slow or failed requests until the failed infrastructure is repaired or removed from service. Although healthy Memcached infrastructure in your subscription will continue to respond normally, responses may slow or fail if the data source becomes overloaded.
Important
If Memcached must be restarted on a single node, all Memcached
nodes for that cluster must be reset. If this doesn’t occur, multi_item
objects contain incomplete data.
Environments running on Cloud Next technologies have at least two Memcached nodes running at all times for improved resiliency. If one of these nodes becomes impaired, total Memcached capacity is reduced and impacted cache items need to be read from the database at least once. Acquia leverages mcrouter to handle this transparently without changing to the Drupal application. Any impaired Memcached nodes are replaced automatically. All Memcached nodes operate with the Memcached memory limit configured on a given environment. If 64 MB of memory is allocated, the combined total memory available across both nodes is 128 MB.
Drupal 9 or later provides additional caching flexibility not available in Drupal 7. Instead of caching the entire page, caches in Drupal 9 or later are saved in bins, each of which can be set to cache information in the database, or with Memcached.
Acquia recommends you use Memcached for most cache bins, as it is preferable to local caches, such as PHP’s APCu cache, since it makes cached items available to all layers of PHP.
Acquia’s recommended default Memcached configuration is available for download
and
configures your website to use Memcached for the following bins:
// Override default fastchained backend for static bins.
// @see https://www.drupal.org/node/2754947
$settings['cache']['bins']['bootstrap'] = 'cache.backend.memcache';
$settings['cache']['bins']['discovery'] = 'cache.backend.memcache';
$settings['cache']['bins']['config'] = 'cache.backend.memcache';
// Use Memcache as the default bin.
$settings['cache']['default'] = 'cache.backend.memcache';
You can configure additional bins to use a Memcached back end, such as:
For advanced information about cache bins, see the configuration section of the Drupal Cache API documentation on Drupal.org.
Important
Content Hub 2.x requires the Depcalc module which needs to use the database backend.
$settings['cache']['bins']['depcalc'] = 'cache.backend.database';