There are different types of caching like browser, page, server-side, CDN, and object caching. Object caching is important to make your database queries run faster and ultimately improve your website speed.
In this article, we’re going to explore object caching and dive into one of the most popular object caching systems, called Memcached.
You shouldn’t confuse Memcached with memcache, which is a PHP extension created for the Memcached caching service.
What is Object Caching?
Object caching involves storing database query results so that the next time a user needs a result, it can be served from the cache without repeatedly querying the database.
As a Content Management System, WordPress is naturally and heavily dependent on the database. As such, database efficiency is crucial to scaling WordPress.
Let’s say you run a high-traffic site and requests to your pages generate a large number of database queries. Your server can quickly become overwhelmed with this and in turn negatively affect your site’s performance.
So, enabling object caching can help ease the load on your database and server and deliver queries faster.
WordPress Object Caching
The built-in object caching in WordPress saves a copy of complex queries and stores their results in a database table.
The database stores the most frequently used queries running on the pages of your site. This copy of the requests reduces the load time and improves your website’s performance.
If object caching is functional, your server won’t have to regenerate query results every time. It can use the object caching layer previously created to deliver results.
You can use different technologies like Memcached, Redis, and APC to store an object cache.
What is Memcached?
Memcached is an open-source memory caching system built to ease database load for dynamic web applications or websites that need login/registration.
Brad Fitzpatrick initially developed Memcached back in 2003. Today, Facebook, Twitter, YouTube, Wikipedia, and other big and small web applications utilize it to their advantage.
Its developers define Memcached as an in-memory key-value store for small arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Memcached uses a client-server architecture based on 4 components:
- Client-server: It retains the list of available Memcached servers
- Client-based hashing algorithm: It picks up a server based on the requested “key.“
- Server software: It stores the combinations of values + key into an internal hash table
- Least Recently Used (LRU) algorithm: It decides when to use old data or the memory
How Memcached works?
Memcached works like any other caching system, but with the database at the core of the process. Let’s see how Memcached works in five quick steps:
- The client-server receives a query from a user
- 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. At the same time, the Memcache index gets updated with the latest values. Once the latest values get updated, the cache is ready for use in the future as observed in step 3.
Usually, you set up Memcached via different Memcached servers and clients. These servers and clients help to distribute the load of the requests.
The client-server uses the hashing algorithm to ascertain which Memcached server should it forward the request to.
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.
Using Memcached on your WordPress website
The first condition to use Memcached on your WordPress site is that your hosting server should have it installed.
The second condition is that your web application or website can support Memcached.
Memcached doesn’t require too many CPU resources since it is solely relying on RAM.
A few web hosting services come gave Memcached system pre-installed on their cloud servers.Siteground, A2 Hosting, or Cloudways are examples of hosting services that have pre-installed Memcached systems.
If your server supports Memcached, you can most likely use it through the pre-built Memcached PHP extension.
Advantages of using Memcached on your WordPress website
An important advantage of Memcached is that it stores all the information in the RAM. This implies that there’s no need to load it 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 like documents, images, and much more complex structures.
Moreover, depending on the usage environment you set up, the failure of one of the Memcached servers isn’t usually critical.
In fact, more often than not, servers use Memcached as a read-only cache or to hold temporary information. If you’re using it for persistent data, you can switch to an alternative lookup method which reloads the data into the RAM of a different server.