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.
In this article, we’ll discuss about what is persistent cache and why is it beneficial for all types of WordPress sites.
Table of Contents
What is persistent object cache and why is it recommended?
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.
The three key benefits of persistent object cache are:
- Speed: Reduces time to fetch data.
- Scalability: Handles more concurrent users.
- Efficiency: Lowers database load.
In simpler terms, persistent object cache works in three steps:
- Data Storage: Stores frequently accessed data in memory.
- Cache Hits: Serves data from cache if available.
- 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:
- Redis
- 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.Feature Redis Memcached Data Types Strings, Lists, Sets, Hashes Strings (key-value pairs) Persistence Yes (RDB, AOF) No Scalability Horizontal (Sharding) Horizontal (Sharding) Memory Management LRU, LFU, TTL LRU Performance Slightly slower for simple tasks Faster for simple tasks Complexity Higher (more features) Lower (simpler)
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
Metric | Redis | Memcached |
---|---|---|
Read Speed | Fast | Very Fast |
Write Speed | Fast | Very Fast |
Memory Efficiency | High (data structures) | Moderate (simple key-value) |
CPU Usage | Moderate (complex ops) | Low |
Eviction Policies
Policy | Redis | Memcached |
---|---|---|
LRU | Least Recently Used | Least Recently Used |
LFU | Least Frequently Used | Not supported |
TTL | Time to Live | Not supported |
Noeviction | No eviction if memory full | Not supported |
Security Features
Feature | Redis | Memcached |
---|---|---|
Authentication | Password-based | SASL (limited) |
Encryption | TLS/SSL | External solutions required |
Access Control | ACLs | IP whitelisting |
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:
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.
Step 2: After installation, the Redis server needs to be started manually. This can be done in cPanel under Software -> Redis.
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.
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: ReachablePress the Enable Object Cache button to start the Redis cache.
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.