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.

📣 Join the live AMA session with Adam Silverstein on open source and WordPress core! Register Now →

WordPress Memcached: What Is It and How to Use It?

Updated on March 25, 2024

9 Min Read
WordPress Memcached

Struggling with slow-loading web pages? You need Memcached to speed up your WordPress site. But what exactly does Memcached do? Let’s find out in this detailed guide.

Memcached is a powerful caching mechanism that resides on your hosting server, designed to tackle the heavy load of database queries. It dramatically improves page load times and enhances the overall user experience by reducing the strain on your database.

Internet giants like YouTube, Reddit, Facebook, Twitter, and Wikipedia rely on Memcached to boost their page load times. Even major cloud services providers such as Google App Engine, Microsoft Azure, IBM Bluemix, and Amazon Web Services offer Memcached services through their APIs.

And even some managed hosts like Cloudways offer pre-installed Memcached on its managed WordPress hosting cloud servers for page load optimization.

So let’s learn more about Memcached and its benefits for WordPress sites. This blog details how Memcached optimizes database queries, improves performance, and reduces page load times for a seamless user experience.

What Is Memcached?

According to the official Memcached website, 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 simple terms, Memcached is a distributed temporary object caching system that stores strings and objects in the server’s RAM resulting from processing database queries, API calls, or page rendering. The servers used for this mechanism can be called Memcached servers.

Experience Lightning-Fast WordPress Hosting With Cloudways

Take your website to new heights with Cloudways’ high-performance hosting. Unlock speed, reliability, and scalability today.

Understanding Memcached and Object Caching in Simple Terms

Page caching handles the caching of static resources such as images, HTML, CSS, and JS files, and browser caching optimizes resource storage within the user’s browser; while object caching focuses specifically on the database.

Object caching’s primary purpose is to cache query results from the database.

When users or crawlers make requests to your website, it generates database queries. However, a high number of database requests can lead to a substantial accumulation of queries, causing server overload and resulting in slower website performance.

And to address this issue, WordPress introduced its object caching class, WP_Object_Cache, back in 2005. The WP_Object_Cache class acts as a mechanism for caching the results of database queries, significantly improving website performance and reducing the server load.

By leveraging object caching, WordPress sites can effectively optimize their database performance, resulting in faster response times and a smoother user experience. Understanding the importance of object caching, Cloudways provides the Object Cache Pro plugin for free (normally priced at $95) to all users who opt for a server with a capacity of 2GB or higher.

WordPress Redis vs. Memcached

Memcached and Redis are both widely used in-memory databases but have some distinct characteristics.

Let’s explore the comparison between the two:

Memcached Redis
Ease of Use Easy installation and user-friendly interface Easy installation and user-friendly interface
Latency Milliseconds Milliseconds
Open-source Yes Yes
Programming Languages Compatible with popular languages Compatible with popular languages
Engine Multi-thread Single-thread
Scalability Vertical scalability Vertical and horizontal scalability
Key Length Limit 250 characters (1 MB) Supports larger data types (up to 512 MB)
Data Persistence No Yes
Failover Mechanism No Supports data restoration and backups
Use Cases Ideal for caching and session storage Ideal for complex processes and multitasking

In summary, Memcached and Redis are both easy to install and use, offer low latency, and are compatible with popular programming languages. Memcached is suitable for caching and session storage purposes, while Redis excels in handling complex processes and multitasking.

Memcached is multi-threaded, making it suitable for handling large quantities of data, while Redis is single-threaded and ideal for smaller quantities. Redis provides data persistence and failover mechanisms, whereas Memcached does not have built-in support for these features.

Why Should You Use Memcached on Your WordPress Site?

Imagine you have a WordPress site with high traffic, and as visitors come to your site, they are welcomed with slow site speeds as the server struggles to handle the increasing load. Each database query adds to the server’s burden, resulting in performance bottlenecks.

But what if there was a solution? Luckily, there is, and that’s Memcached again.

By implementing Memcached on your WordPress site, you can alleviate the strain on your server by caching frequently accessed data in RAM. Memcached speeds up dynamic web applications like ecommerce stores, registration/login websites, etc., by reducing the database load.

By keeping the server less busy, your visitors will experience a faster loading time and a better user experience. There is an interesting and funny real-world story on GitHub that you might want to read to understand the typical use case of Memcached.

Is Your Current WordPress Hosting Underperforming?

Supercharge your WordPress performance with Cloudways hosting and unlock its full potential.

WordPress Memcached: Pros and Cons

Memcached offers several advantages as a caching solution, making it suitable for high-traffic web applications and large database access. However, it’s important to consider both the benefits and drawbacks before deciding.

Pros:

  • Exceptionally fast response times with in-memory key-value storage.
  • Scalable architecture for vertical scaling of computing capacity.
  • User-friendly and flexible for application development.
  • Supports open data formats and compatibility with various clients and programming languages.

Cons:

  • Data is stored temporarily and may be lost if a Memcached instance fails.
  • Limited visibility of stored data can make debugging challenging.
  • Key length for values is restricted to 250 characters (1 MB).
  • Lacks built-in security features, requiring additional measures for protection.
  • No redundancy or failover mechanisms for data backup.

Evaluating these pros and cons will help you determine whether Memcached aligns with your caching requirements.

How Does Memcached Work?

Whenever a visitor/browser requests the server requiring the database to process and respond, it adds load. 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.

Memcached is a distributed memory object caching system that consists of four major components:

  1. Client Software: Receives a list of distributed available Memcached servers.
  2. Client-Based Hashing Algorithm: Chooses the server based on key values.
  3. Server Software: Stores data (values) and keys into an internal hash table.
  4. 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 it is still valid before replying to the request. If the cache is absent, it sends the request to the database to process and store the result as an item. If a server becomes out of memory, it looks and replaces the expired items. If there’s still any need for information, Memcached looks for the items not requested for a particular period. By this, it keeps more recently requested information in dynamic memory.

At a high level, Memcached works as follows:

1. It checks the requested data to see 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 is not 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 ensures that fresh content is delivered to the requested client.

How to Use Memcached on Your WordPress Site (3 Methods)

You can use Memcached with your WordPress website in three ways:

  • Install it using the Cloudways platform (easiest method)
  • Manual installation on your hosting server
  • Use a WordPress Memcached plugin

1. Install Memcached on WordPress Via Cloudways (Easiest Method)

Cloudways users do not have to worry about all the technicalities related to WordPress Memcached because it comes pre-installed and activated on all Cloudways servers.

Here’s how to check if Memcached is running on your Cloudways server.

  • Log in to Cloudways Platform.
  • Navigate to your respective Server → Manage Services.
  • You will see that Memcached is pre-installed and activated by default.

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

2. Install Memcached on Server (Manual Method)

If you are not a Cloudways customer but still want to install Memcached on your server, then you can try the manual method.

Memcached does not require a lot of CPU resources. It is purely dependent on the RAM.

If you have a web server with 8GB RAM, but the OS and your websites consume only 4GB, then you can assign the rest of the RAM to Memcached instances to increase its storage capacity.

Give a read to all technical requirements here.

Memcached works on most Linux-based servers; installing it is just 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 handle security updates.

  • For Debian or Ubuntu users:

Command: apt-get install memcached 

  • For Redhat/Fedora:

Command: yum install memcached 

For more information, read the documentation of your respective OS.

Note: It is worth mentioning that Nginx, one of the most popular reverse-proxy servers, comes prepackaged with the Memcached module providing the most flexible Nginx Memcached bundled solution.

3. Install Memcached on WordPress (Using a Plugin)

WordPress repository has some of the best plugins to extend the site’s functionality. It also offers numerous cache plugins, most of which support Memcached, like W3TC. If you use W3TC, you may navigate to the General Settings tab and select Memcached from the dropdown, where it says:

  • Page Cache
  • Minify Cache
  • Database Cache

W3TC WordPress Memcached

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

How to Check If WordPress Memcached Is Enabled

There are multiple ways to test if Memcached is running. One of them is using Telnet.

  • To check, log in to your server SSH terminal and write the below command:
telnet localhost 11211

or

telnet 127.0.0.1 11211

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

  • If the above command runs successfully, you will get the below result:
Connected to localhost.
Escape character is '^]'.

Otherwise, you will get a connection error.

To check some basic stats, you can use this command: stats

You will see the following output:

STAT pid 313
STAT uptime 2778636
STAT time 1535727399
STAT version 1.4.21
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 47.119322
STAT rusage_system 48.765342
STAT curr_connections 1
STAT total_connections 151198
STAT connection_structures 3
STAT reserved_fds 20
STAT cmd_get 46
STAT cmd_set 4
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 30
STAT get_misses 16
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 22942
STAT bytes_written 3433252
STAT limit_maxbytes 268435456
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT bytes 106
STAT curr_items 1
STAT total_items 4
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
STAT crawler_reclaimed 0
STAT lrutail_reflocked 0

To check the items, use: stats items

Result:

STAT items:2:number 1
STAT items:2:age 2486
STAT items:2:evicted 0
STAT items:2:evicted_nonzero 0
STAT items:2:evicted_time 0
STAT items:2:outofmemory 0
STAT items:2:tailrepairs 0
STAT items:2:reclaimed 0
STAT items:2:expired_unfetched 0
STAT items:2:evicted_unfetched 0
STAT items:2:crawler_reclaimed 0
STAT items:2:lrutail_reflocked 0

To check current memory statistics, type: stats slabs

Result:

STAT 2:chunk_size 120
STAT 2:chunks_per_page 8738
STAT 2:total_pages 1
STAT 2:total_chunks 8738
STAT 2:used_chunks 1
STAT 2:free_chunks 8737
STAT 2:free_chunks_end 0
STAT 2:mem_requested 106
STAT 2:get_hits 30
STAT 2:cmd_set 4
STAT 2:delete_hits 0
STAT 2:incr_hits 0
STAT 2:decr_hits 0
STAT 2:cas_hits 0
STAT 2:cas_badval 0
STAT 2:touch_hits 0
STAT active_slabs 1
STAT total_malloced 1048560

To quit the connection, type quit and press enter.

Say Goodbye to Slow WordPress Sites. Switch to Cloudways

Don’t let sluggish loading times hold your website back. Migrate to Cloudways and enjoy blazing-fast performance for your WordPress site.

Final Thoughts

Memcached is considered one of the best 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.

Instead of going through the manual process of installing Memcached on your server or relying on a caching plugin like W3TC, opting for Cloudways managed hosting is the ideal choice. With Cloudways, Memcached is pre-installed and activated on all servers launched on their platform, providing a hassle-free experience.

Q1. Does WordPress support object cache?

A. Yes, WordPress supports object cache through its built-in functionality. It utilizes the WP_Object_Cache class, which allows for the automatic storage of data from the database in PHP memory.

Q2. How do I know if Memcached is running on WordPress?

A. You can check the WordPress admin panel to determine if Memcached is running on WordPress. Look for a caching section within the panel. If you find a dedicated section specifically for Memcached, it indicates that Memcached is indeed functioning with WordPress. However, if no such section is visible, it suggests that Memcached is not currently running on WordPress.

Q3. How to exclude some functions in WordPress?

A. In WordPress, you have the ability to exclude specific functions from caching by adding a line of code to the wp-config.php file. This allows you to disable caching for certain pages, URLs, user agents, IP addresses, cookies, and user roles based on your specific needs.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Sarim Javaid

Sarim Javaid is a Digital Content Producer at Cloudways. He has a habit of penning down his random thoughts and giving words and meaning to the clutter of ideas colliding inside his mind. His obsession with Google and his curious mind add to his research-based writing. Other than that, he's a music and art admirer and an overly-excited person.

×

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

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now