How many types of caching do you know and use on your WordPress site?
If you’re using a WordPress cache plugin like WP Rocket, there’s no doubt you’re already profiting from its browser and page caching features. And if you’re also using its advanced caching options, then your site is enjoying mobile and user caching too.
Then there’s server-side caching (like Varnish or NGINX), and CDN caching: the first depends on the hosting you’re using, while the second on the CDN service you chose.
The last type of caching you’ll want to add to the group is object caching: this is important to make your database queries run faster and finally improve your website speed.
In this post, we’re going to explore the secrets of object caching and dive into one of the most popular object caching systems: Memcached.
Memcached shouldn’t be confused with memcache, which is a PHP extension created for Memcached caching service, and it’s out of the scope of this post.
Memcached And Object Caching Explained In Plain English
Before getting into the details of Memcached, let’s explain what object caching does precisely.
If page caching takes care of caching static resources (images, HTML, CSS and JS files) and browser caching manages the same resources within the browser, object caching is devoted to your database.
The aim of object caching is to cache query results from your database. You can find a detailed explanation of what object caching is on our blog.
An efficient database is one of the crucial factors for a fast website: WordPress is a content management system which is naturally dependent on its MySQL database.
Every time users (or crawlers) make a request on your website, they generate database queries. If your site experiences a large number of requests to the database, queries can quickly pile up, overwhelming your server and slowing down your website.
The good news is the WordPress introduced its object caching class a long time ago: it was 2005 when the class named WP_Object_Cache was implemented into WordPress core.
What Does WordPress Object Caching Do?
WordPress built-in object caching saves a copy of complex queries and stores their results in a database table (i.e., like a HTTPS redirect).
The most frequently used queries running on the pages of your site are stored on the database: this copy of the requests allows to reduce load time and make your website more performant.
Of course, this process is not a prerogative of WordPress sites: the same thing happens on all sites where an object caching system has been implemented.
For example, Drupal, which is another very popular content management system, implements a similar built-in object caching system.
If object caching is working, your server won’t have to regenerate queries’ results every time, but it can use the object caching layer previously created.
To store an object cache, you can use different technologies: among the most popular object caching engines are Memcached, Redis, and APC.
What is Memcached?
Memcached is an open source distributed memory caching system built to ease database load for dynamic web applications like e-commerce stores or websites where login/registration is needed.
Memcached was initially developed by Brad Fitzpatrick in 2003 and is now used by Facebook, Twitter, YouTube, Wikipedia, and other big and small web applications.
Its developers define it as follows:
Memcached is an in-memory key-value store for small arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.(source)
This system stores key-values data for string and objects, such as:
- API calls
- Page rendering
- Results of database calls
Memcached uses a client-server architecture based on four components:
- A client-server, which retains the list of available Memcached servers
- A client-based hashing algorithm, which picks up a server based on the requested “key.“
- A server software, which stores the combinations of values + key into an internal hash table
- A Least Recently Used (LRU) algorithm, which decides when to use old data or the memory
How Memcached Works
Memcached works like other caching systems, but now the database is at the core of the process.
Let’s see the workflow in five quick steps:
- The client-server receives a query from a user (i.e., access to a specific URL of a website with an HTTPS redirect);
- The client-server checks with the Memcached server if the data needed is already stored in its memory;
- If the data exists, Memcached directly returns it to the client server;
- If the data isn’t already saved in the cache, Memcached forwards the request to the database;
- Requested data is now forwarded to the client-server and, at the same time, the Memcache index is updated with these latest values: the cache is now ready to be used in the future (see step 3).
Typically, Memcached is set up via different Memcached servers and clients, which help to distribute the load of the requests.
The client-server uses the hashing algorithm to decide which Memcached server the request should be forwarded.
It’s important to note that Memcached servers don’t share data: so the database sends data only to one Memcached server at a time.
How To Use Memcached On your WordPress Site?
The first condition to use Memcached on your WordPress site is that your hosting server has it installed.
The second condition is that your web application can support Memcached.
Since Memcached is solely relying on RAM, it doesn’t require too many CPU resources.
Several web hosting services come with pre-installed Memcached system on their cloud servers: you can find some examples on A2 Hosting, Cloudways or Siteground.
If your server supports Memcached, it’s very likely that you can use it through the pre-built Memcached PHP extension.
What Are The Benefits Of Using Memcached On Your WordPress site?
The main advantage of Memcached is that all information is stored in RAM: this means it doesn’t need to be loaded from the disk each time.
Another advantage is that there are no data restrictions: you can use Memcached to cache a number of different data, such as documents, images but, above all, much more complex structures.
Furthermore, depending on the usage environment you set up, the failure of one of the Memcached servers isn’t usually critical. Most of the time, in fact, Memcached is used as a read-only cache or to hold temporary information. If you use it for persistent data, you can recede to an alternative lookup method which reloads the data into RAM of a different server.
Is WP Rocket Compatible With Memcached?
Being a plugin focused on page caching, WP Rocket doesn’t take care of object caching. So you won’t find any specific Memcached option.
This doesn’t mean you can’t use Memcached with WP Rocket!
To use WP Rocket and Memcached together, you’ll need to install an additional plugin.