Speed up Drupal 8 sites with Memcached and reduce the database load

Speed Up
Speed Up Drupal 8

Speed up drupal 8 site with memcached

To speed up website, sometimes choosing correct caching is key to success. The main motive of the memcached is to reduce the database load and increase the website performance. Memcached is using RAM to store the data and hash table which eventually decrease the load on database and API.

Memcached system is working on TCP protocol which means you can able to install it on a separate server and store high amount data to RAM.

Most important thing about memcached is if memcached is running out of space then older data gets overwritten and replaced by newer data. It will work as non-persistent caching. 

Memcached will work on default port 11211 which can be change to any other port.

In this tutorial, we would cover the below points which are going to help you to understand the Memcached working and also help to speed up your website.

  • Advantages of Memcached
  • Disadvantages of Memcached
  • When we used Memcached?
  • Installation and Configuration of Memcached

Advantages of Memcached

1. It is an open-source cache system which means it’s freely available.
2. It will work as a cross-platform.
3. It will also handle the sessions.
4. it is a distributed memory system.

Disadvantages of Memcached

1. Data will not be persistent means data will be flush out if you restart the service.
2. It will be using only 1/4 of the size of system RAM, so if RAM size out of space then data is overwritten.

When we use Memcached?

We will use Memcached to decrease the load on the database and speed up website by caching database queries which are most frequently hit or when there is a high traffic on the website.

Installation & Configuration of Memcached

Let’s install and configure Memcached to speed up the website. 

Step 1: Install Memcached Package

Run below command to install the Memcached and libmemcached-tools package on your server.

$ apt-get install memcached libmemcached-tools php7.2-memcached

Step 2: Check memcached.conf configuration

After installation of the Memcached package,  If you are running Memcached on separate instance then you need to change bind IP from 127.0.0.1 to server internal IP address in memcached.conf file and also you can set the memory size in the same file.

vim /etc/memcached.conf

# memcached default config file
# 2003 - Jay Bonci <[email protected]>
# This configuration file is read by the start-memcached script provided as
# part of the Debian GNU/Linux distribution.

# Run memcached as a daemon. This command is implied, and is not needed for the
# daemon to run. See the README.Debian that comes with this package for more
# information.
-d

# Log memcached's output to /var/log/memcached
logfile /var/log/memcached.log

# Be verbose
# -v

# Be even more verbose (print client commands as well)
# -vv

# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 512

# Default connection port is 11211
-p 11211

# Run the daemon as root. The start-memcached will default to running as root if no
# -u command is present in this config file
-u memcache

# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
-l 127.0.0.1

# Limit the number of simultaneous incoming connections. The daemon default is 1024
# -c 1024

# Lock down all paged memory. Consult with the README and homepage before you do this
# -k

# Return error when memory is exhausted (rather than removing items)
# -M

# Maximize core file limit
# -r

Step 3: Enable Memcached Service and Restart the service 

Use below command to enable the Memcached service at boot time and also restart the service to reflect Memcached configuration changes.

  • Enable Memcached Service

    $ systemctl enable memcached
  • Restart Memcached Service

    $ /etc/init.d/memcached restart

Step 4: Configure Memcached with Drupal 8

After installation and configuration of Memcached on the server, download the drupal Memcached module on the server under the “sites/all/modules” directory.

$ cd /var/www/html/website/web/sites/all/modules/
$ wget https://ftp.drupal.org/files/projects/memcache-8.x-2.1.tar.gz
$ tar -xvf memcache-8.x-2.1.tar.gz
$ rm -f memcache-8.x-2.1.tar.gz
Change Directory Permissions
$ chown -R www-data:www-data memcache
$ find -type d -exec chmod 775 {} ;
$ find -type f -exec chmod 664 {} ;

Go to “https://example.com/admin/modules” and enable the modules.

Memcached Module

Step 5: Change in settings.php

After saving the configuration in the admin panel, you will get some error of settings.php file is not configured, etc. just ignored the error and come to the server then open settings.php file which resides under “web/sites/default/” directory.

Add below provided Memcached code to the end of settings.php file. This code helps to integrate Memcached with the drupal caching system.

vim settings.php

$settings['memcache']['servers'] = ['127.0.0.1:11211' => 'default'];
$settings['memcache']['bins'] = ['default' => 'default'];
$settings['memcache']['key_prefix'] = '';
$settings['cache']['default'] = 'cache.backend.memcache';
$settings['cache']['bins']['render'] = 'cache.backend.memcache';

Now you guys know how to configure Memcached with Drupal 8 to reduce database load and speed up the website. Give it try for your own website.

In case you need any help in installing and configuring Memcache with Drupal 8, do share with us, or do you have questions or thoughts to share about this guide? If yes, reach us via the comment section below.

Prashant

Welcome to Linuxgrow. I'm Prashant, a tech-blogger from Mumbai, India. I started Linuxgrow as a passion and to share my knowledge about technologies. Here at Linuxgrow, I write about Linux technologies, Aws Cloud, Wordpress blogging and scripting knowledge. You can read more about me at About us page. Thank You :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook