Redis Object Cache for WordPress

Redis Object Cache for WordPress

WordPress Redis Object Cache can increase your WordPress website’s performance, but not every slow website can benefit from it.

Learn when Redis is good for WordPress and when it’s not, how it works, and how to install it on your WordPress sites.

Table of Contents

Is Redis Good For WordPress?

What is Redis Object Cache?

Redis is an open-source key value store that can operate as both an in-memory store and as a cache. WordPress uses this Object Cache to save time by storing in memory the database query results.

The Object Cache is not the same as the HTML Cache. WordPress plugins like WP-Super-Cache, WP-Rocket, etc. are HTML cache plugins, whereas Object Cache stores database content.

While the Page Cache plugins work on caching the HTML page to save time skipping the PHP process, the Object Cache works caching data to skip the database queries.

How does Object Cache work with WordPress?

Object caching stores database query results that have been requested. Subsequently, it serves them up faster the next time a user requests them. This way, your database doesn’t have to be queried again.

WordPress also has Object Cache built-in with the Core WP_Object_Cache class. The problem is that this object caching in WordPress isn’t persistent by default.

This means that cached objects are only stored for as long as the Page Generation process lasts. This is quite inefficient in practice unless you install a persistent caching mechanism such as Redis Object Cache.

Do I need Redis in WordPress?

If one of the following situations is your case, you are a candidate for Object Redis Cache:

  • You cannot cache your WordPress site content because it changes depending on the user. In these cases, the HTML cache plugins can’t be used.
  • Your site is slow due to an intense load in your database. With Object Caching, your database can be queried much less often. Moreover, retrieving content from Redis is a lot faster than sending queries to the database.
  • Your server’s resources are also used more efficiently. This is an especially important consideration if you’re looking to scale your WordPress website.
  • My Database is fast enough but I have a high user concurrence at certain moments.

Does Redis speed up WordPress?

Not always! Here is a list of all the requirements to meet in order to take advantage of Redis Object Cache:

  1. An HTML static page cache plugin can’t be used or does not cover all your pages. This is common when your WordPress site interacts or changes depending on the user. Examples: e-commerce websites using WooCommerce, Membership sites, Social Networking websites, or Forums using BBPress, BuddyPress, etc.
  2. The Page Generation Time is high (e.g. more than 0,5 seconds) or can be high due to the concurrency of users.
  3. The time dedicated to Database Queries is high compared to the Page Generation Time, OR the amount of queries is high (e.g. more than 200 queries) so an eventual concurrency of users can impact performance.

In this next section, we’ll show you how to inspect your WordPress and analyze the Page Generation Time of your site.

How to analyze the WordPress Page Generation Time

To be able to analyze the Page Generation Time on every page of your WordPress site, and to identify the sources of performance problems, install the free Query Monitor plugin by John Blackbourn.

This plugin in addition to the Page Generation Time shows how many database queries ran to generate the page and how much time that took.

In the example shown below, we can identify the key metrics when running without Redis Object Cache:

  • Page Generation Time: 0.2926s
  • Database Queries: Total: 132
  • Object Cache: plugin not installed
Query Monitor Page Generation Time, Queries and Object Cache information
A Page Generation time of 0.2962s and 132 database requests, without Redis Object Cache.

Will Redis Object Cache help us reduce the Page Generation Time?

In our case, we have 132 Database Queries, which looks like a lot but the Time our server needs to solve these queries is very low: 0.0193 seconds. In such a case Redis Cache could only help us to reduce those 0.0193 seconds. It’s not worth it if no high concurrency.

If the Database Queries Time is low, Redis Object Cache can only help us in cases of high concurrency.

Verifying if Redis Object Cache saves time caching my Database requests

Let’s say we have high concurrency and we want to serve queries from Redis, avoiding bottlenecks in our Database:

Continuing with our case, let’s check the Page Generation Time when running Redis Object Cache.

Page Generation Time, Database Queries and Redis Object Cache performance results
A Page Generation time of 0.2736s and 31 database requests, with Redis Object Cache.

The results with Redis Object Cache shown above tell us:

  • Page Generation Time: 0.2736s
  • Database Queries: Total: 31
  • Object Cache: 99.3% Hits

We have reduced our Page Generation Time to less than 20 milliseconds. It is too low a value to be taken into account, but the Database queries did decrease from 132 to 31, and this is what will help us in case of high concurrency.

Conclusion:

In this case, Redis Object Cache only helps us under user concurrency. The impact in Page Generation Time is low, but Redis Object Cache works fine by reducing the database load.

How do I implement Redis cache in WordPress?

To use Redis for object caching on WordPress sites, you have to start by installing and configuring a Redis service. Then you have to install the PHP Redis extension, followed by the Redis Object Cache plugin.

The easiest path to install Redis Object Cache for WordPress

If your server is hosted at Wetopi, installing, enabling, and configuring Redis Object Cache is a simple process.

1. Add the Redis Object Cache Wetopi addon

Go to your Wetopi site and open the Addons tab you’ll find at the bottom, in the “Site options” section:

Wetopi addons with Redis Object Cache

Once you click over the Redis Object Cache, a floating window shows you the Redis Object Cache Addon details:

Enable Redis Object Cache for WordPress

NOTE: Small servers are scaled to Medium to accommodate the Redis service.

Once you get the Redis Addon enabled, your Wetopi servers will boot with an instance image running the Redis service and the PHP engine with its Redis module enabled.

2. Install and configure the Redis Object Cache plugin with a single click

The second and last step will install and configure the Redis Object Cache plugin so your WordPress can deal with the Object Cache storage.

With Wetopi this is a single click. You’ll find this option in the menu of your WordPress server, within the advanced options section:

install and configure redis object cache with a single click
Click on WP Redis Setup to install, activate and optimally configure the Redis Object Cache plugin

No further action is required.

If during a test, you don’t want to break your production site:

Clone your server to create a Staging environment!

To clone a WordPress site with Wetopi is as easy as a simple click.

The Step-by-step installation process of Redis Object Cache for WordPress

If you are hosted at Wetopi, you don’t need to do the next steps steps. You can jump to the section “Using Redis Object Cache Plugin

1. Install and configure the Redis service

If you are running an Ubuntu distribution follow this command:

sudo apt install redis

Configure Redis /etc/redis/redis.conf to run as fast as possible by enabling the socket connection:

# Unix socket.
#
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
unixsocket /var/run/redis/redis.sock
unixsocketperm 777

Set also the memory limit, the memory policy and configure Redis to work purely in memory not saving to disk (it’s cache, we don’t need persistence).

############################## MEMORY MANAGEMENT ################################

# Set a memory usage limit to the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys
# according to the eviction policy selected (see maxmemory-policy).

maxmemory 128mb


# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> Evict using approximated LRU among the keys with an expire set.
# allkeys-lru -> Evict any key using approximated LRU.
# volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
# allkeys-lfu -> Evict any key using approximated LFU.
# volatile-random -> Remove a random key among the ones with an expire set.
# allkeys-random -> Remove a random key, any key.
# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
# noeviction -> Don't evict anything, just return an error on write operations.
#
# LRU means Least Recently Used
# LFU means Least Frequently Used
#
maxmemory-policy allkeys-lru

...


################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
save ""

You can restart it to let everything get configured using

sudo systemctl restart redis.service

Check the Redis status with this command:

sudo systemctl status redis

2. Install the PHP redis module

If you are running an Ubuntu or Debian distribution you can install the Redis module with a simple apt-get install

apt-get update
apt-get install php7.4-redis
phpinfo() showing the PHP engine with “redis” module enabled.

3. Install the Object Cache Plugin

Login to your WordPress admin and add a new plugin. For example, search Redis and select this specific Redis Object Cache plugin.

Add Redis Object Cache plugin

Install and activate the plugin. Then head over to Redis settings on your dashboard menu.

Now you should click the Enable Object Cache button. This will set the Redis plugin using the default settings.

4. Enable socket connection to Redis for better performance

If you are running your Redis service in the server side by side with your WordPress, the most performant connection method is the UNIX socket.

To configure our plugin Redis Object Cache to connect via socket, you have to define two constants in your wp-config.php

define( 'WP_REDIS_PATH', '/var/run/redis/redis.sock' );
define( 'WP_REDIS_SCHEME', 'unix' );

NOTE: modify the WP_REDIS_PATH path to point to your real redis socket.

Using Redis Object Cache Plugin

The Redis Object Cache Plugin by Till Krüss is very simple.

From your WordPress admin panel, you can open the Metric stats:

Redis Object Cache Metrics
The Metrics tab shows an impressive response time of Redis queries: below 4 milliseconds

This plugin also offers you the option to Flush the Cache from the Overview section:

How to flush redis object cache

NOTE: if you look for a business class Redis object cache plugin, there is a Pro version of this plugin Object Cache Pro.

The Redis Flush Cache

Usually we don’t need to flush the Redis object cache. Still, sometimes it’s the only option when back-end operations aren’t working properly, such as when activating or deactivating a WordPress plugin.

The Flush Cache option is just for the object cache and is a good practice to Flush or Purge all the other HTML caches e.g. WP-Super-Cache.

Final Words

Object caching with Redis can increase your WordPress site’s Page Generation speed when you have a database bottleneck or high user concurrency on dynamic pages.

At Wetopi we love to accomplish complex thing with simple and fast solutions. If you want to test your WordPress site with Redis Object Cache, don’t waste your time! signup and request your free Migration.

We are techies passionate about WordPress. With wetopi, a Managed WordPress Hosting, we want to minimize the friction that every professional faces when working and hosting WordPress projects.

Not a wetopi user?

Free full performance servers for your development and test.
No credit card required.

See how Wetopi stacks up against your current hosting

Migrating sites to us is free and completely effortless on your part.

No hidden small text.
No commitments.
No credit card.

Try before you buy.