Configuring Memcache with Drupal 8 to reduce database load
Developers often come across a situation where they are required to reduce database load by caching DB objects in RAM. Here Memcache improves Drupal application performance by moving standard caches out of the database and by caching the results of other expensive database operations.
Note that Drupal doesn’t support Memcache by default, and for this, we need to install it on the server. Let’s see how to install Memcache on the server and configure it with Drupal 8 to reduce the load on the database with every page load.
Let’s see how to install Memcache on server
Open the terminal on your local machine and run the following codes:
Step 1: sudo apt-get update Step 2: sudo apt install memcached Step 3: sudo apt install php-memcached
Step 4: Make sure Memcache daemon is working fine by running the following command:
“ps aux | grep memcached”
Step 5: Also, check whether the Memcached extension is properly configured in PHP7 by creating info.php page.
And enter the below code
Step 6: Now restart both Memcached and php7-fpm services
service memcached restart service php7.0-fpm restart
Step 7: Go to any web browser and access info.php as
“localhost/memcache/info.php”, and search for “Memcache” you will see the screen similar to the one mentioned below.
Configuring Memcache with Drupal 8:
Open your site settings.php and paste the below code. Here we are using Memcache storage for Drupal 8 as it provides an integration of D8 and Memcached PECL.
// Set’s default cache storage as Memcache and excludes database connection for cache $settings['cache']['default'] = 'cache.backend.memcache_storage'; // Set’s Memcache key prefix for your site and useful in working sites with same memcache as backend. $settings['memcache_storage']['key_prefix'] = ''; // Set’s Memcache storage server’s. $settings['memcache_storage']['memcached_servers'] = ['127.0.0.1:11211' => 'default'];
To debug Memcache include below code following above code in settings.php
// Enables to display total hits and misses $settings['memcache_storage']['debug'] = TRUE;
Note: Before uninstalling Memcache and Memcache storage, comment/delete Memcache settings code from your settings.php file.
That’s it! If you are planning to configure Memcache with Drupal, you need to look out for three things: which cache bins will be offloaded with Memcache; website traffic and volume of content; and the amount of RAM allocated to Memcache.
Now you know how to configure Memcache with Drupal 8 to reduce database load. Go ahead and try it on your own website. In case you need any help in installing and configuring Memcache with Drupal 8, do leave a comment below and I will get back to you ASAP!
Below given is a presentation on "How to install Memcache on the server and configure it with Drupal 8."