The PECL Memcached extension provides a PHP-based API that can communicate with the Memcached service. The classes and functions in the extension accomplish the following tasks:
- Consume the list of configuration and connection details of the infrastructure running Memcached from the Drupal Cache API configuration.
- Receive directives from Drupal’s Cache API, such as
cache_set()
andcache_get
and translate them into commands that can be interpreted by the Memcached service. For more information about Drupal integration, see Drupal and the Memcache module.
Infrastructure and scaling
Memcached infrastructure in a hash ring is unaware of other nodes within the ring. Each infrastructure knows which data in its local array is least recently used and will purge that data accordingly to make room for newer requests.
At the beginning of a request, Drupal uses the Memcache API and Integration module to pass all connection information to the PECL Memcached PHP extension. This includes an array of available infrastructure. For example:
web-1234, web-1235, web-1236
Each of these host names are hashed and converted to an integer, decreasing the likelihood that the infrastructure will appear in alphanumeric, sequential order. By doing this, the process increases the likelihood of webs being distributed evenly in the hash ring. The array of infrastructure is passed to the Memcached service, which maps each value to a hash ring. Memcache does this several times per infrastructure, using different hashing parameters, with the goal of creating an even distribution of infrastructure endpoints throughout the loop. After all hostname points have been mapped, objects are encoded using the following:
$full_key = urlencode($prefix . $bin . '-' . $key);
This creates a key in the following format:
examplesite_-cache_page-https%3A%2F%2Fwww.example.com
The objects are hashed using the crc32 hashing algorithm and mapped in a similar fashion.
Infrastructure and objects are mapped to the hash ring with an absolute value, allowing the Memcached service to quickly determine where they exist. If any infrastructure in the loop or array are removed, the website will not lose all of its cached data and the PECL Memcached extension will evenly distribute the load amongst the remaining parts of the infrastructure. For information about Memcached architecture in Cloud Classic and Cloud Next, see How is Memcached architected differently in Cloud Next?.