How to Use Object Cache with Redis to Reduce WordPress Load
Posted in: Database, PHP, Plugins, WordPress

How to Use Object Cache with Redis to Reduce WordPress Load

Object cache with Redis can minimize the load on your WordPress database by caching frequently accessed data in memory instead of having to retrieve it from the database every time it’s needed. This can significantly reduce the number of database queries required to load a page, resulting in faster page load times and reduced server load.

By reducing the load on your WordPress database, you can also improve the overall performance and scalability of your WordPress site.

In this guide, we learn how to use object cache in Redis in WordPress using a drop-in plugin. Also, we learn the steps to install the Redis server and how to monitor its memory usage.

Installing and Configuring Object Cache Drop-in Plugin

Instead of installing Redis plugin from a WordPress repository, we can install a drop-in plugin.

A drop-in plugin is a PHP file that you can drop into the WordPress “wp-content” directory. This automatically activates the plugin without any additional configuration or settings.

To get WordPress Redis drop-in plugin, navigate to its GitHub repository, and click on the “Code” > “Download ZIP”. Next, extract the zip, you will find a PHP file “object-cache.php”. This is a drop-in WordPress plugin for enabling object cache with Redis.

Setting Default Expiration Time for Redis

Now, before installing this drop-in plugin, you can edit it and set Redis default expiration time by editing the “object-cache.php” file. You will see this line of code:

if ( ! defined( 'WP_REDIS_DEFAULT_EXPIRE_SECONDS' ) ) {
	define( 'WP_REDIS_DEFAULT_EXPIRE_SECONDS', 0 );
}

You can change the value of 0 (which means no expiration) to something like 604800 (which is 7 days). So, it will look like this:

if ( ! defined( 'WP_REDIS_DEFAULT_EXPIRE_SECONDS' ) ) {
	define( 'WP_REDIS_DEFAULT_EXPIRE_SECONDS', 604800 );
}

Setting an appropriate expiration time for cached data ensures that stale data is not being served to site visitors. Additionally, it helps to prevent the Redis cache from becoming too large and overwhelming the server’s resources.

However, there are other methods to prevent excessive memory usage by Redis that are mentioned in this guide below.

Once, you have finished editing the “object-cache.php” file, you can install it as a drop-in plugin on your WordPress site.

Installing Redis Object Cache Drop-in Plugin

Using your WordPress file manager, you can simply put “object-cache.php” inside the “wp-content” directory. So, it will look like “wp-content/object-cache.php”.

Now, in your WordPress admin panel, you can check this drop-in plugin by going to “Plugins” > “Installed Plugins” > “Drop-in”.

Next, you would need to mention the Redis server host, port, and Redis database to use for your WordPress website inside the “wp-config.php” file. You can do so by going to your WordPress file manager, then edit your “wp-config.php” file, and adding the following code just above the message as shown below:

// Redis.
$redis_server = array(
	'host'     => '127.0.0.1',
	'port'     => 6379,
	'auth'     => '',
	'database' => 0,
);

/* That's all, stop editing! Happy publishing. */

You can use database values such as 0, 1, 2, and so on depending on the specific WordPress website in case there are multiple WordPress sites on the same server.

WP Redis: Connection Attempt Failed

After installing the drop-in Redis plugin, if you see a message like “WP Redis: A connection attempt failed”, then it means Redis is not installed on your server. Below are the steps to install Redis and its PHP extension on a server.

Installing Redis Server and PHP Redis Extension

First, you need to log in to your server via SSH (terminal). To check if Redis is already installed on your server, you can check this by using the command:

> redis-cli info

If Redis is already installed, you should get information about the Redis service, else you will get the command not found message.

If it shows command not found, then install Redis by running these commands one by one:

> sudo apt-get update
> sudo apt-get install redis-server

Next, press Y to continue installing Redis and its related packages. Now, you can re-run the “redis-cli info” command and it should return some output.

The drop-in plugin also requires a PHP Redis extension. You can enable this extension by going to your cPanel PHP settings. Or, you can install it using the SSH terminal of your server.

Enable PHP Redis Extension via cPanel

If you use a cPanel for managing server configurations, there you can enable the PHP Redis extension by going to your cPanel’s PHP settings.

Depending on the PHP version that your WordPress site runs on, you would need to enable the PHP Redis extension that corresponds to the PHP version.

Install PHP Redis Extension via SSH

If the PHP Redis extension is not installed, then you can install it via SSH terminal using the following command:

> sudo apt-get install php8.1-redis

You can adjust the above command to install the PHP Redis extension for the PHP version that your WordPress site uses.

Setup Maximum Memory Limit and Policy for Redis

It’s good to set a maximum memory limit for a Redis so it does not run out of memory. Therefore, it won’t throw any memory-related errors. To set the maximum memory limit for Redis, you can SSH on your server and run the command to edit the Redis configuration file:

> sudo nano /etc/redis/redis.conf

Set Maximum Memory Value

Next, in the “redis.conf” file, using the nano editor, use the “Ctrl + W” key to search for “maxmemory <bytes>”. You will see this line:

# maxmemory <bytes>

Remove the “#” and change the above line to add maximum memory, so it will look like this:

maxmemory 100mb

The value “100mb” depends on the size of your actual WordPress database as well as how frequently data is accessed, you can experiment with it by setting a small value first like “40mb”, “60mb”, and so on, then you can monitor Redis memory usage and change its value depending on the Redis memory usage.

Save the file with “Ctrl + X” and then “Y”. Then, restart Redis using the command:

> sudo systemctl restart redis.service

Set Policy for Maximum Memory

It is also necessary to set a policy for evicting keys once maximum memory usage is reached by the Redis instance, you can do so by the editing Redis configuration just like above:

> sudo nano /etc/redis/redis.conf

Using “Ctrl + W”, search for “maxmemory-policy noeviction”, then you will find this line:

# maxmemory-policy noeviction

Remove the “#” and change it so, it will look like this:

maxmemory-policy volatile-lru

You may also change it to “allkeys-lru” instead of “volatile-lru”.

maxmemory-policy allkeys-lru

The “allkeys-lru” value evicts the least recently used keys from the Redis cache.

The “volatile-lru” value only considers volatile keys when evicting items from the cache. Volatile keys are those that have an expiration time set, so they will be automatically deleted from the cache when that time is reached.

Save the file with “Ctrl + X” and then “Y”.

After making changes to the Redis configuration, you must restart the Redis service by running the following command:

> sudo systemctl restart redis.service

Monitoring Redis Memory Usage

There are many useful Redis commands to check Redis keyspace and its memory usage.

To check the current memory usage of Redis, use the command:

> redis-cli info memory

This will display a lot of information related to memory usage.

See only the used memory and memory overhead of Redis using the command:

> redis-cli info memory | grep 'used_memory'

Display the current memory usage of Redis in a human-readable format:

> redis-cli info memory | grep 'used_memory.*human';

You can also check the memory usage of individual Redis databases with the command:

> redis-cli info keyspace

This will show the memory usage of each database in bytes.

You may also want to clear all cache in the Redis memory:

> redis-cli FLUSHALL

For clearing individual Redis databases like 0, you can run the following command:

> redis-cli -n 0 FLUSHDB

In conclusion, using an object cache with Redis can significantly reduce the load time of WordPress websites. By storing frequently accessed data in memory, object cache like Redis can reduce the number of database requests and improve site performance.

To use object cache with Redis in WordPress, users have to install and configure Redis, enable the PHP Redis extension, install a Redis object cache plugin, and add Redis configuration settings to the WordPress configuration file.

Back to Top