Skip to main content

Memcached Explained

· 6 min read
Shing Kuo

Memcached

In the previous article, we discussed about Redis and this time around, we will take a look at Memcached which is another popular Object-caching system.

We will also take a look at implementing it in WordPress and do a brief comparison with other Object Caching Systems. In a future, we will also do a full comparison between Redis and Memcached to find out which is superior in different scenarios.

What is Memcached?

Memcached is an open-source, high-performance, distributed memory object caching system. It is fundamentally an in-memory key-value store for small arbitrary data, such as strings and objects. This data can be the results of database calls, API calls, or page rendering.

Originally developed by Brad Fitzpatrick for his website, LiveJournal, in 2003, Memcached is now used by many high-traffic websites, including platforms like Facebook, Twitter, YouTube, and Wikipedia. The primary goal of Memcached is to enhance the speed of dynamic web applications by alleviating database load.

How does Memcached Work?

Memcached operates based on a client-server architecture. The process starts when the client-server receives a query from a user. The client-server then checks with the Memcached server if the required data is already stored in its memory.

If the data exists, Memcached directly returns it to the client server, which significantly reduces the time taken to get the data. If the data isn't already saved in the cache, Memcached forwards the request to the database. The requested data is then forwarded to the client-server and, at the same time, the Memcache index is updated with these latest values. This makes the cache ready to be used in the future.

This process of storing data in memory reduces the number of direct reads from the database, thereby speeding up the application. Memcached is multithreaded, which means that it can handle multiple connections from multiple clients simultaneously.

Features of Memcached

Memcached is known for its simplicity and efficiency. It uses a very simple design that allows you to fully utilize the capabilities of your server. Here are some of the major features of Memcached:

  1. Distributed Caching: Memcached allows you to distribute your cache across multiple servers. This means that as your website grows, you can simply add more servers to scale your cache size.

  2. Automatic Data Removal: Memcached automatically removes older data with its Least Recently Used (LRU) algorithm. This means you don’t have to manually manage the cache size.

  3. Hashing Algorithm: Memcached uses a client-side hashing algorithm for distributing keys over servers. If a server goes down, the clients can continue to operate, and the keys are distributed to the remaining servers.

  4. Multithreading: Memcached can handle multiple client connections concurrently due to its multithreaded nature.

  5. Non-Blocking IO: Memcached uses non-blocking input/output, which allows it to handle thousands of items concurrently.

How to use Memcached in WordPress

To use Memcached in WordPress, you need to have the Memcached server and PHP Memcached extension installed on your server. Once these are set up, you can use a WordPress plugin that supports Memcached, such as W3 Total Cache or WP Rocket.

These plugins will allow you to easily enable Memcached for your WordPress site and take advantage of its benefits. They provide an easy-to-use interface for managing your cache, and they can also handle other performance optimizations, such as minifying your HTML, CSS, and JavaScript files.

The Impact of Memcached on Website Performance

By caching database query results, Memcached can significantly speed up a website. This is particularly beneficial for websites that receive a large number of requests to the database. By reducing the need for your server to continuously regenerate queries’ results, Memcached can help to make your website more performant.

The impact of Memcached on website performance can be quite significant. For example, a website that previously took 5 seconds to load could potentially load in under 2 seconds with Memcached. This can lead to a better user experience, improved SEO rankings, and increased conversion rates.

However, it's important to note that the benefits of Memcached will vary depending on the specifics of your website. Websites with dynamic content or high traffic will typically see the most improvement.

Memcached vs Other Caching Systems

While there are other popular caching systems like Redis and APC, Memcached stands out for its simplicity and efficiency. It is particularly well-suited to websites with high traffic and dynamic content.

Redis, for example, is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. It supports various types of data structures, such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, and geospatial indexes with radius queries. Redis also supports replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence.

APC (Alternative PHP Cache) is another popular caching system. It provides an opcode cache, which caches the compiled bytecode of PHP scripts to avoid the overhead of parsing and compiling source code on each request. APC also provides a user cache for storing application data.

While each of these systems has its own strengths, Memcached is often praised for its sheer simplicity. It doesn't have the advanced features of Redis or APC, but it does one thing extremely well: caching key-value data in memory. This makes it an excellent choice for applications that need a simple, fast caching solution.

Summing Up

Memcached is a very powerful tool that can significantly improve the performance of your website. By storing key-value data in memory, it can reduce the load on your database and make your website faster and more responsive. However, like any tool, it's important to use Memcached correctly. Misusing Memcached or using it for inappropriate tasks can lead to problems. Therefore, it's important to understand how Memcached works and what it's best used for before integrating it into your application.

This article has provided a brief overview of Memcached, including what it is, how it works, its features, and how to use it in WordPress. For more detailed information, you can refer to the official Memcached documentation or other online resources. Remember, while Memcached can significantly improve your website's performance, it's important to monitor your website regularly to ensure it is running optimally.