Magento executes multiple data and objects from its database when a customer visits and buy something from the store. These objects are defined in Magento core, PHP calls, and different blocks of the page which may reduce the performance of your Magento store.
If you want to optimize the speed of your Magento ecommerce store, one of the recommended solutions is to configure and use the Memcached Magento support.
What is Memcached?
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.
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 configure and run Memcached Magento on your store using 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 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 and run Magento 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:
Now that Memcached is working on the server let’s see how to configure Memcached with Magento 1 and Magento 2.
Configure Magento Memcached
Now, I am going to configure Memcached with Magento 1. I will add the following code to the $Magento1Root/app/etc/local.xml file. This code will proceed right after the <global> tag. An example of the code can also be found in the $Magento1Root/app/etc/local.xml.additional file.
Just copy and then paste the below code into your local.xml file:
<backend>memcached</backend><!-- apc / memcached / empty=file -->
<memcached><!-- memcached cache backend related config -->
<servers><!-- any number of server nodes can be included -->
Keep in mind that the <host> and <port> tags are set as the correct values. Usually, they are set as localhost or 127.0.0.1 and 11211.
Once it’s done, save the local.xml file and remove all files and folders from the $Magento1Root/var/cache directory.
Magento Memcached Verification
I recommend verifying Magento Memcached configuration to make sure your store works perfectly. Doing so will take only a few minutes and if you face an issue, you can simplify troubleshooting later.
Just check your Magento store and see if it is working or not. If your Magento store fails to load frontend, go back and verify the local.xml file that you have placed the code properly. Also, check your web server’s configuration as well.
Configure Magento 2 Memcached
Magento 2 also 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:
'save' => 'files',
Modify the above code as follows:
'save' => 'memcached',
'save_path' => '127.0.0.1:11211'
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.
Depleting performance during peak sessions is a primary issue for many ecommerce stores. To overcome it, Magento 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 store. It comes with a dynamic backend mechanism that efficiently handles cache data.
If you face any issues while configuring Magento Memcached, feel free to contact me or comment below.
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 email@example.com
Start Growing with Cloudways Today.
Our Clients Love us because we never compromise on these