How to Fix You Should Use a Persistent Object Cache in WordPress

How to Fix You Should Use a Persistent Object Cache in WordPressHow to Fix You Should Use a Persistent Object Cache in WordPress

WordPress 6 introduced a new recommendation under Site Health. If you are not using any cache plugin, you will get the following recommendation under Site Health:

You should use a persistent object cache.
You should use a persistent object cache
You should use a persistent object cache

In this article, we’ll discuss about what is persistent cache and why is it beneficial for all types of WordPress sites.

Most hosts and cache plugins work in a similar way by saving an html copy of the WordPress pages. These html pages are then served to the visitors instead of dynamically generating the page again using PHP and database queries.

The problem with this approach is that it doesn’t work if you are logged in or your website pages change frequently e.g., using a forum or comments on WordPress etc.

Persistent object cache comes as a solution to this problem. That’s why WordPress recommends having this cache enabled on any WordPress installation.

Persistent object cache stores frequently accessed data in memory, reducing the need for repeated database queries. This improves performance by speeding up data retrieval and lowering server load.

How WordPress persistent object cache works
How WordPress persistent object cache works

The three key benefits of persistent object cache are:

  1. Speed: Reduces time to fetch data.
  2. Scalability: Handles more concurrent users.
  3. Efficiency: Lowers database load.

In simpler terms, persistent object cache works in three steps:

  1. Data Storage: Stores frequently accessed data in memory.
  2. Cache Hits: Serves data from cache if available.
  3. Cache Misses: Fetches from database and updates cache.

If you are already using a cache plugin for WordPress, make sure it supports persistent caching. If not, you should install a persistent cache plugin separately. This should improve the performance of your site regardless of being small or large.

I have tested several cache plugins for many of my sites. While most static sites can live with commonly available cache plugins, some dynamic sites need advanced caching to make sure they are using less CPU, which is usually the most expensive resource in a hosting environment.

Redis vs Memcached

There are two common types of persistent object cache for WordPress:

  1. Redis
  2. Memcached

Redis and Memcached are both in-memory caching systems used to speed up WordPress by storing frequently accessed data. Redis supports more complex data types and persistence, Memcached is simpler and often faster for basic key-value storage.

FeatureRedisMemcached
Data TypesStrings, Lists, Sets, HashesStrings (key-value pairs)
PersistenceYes (RDB, AOF)No
ScalabilityHorizontal (Sharding)Horizontal (Sharding)
Memory ManagementLRU, LFU, TTLLRU
PerformanceSlightly slower for simple tasksFaster for simple tasks
ComplexityHigher (more features)Lower (simpler)
Technical comparison between Redis and Memcached

In simpler terms, Redis offers more advanced caching features while Memcached excels in simplicity and speed for basic tasks. Installing and configuring Redis is more complex than Memcached.

Here are some comparisons between Redis and Memcached:

Performance

MetricRedisMemcached
Read SpeedFastVery Fast
Write SpeedFastVery Fast
Memory EfficiencyHigh (data structures)Moderate (simple key-value)
CPU UsageModerate (complex ops)Low
Performance comparison between Redis and Memcached

Eviction Policies

PolicyRedisMemcached
LRULeast Recently UsedLeast Recently Used
LFULeast Frequently UsedNot supported
TTLTime to LiveNot supported
NoevictionNo eviction if memory fullNot supported
Eviction policy comparison between Redis and Memcached

Security Features

FeatureRedisMemcached
AuthenticationPassword-basedSASL (limited)
EncryptionTLS/SSLExternal solutions required
Access ControlACLsIP whitelisting
Security features comparison between Redis and Memcached

Before selecting the persistent object cache for your WordPress install, make sure your hosting provider supports that solution. I have selected Redis cache for itechtics.com.

The main reason for selecting Redis is its persistence and durability. This means that Redis has the ability to persist data to disk, ensuring data is not lost even in the event of system failure unlike Memcached.

How to configure Redis Object Cache for WordPress

Although there are many plugins for caching that include persistent object caching feature, I prefer to use the one specifically created for this very purpose, Redis Object Cache plugin.

Installing and configuring Redis Object Cache plugin is not very straightforward and requires some manual configurations. Let’s install this plugin step by step:

  1. Step 1: The first and foremost step is to install and enable Redis Cache on your server. If you are using cPanel, you can enable it by going to “Select PHP version” and then selecting “Redis” under Extensions.

    Enable Redis in cPanel
    Enable Redis in cPanel
  2. Step 2: After installation, the Redis server needs to be started manually. This can be done in cPanel under Software -> Redis.

    Starting Redis Server
    Starting Redis Server
  3. Step 3: Once the server is started, note down the IP address, port number, and password from the Redis config. Now open wp-config.php of your WordPress site and enter the following at the top of the file after <?

    // Enable Redis cache
    define('WP_CACHE', true);
    define('WP_REDIS_HOST', '127.0.0.1');
    define('WP_REDIS_PORT', 413);
    define('WP_REDIS_PASSWORD', '123');
    define('WP_REDIS_MAXMEMORY', '128mb');
    define('WP_REDIS_DATABASE', 0); // Use database 0 unless you need to change it
    define('WP_REDIS_CLIENT', 'phpredis'); // Ensure the right client is used.
  4. Step 4: Once the configuration is saved in wp-config.php, go to WordPress admin panel (wp-admin) and install the following plugin: Redis Object Cache. Once installed, go to Settings -> Redis. It should show you the following:

    Status: Not enabled
    Filesystem: Writeable
    Redis: Reachable

    Press the Enable Object Cache button to start the Redis cache.

    Redis Object Cache in WordPress
    Redis Object Cache in WordPress

This will enable Redis object caching for your site. The site will use more CPU and other resources when the cache is warming up. However, once the caching is complete, the resource usage will drop significantly resulting in better performance and load time of the site.

Finally, the test for using persistent object cache under Site Health in WordPress will be resolved. I hope this has been helpful to you. If there is any confusion, we can always discuss in the comments below.

If you liked this post, Share it on:
Usman Khurshid is a seasoned IT Pro with over 15 years of experience in the IT industry. He has experience in everything from IT support, helpdesk, sysadmin, network admin, and cloud computing. He is also certified in Microsoft Technologies (MCTS and MCSA) and also Cisco Certified Professional in Routing and Switching.

Get Updates in Your Inbox

Sign up for the regular updates and be the first to know about the latest tech information

Talk to us now

Talk to us straight and get your questions answered right away

Tell Us About Your Project