Using Redis Object Cache to Speed Up Your WordPress Installation

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It can be combined with other databases like MySQL to provide a mechanism to cache your queries.

In this article, we’ll show how to cache the MySQL queries that are required to render WordPress pages. This will provide us with significant performance gains, though with some caveats.

Intro to Better WordPress Caching

WordPress uses the MySQL database to cache internal application objects (breadcrumbs, menu items, etc.) which can be expensive to generate. Since the database also handles queries for page requests, it’s possibly the most common bottleneck and often causes increased load-times.

Redis provides a caching mechanism that acts as an alternative to the MySQL database. If a user visits a WordPress page, the MySQL queries required to generate the page are served via Redis which caches the results. This results in blazing fast load times, comparable to static pages.

Another popular caching choice is Memcached. However, Redis does almost everything that Memcached does so we will be using Redis for the sake of this tutorial.

How Does Redis Caching Work?

When a user requests a WordPress page for the first time, a MySQL query is performed on the server. Redis caches this query, so when another user requests the same WordPress page, the results are provided from Redis without the need to query the database again.

If the query is not cached in Redis, the results are provided by MySQL, which are then added to the Redis cache.

If a particular value is updated in the database, the corresponding Redis value is invalidated to prevent bad cache data from being served to the user.

Redis Object Cache and WordPress Working Together

Tutorial Requirements

Here is the list of technologies used in this article:

  • 2 WordPress installations (with and without the plugin, for benchmarks)
  • Redis server
  • Redis WP Object Cache Plugin
How to speed up your WordPress installation using Redis Object CacheClick To Tweet

WordPress Installation

You can download the latest version of WordPress here and set up two different installations on a host like Heroku or AWS.

Redis Server

ScaleGrid provides world-class support for Redis Standalone and Master/Slave clusters with enhanced security features. We’ll be using a Standalone Redis cluster hosted on ScaleGrid with the Allkeys-LRU caching strategy, and you can signup for a free 30-day trial of our Redis hosting┬áto assist with this tutorial.

ScaleGrid Redis Hosting - Use Redis as a Cache

Redis WP Object Cache Plugin

This plugin is used as a drop-in to your WordPress installation and can be configured to use the Redis host. You can find the plugin here.

Setup Instructions: Redis Object Cache for WordPress

Here are the steps for getting the Redis Object Cache Plugin installed on a WordPress installation.

Step 1: Install the Redis Cache Plugin

This tutorial assumes that you already have two fresh installations of WordPress set up. On one of them, you need to install the Redis Object Cache plugin. Once the plugin is installed, you can activate it from the plugins menu.

Redis Object Cache WordPress Plugin Activated

Step 2: Find Redis in your Settings

Once the plugin is activated, a Redis option will appear under the Settings Menu.

Redis Object Cache WordPress Options

Step 3: Add WordPress Configurations

After setting up the plugin, we need to add configuration options to the WordPress config file. At a minimum, you would need to set up the following options:

define('WP_REDIS_HOST', '');
define('WP_REDIS_PASSWORD', '');

ScaleGrid Redis Hosting - Add WordPress Configurations

You can find the above options on the ScaleGrid Redis Cluster Details page.

Save the config and enable the Object Cache. If the authentication is correct, you should see the following screen:

Redis Object Cache Settings Saved

At this point, Redis has been configured as a cache for your WordPress installation.

Monitoring and Debugging WordPress Queries

All Redis clusters come installed with the Redis command line interface (redis-cli) tool that you can use to monitor the incoming query requests from WordPress. Here’s a screenshot of how this might look:

ScaleGrid Redis Hosting, Monitoring and Debugging

If you do not see any output on the console, go to the WordPress website and refresh.

WordPress & Redis Metrics to Track

The major metric to track here is the page load time. Here is the default configuration we’re using for the fresh install:

  • WordPress: 4.9.5
  • Redis: v.4.0.2
  • WordPress Theme: Twenty Seventeen

Based on a fresh installation on the above parameters, we can see almost 50% reduction in page load times:

  • Without the Redis Object Cache = ~900ms
  • With the Redis Object Cache = ~400ms

Redis Object Cache Disadvantages

There are a few caveats to using the Redis Object Cache approach on WordPress. The caching mechanism does not work well if you have a lot of plugins and themes installed. This is due to the fact that all their data would need to be cached as well with Redis.

You can also learn how to Cache Tweets Using Node.js, Redis, and Socket.io in our earlier article.

As always, if you build something awesome, do tweet us about it @scalegridio. If you need help with MongoDB or Redis hosting and management, reach out to us at support@scalegrid.io.

Check out our Redis Hosting plans


Kunal is the UI guy at ScaleGrid.io. You can reach him at _kunalnagar


0 Shares
+1
Tweet
Share
Share
Pin