Magento 2 Tutorials

How To Configure Memcached With Magento 2

Disclaimer: Magenticians does not necessarily agree with the views expressed in this guest post. They are presented to bring to light all diverse views in the Magento and general ecommerce community.

Memcached is a distributed memory caching system which is often used to speed up dynamic database-driven sites by caching data & objects in Memory (RAM) to reduce the reading time of an external data source. It is high-performance, generic in nature, but intended for use in speeding up dynamic websites by alleviating database load.

Memcached provides a big hash table that can be distributed across multiple machines. When the table is overloaded, subsequent inserts cause older data to be purged in the Least Recently Used (LRU) order.

Magento uses Memcached for session storage. I recommend you to use Memcached with Magento for session storage instead of Redis because Redis does not support session locking. PHP has no native support for Memcached. You will need to install a Memcached extension for PHP to use it.

Verify Memcached on the Web Server

Before we start this Memcached Magento tutorial, I assume that you have successfully installed Memcached on your server. First of all, let’s verify Memcached on the web server.

To do so, create a phpinfo.php file on the web server root with the following code.

<?php
phpinfo();
?>

Now browse that page in your web browser. For example:

http://yourwebsite.com/phpinfo.php

Finally, make sure that Memcached displays as follows:

Verify Memcached
If Memcached does not display, restart your web server and refresh the browser page. If it still does not show, verify you installed the php-pecl-memcached extension.

Configure Memcached With Magento 2

In this step, we are going to configure Memcached with Magento 2. You need to add Memcached configuration in the env.php file. In this env.php file, you will find a large number of PHP arrays with different other configurations and settings. Open Magento2_ROOT_Directory > app > etc > env.php in your favorite text editor and locate the following:

'session' => 
array (
  'save' => 'files',
),

Change the above code as follows:

'session' => 
array (
  'save' => 'memcached',
  'save_path' => 'memcached ip or host:memcached port',
),

Once you are done, save the env.php file, and now finally, we will verify whether we have successfully configured Memcached with Magento 2 or not.

Verify Memcached With Magento 2

To verify Memcached configuration with Magento 2, first delete the content of the below directories under your Magento 2 installation directory:

rm -rf var/page_cache/* var/cache/* var/session/*

Now visit any page on the store and log in to your Magento Admin and browse other pages as well and see if no errors display. If so, it means Memcached is working! If you face any error such as HTTP 500 (Internal Server Error) or Processing error, make sure that Memcached is running and configured correctly and env.php has no syntax errors.

Conclusion

Magento supports file-based cache which is not enough for big stores. Memcached is a free and an open source distributed memory object caching system which helps speed up Magento stores by lightening the database load.

Memcached is a simple but a powerful tool. Its simple design promotes the fastest deployment & development and solves many problems facing large data caches.

Subscribe Newsletter

Subscribe to get latest Magento news

40% Off for 4 Months on Magento Hosting + 30 Free Migration