How To Configure Magento 2 To Use Memcached

by Fayyaz Khattak  April 30, 2018

Memcached is a distributed memory cache that speeds up large-scale web applications having dynamic databases. It reduces the load of database requests on a server and displays data through the cache whenever an external data source requests it.

Magento 2 Memcached

What is Memcached?

Let’s take an example to understand Memcached better.

Suppose, you own a DVD store with lots of movies of different genres like action, drama, and comedy. You assign labels to the shelves that have the demanding movies and place them on the front counter.

Now, whenever a customer wants a movie, he will immediately reach the designated shelf with the help of the labels.

The result? Boosted response rate and decreased load on the store.

Memcached-enabled websites work in precisely the same way.

Let’s learn how you can install Memcached on your Magento 2 store in simple steps.

Install Memcached on Server

PHP has no native support for Memcached, which means you will need to go through a lot of hurdles to install Memcached extension for PHP to use it on your server.

But, if you become a Cloudways user, you won’t have to worry about a thing, as Cloudways takes care of all these hurdles and helps you enable Magento 2 Memcached easily. That’s why I would recommend you to signup on Cloudways Platform and activate your account.

In this tutorial, I will use Cloudways to configure Magento 2 for using Memcached.

Verify Memcached Running on the Server

First of all, check if Memcached is already running on your system. To do so, type the following command in the terminal:

ps aux | grep memcached

Or, try running the following command:

netstat -ap | grep 11211

Now, create a phpinfo.php page on the root directory of your application and enter the following code into it:

<?php phpinfo(); ?>

Open this page in your web browser and search for the term ‘Memcached’ and make sure it displays as follows:

Memcached

Now that Memcached is working on the server let’s see how to configure Magento 2 to use Memcached.

Setup Memcached with Magento 2

Magento 2 supports Memcached for caching many objects. It isn’t enabled by default, so you will have to make simple changes to your $Magento2Root/app/etc/env.php file to enable it.

In this env.php file, you will see a large number of PHP arrays with different settings and configurations. Open the env.php file in your favorite code editor and search for the following:

Modify the above code as follows:

Save your changes to the env.php file, and now, we have to verify the Memcached Magento 2 configuration in the following way.

Magento 2 Memcached Verification

To verify that Magento 2 has been configured appropriately to use Memcached, run the following command in the terminal and delete the content of some directories under your Magento 2 installation:

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

Open your Magento 2 store in a browser and start browsing different pages to see if some page breaks down. Login to the admin panel as well to see if any error pops up. If so, it means Memcached has been configured with Magento 2 store, and it is working correctly.

If you face any processing or HTTP 500 (Internal Server Error) error, make sure that Memcached is running on the server, and that no errors exist in the env.php file.

Conclusion

Depleting performance during peak sessions is a primary issue for many ecommerce stores. To overcome it, Magento 2 has provided a few caching technologies that enhance its performance. Memcached is one of these caching mechanisms that have the power to handle complex data caching processes. It is a distributed memory caching system and deploys easily with any Magento 2 store. It comes with a dynamic backend mechanism that efficiently handles cache data.

If you face any issues while configuring Magento 2 Memcached, feel free to contact me or comment below.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways – A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at m.fayyaz@cloudways.com

Stay Connected:

You Might Also Like...