The secrets of Memcached: the object caching system to boost your web performance

How many types of caching are you familiar with? Which ones do you use on your WordPress site?

If you’re using a WordPress caching plugin like WP Rocket, you’re probably making use of its browser and page caching features. And if you also use its advanced caching options, your site also benefits from user and mobile caching.

There is also server-side caching (with tools like Varnish or NGINX) and CDN caching: the first depends on the host you use, and the second on the CDN service you choose.

The last type of caching you can add is object caching: it makes database queries faster and, therefore, improves your website’s speed.

In this article, we’ll take a look at the secrets of object caching in detail and dive into one of the most popular object caching systems: Memcached.

Memcached is not to be confused with Memcache, a PHP extension created for Memcached’s caching service, which is not part of what we will explore in this article.

Object caching explained in simple words

Before we explain how Memcached works, let’s go over what exactly object caching is.

You know that page caching is all about caching static resources (images, HTML, CSS, and JS files), and browser caching takes care of the same resources within the browser. Object caching is dedicated to caching your database.

An efficient database is one of the main assets of a fast website: WordPress is a content management system that naturally depends on its MySQL database.

Every time a user (or crawlers) sends a request to your site, it generates requests to your database. If your site receives a large number of requests to the database, they can quickly add up, saturating your server and slowing your site down.

The good news is that WordPress had already released its object caching class in 2005. The class named WP_Object_Cache was then integrated into the heart of WordPress.

What does WordPress object caching do?

WordPress’s built-in object caching saves a copy of complex queries and keeps its results in a table (i.e., an HTTPS redirect).

The most frequently used queries in your site’s pages are therefore saved in the database: this copy of the queries reduces loading time and makes your site more efficient.

This process is not exclusive to WordPress sites: all sites where an object caching system has been set up work in the same way.

For example, Drupal, another popular content management system, offers a similar built-in object caching system.

If object caching is working, your server won’t need to generate query results every time. It will just need to use the object cache layer created beforehand.

To store an object cache, you have several options available to you. Among the most popular object caching engines are Memcached, Redis, and APC.

What is Memcached?

Memcached is an open-source in-memory system cache developed to facilitate database loading for dynamic web applications such as e-commerce stores or sites where identification is required.

Brad Fitzpatrick developed Memcached in 2003. It is now used by Facebook, Twitter, YouTube, Wikipedia, and other web applications of varying sizes.

This system saves key-value associative data for strings and objects like:

  • API calls
  • Rendering pages
  • Database call results

Memcached uses a client-server architecture based on four elements:

  • A client-server, which keeps the list of available Memcached servers
  • A hash algorithm, which chooses a server based on the required “key”.
  • A server application, which saves key-value combinations in an internal hash table
  • A Least Recently Used (LRU) algorithm, which decides when to use old data or memory

How Memcached Works

Memcached works like other caching systems but with the database at the heart of the process.

Here is its process in 5 quick steps:

  • The client-server receives a request from a user (such as accessing the specific URL of a website with an HTTPS redirect);
  • The client-server checks with the Memcached server whether the required information is already stored there;
  • If the information exists, Memcached sends it directly to the client-server;
  • If the information is not already saved in the cache, Memcached sends the request to the database;
  • The required information is then transferred to the client-server while, at the same time, the Memcached index is updated with this new data: the cache is ready to be used in the future (see step 3).

Memcached is implemented through different Memcached servers and clients, which helps to distribute the load of requests.

The client-server uses the hash algorithm to choose which Memcached server to forward the request to.

Note that Memcached servers do not share data. The database sends data to one Memcached server at a time.

How to use Memcached on your WordPress site?

The first requirement to use Memcached on your WordPress site is for your host to install it on their server. The second condition is that your web application can use Memcached.

Since Memcached relies primarily on RAM, it does not require a lot of CPU resources.

Many hosting services are provided with the Memcached system installed on the cloud of their servers. Among them, we find A2 Hosting, Cloudways, or even Siteground.

If your server offers Memcached, you can likely use it through the Memcached PHP extension.

What are the benefits of Memcached for your WordPress site?

The biggest advantage of Memcached is that all information is stored in RAM: it does not need to be downloaded from disk every time.

Another advantage is that there is no data restriction. You can use Memcached on a wide variety of data such as documents, images, but especially more complex structures.

Moreover, depending on the usage environment you set up, one of the Memcached servers’ failures is generally not critical. Most of the time, by the way, Memcached is used as a read-only cache or to hold temporary information. If you use it for persistent data, you can return to an alternate search method that reloads the data into a different server’s RAM.

Is WP Rocket compatible with Memcached?

WP Rocket is a page caching plugin and therefore doesn’t take care of object caching. You will not find a specific option for Memcached.

However, you can use Memcached with WP Rocket: you need to install an additional plugin.

Do you want to launch your online business presence? Are you looking for Web Development in Pakistan to upgrade your web design? Count on us to get competing Web Design and Development services.

Leave a comment

Design a site like this with WordPress.com
Get started