This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

This holiday season, maximize your sales through enhanced website performance and 24/7 caring support. SAVE 30% NOW

Use Memcached to Reduce Database Load on WordPress Website

Updated on  11th September

6 Min Read
Memcached For WordPress
Reading Time: 6 minutes

Memcached is one of the caching mechanisms that reside on your hosting server. It mainly deals with the database queries that help in reducing the database load resulting in a fast loading web page. If your website/store relies heavily on database queries, using Memcached for WordPress website would significantly improve the performance and reduce the page load time.

The internet giants including YouTube, Reddit, Facebook, Twitter, and Wikipedia are using Memcached to boost the page load time. Google App Engine, Microsoft Azure, IBM Bluemix and Amazon Web Services also offer a Memcached service through an API. Considering its importance in boosting page load time, we (Cloudways) offer pre-installed Memcached on our cloud servers. However, sometimes you may need to configure your application (WordPress) to take full advantage of Memcached.

What is Memcached?

According to the official statement, Memcached is a:

“Free and open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.”

In a simple term, Memcached is a distributed temporary object caching system that stores strings and objects in the server’s RAM resulted from processing the database queries, API calls or page rendering. The servers used for this mechanism can be called as Memcached servers.

What is Memcached Used For?

Memcached is used for speeding up dynamic web applications like ecommerce stores, registration/login websites, etc. by reducing database load. It stores the processed result so whenever a visitor requests the same query again, Memcached can respond to that instead of processing the query and reply. By keeping the server less busy, your visitors will experience a faster loading times and better user experience.

There is an interesting and funny real-world story at the GitHub, give it a read to understand the typical use case of Memcached.

How Does Memcached Work?

Every time a visitor/browser requests the server that requires the database to process and respond, it adds load to the server. Memcached reduces that load by storing data objects in temporary dynamic memory. It saves the data on key-values and checks its memory before sending the browser’s request to the database. If a cache is present, it replies to the request without involving the database to process.

As discussed above, Memcached is a distributed memory object caching system that consists of four major components:

  • Client Software: Receives a list of distributed available Memcached servers.
  • Client-Based Hashing Algorithm: Chooses the server based on key-values.
  • Server Software: Stores data (values) and keys into an internal hash table.
  • Server Algorithms: Identifies when to delete the old data and reuse memory.

The above components allow Memcached to store and retrieve data. Each item consists of a:

  • Key
  • Expiration time
  • Raw data

When an item is requested, Memcached validates its expiration time to see if the item is still valid before replying to the request. If a cache is not present, it sends the request to the database to process and store the result as an item.

If a server becomes out of the memory, it looks and replaces the expired items. If it still needs any information, it looks for the items not requested for a particular period. By this, Memcached keeps more recently requested information in dynamic memory.

At a high-level Memcached work as follows:

  1. Memcached checks the requested data if it is stored in the cache.
  2. Two possible outputs:
    • Data is stored in cache: Return the requested data from Memcached without interrupting the database.
    • Data isn’t stored in cache: Execute the requested query, process and retrieve the data, and save the result in memory.
  3. Whenever there is an update on an item, or an item is expired, Memcached updates its cache and makes sure the fresh content is delivered to the requested client.

How to Use Memcached?

There are two steps involved in using Memcached. First, it must be installed on your hosting server and then your application should be able to support Memcached.

The installation and configuration process is described below:

Install Memcached on Server

Memcached does not require a lot of CPU resources. It is purely dependent on the RAM. If you have a web server of 8GB RAM, but the OS and your websites consume only 4GB, then it is a good idea to assign rest of the RAM to Memcached instances to increase its storage capacity. Give a read to all technical requirements here.

Memcached works on most of the Linux-based servers, installing Memcached is just a two Linux commands away. Generally, you should install Memcached from a package provided by your OS (Debian, Ubuntu, etc.). The Operating System will solve dependencies for you and take care of security updates.

For Debian or Ubuntu users:

Command: apt-get install memcached 

For Redhat/Fedora:

Command: yum install memcached 

For more information, see the documentation of your respective OS. It is worth mentioning that Nginx, one of the most popular reverse-proxy server comes prepackaged with the Memcached module providing the most flexible Nginx Memcached bundled solution.

Check If Memcached Is Running

There are multiple ways to test if Memcached is running. One of them is using Telnet, log in to your server SSH terminal and write below command:

or

P.S: The above command will run if the local server is your Memcached server.

If the above command runs successfully, you will get the below result:

Otherwise, you will get a connection error.

To check some basic stats you can use below command: stats

You will get something like below:

To check the items, use: stats items

Result:

To check current memory statistics write:  stats slabs

Result:

To quit the connection type quit and press enter.

There is a list of some useful commands to run and inspect Memcached servers that can be found here.

Memcached for Cloudways Users

Cloudways users do not have to worry about all the technicalities related to Memcached. All servers launched on Cloudways Platform came pre-installed and activated Memcached.

Navigate to your respective Server → Manage Services, and you can see Memcached is pre-installed and activated by default.

Memcached WordPress

Not a Cloudways customer but want to take advantage of Memcached? Get a FREE trial without providing your credit card details.

Memcached WordPress Plugin

There are plenty of WordPress cache plugins available on the market. Most of them support Memcached like SG Optimizer and W3TC, one of the widely used cache plugins allow you to configure Memcached. If you are one of the W3TC users, navigate to the General Settings tab and select Memcached from the dropdown, where it says:

  • Page Cache
  • Minify Cache
  • Database cache

Save all settings and do not forget to purge the cache once.

Final Thoughts

Memcached is considered as one of the ways to speed up a WordPress site if configured properly as it reduces the database load and keeps the server less busy resulting in better performance and page load time.

Share your opinion in the comment section. COMMENT NOW

Mustaasam Saleem

Mustaasam is the WordPress Community Manager at Cloudways - A Managed WordPress Hosting Platform, where he actively works and loves sharing his knowledge with the WordPress Community. When he is not working, you can find him playing squash with his friends, or defending in Football, and listening to music. You can email him at mustaasam.saleem@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.