Using Redis Object Cache to Speed Up Your WordPress Installation
Tags: redis, mysql, wordpress, cache
Published: 2018-05-31 15:47:22

---
title: Using Redis Object Cache to Speed Up Your WordPress Installation
published: true
description: ScaleGrid Tutorial - How to increase your WordPress page load time using Redis Object Cache for MySQL queries to significantly improve your page speed performance.
tags: Redis, MySQL, WordPress, Cache,
canonical_url: https://scalegrid.io/blog/using-redis-object-cache-to-speed-up-your-wordpress-installation/
---
<link rel="canonical" href="https://scalegrid.io/blog/using-redis-object-cache-to-speed-up-your-wordpress-installation/" />

<img src="https://thepracticaldev.s3.amazonaws.com/i/7bhzge022zed5nu4hqsq.png" alt="Using Redis Object Cache to Speed Up Your WordPress Installation - ScaleGrid Blog" width="933" height="805" />

<p style="text-align: justify;"><a title="Redis" href="https://redis.io/" target="_blank">Redis</a> 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 <a title="MySQL" href="https://www.mysql.com/" target="_blank">MySQL</a> to provide a mechanism to cache your queries.</p>
<p style="text-align: justify;">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.</p>

<h2><span style="font-weight: 400;">Intro to Better Wordpress Caching</span></h2>
<p style="text-align: justify;"><a title="Wordpress" href="https://wordpress.org/" target="_blank">WordPress</a> 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.</p>
<p style="text-align: justify;">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.</p>
<p style="text-align: justify;">Another popular caching choice is <a href="https://memcached.org/" target="_blank">Memcached</a>. However, Redis does almost everything that Memcached does so we will be using Redis for the sake of this tutorial.</p>

<h2><span style="font-weight: 400;">How Does Redis Caching Work?</span></h2>
<p style="text-align: justify;">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.</p>
<p style="text-align: justify;">If the query is not cached in Redis, the results are provided by MySQL, which are then added to the Redis cache.</p>
<p style="text-align: justify;">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.</p>
<a href="https://scalegrid.io/blog/wp-content/uploads/2018/05/Capture2.png"><img class="alignnone size-full wp-image-3149" src="https://scalegrid.io/blog/wp-content/uploads/2018/05/Capture2.png" alt="Redis Object Cache and WordPress Working Together" width="858" height="648" /></a>
<h2><span style="font-weight: 400;">Tutorial Requirements</span></h2>
<p style="text-align: justify;">Here is the list of technologies used in this article:</p>

<ul>
	<li>2 WordPress installations (with and without the plugin, for benchmarks)</li>
	<li>Redis server</li>
	<li>Redis WP Object Cache Plugin</li>
</ul>

<h3><span style="font-weight: 400;">WordPress Installation </span></h3>
<p style="text-align: justify;">You can download the latest version of WordPress <a href="https://wordpress.org/latest.zip">here</a> and set up two different installations on a host like <a title="Heroku - Wordpress Installations" href="https://www.heroku.com/home" target="_blank">Heroku</a> or <a title="AWS - WordPress Installations" href="https://aws.amazon.com/" target="_blank">AWS</a>.</p>

<h3><span style="font-weight: 400;">Redis Server</span></h3>
<p style="text-align: justify;">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 <a href="https://console.scalegrid.io/users/register">Redis hosting</a> to assist with this tutorial.</p>
<a href="https://scalegrid.io/blog/wp-content/uploads/2018/05/Capture3.png"><img class="alignnone size-full wp-image-3150" src="https://scalegrid.io/blog/wp-content/uploads/2018/05/Capture3.png" alt="ScaleGrid Redis Hosting - Use Redis as a Cache" width="1083" height="767" /></a>
<h3><span style="font-weight: 400;">Redis WP Object Cache Plugin</span></h3>
<p style="text-align: justify;">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 <a href="https://wordpress.org/plugins/redis-cache/">here</a>.</p>

<h2><span style="font-weight: 400;">Setup Instructions: Redis Object Cache for WordPress</span></h2>
<p style="text-align: justify;">Here are the steps for getting the Redis Object Cache Plugin installed on a WordPress installation.</p>

<h3><span style="font-weight: 400;">Step 1: Install the Redis Cache Plugin</span></h3>
<p style="text-align: justify;">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.</p>
<a href="https://scalegrid.io/blog/wp-content/uploads/2018/05/image3.png"><img class="alignnone size-full wp-image-3141" src="https://scalegrid.io/blog/wp-content/uploads/2018/05/image3.png" alt="Redis Object Cache WordPress Plugin Activated" width="1193" height="234" /></a>
<h3><span style="font-weight: 400;">Step 2: Find Redis in your Settings</span></h3>
Once the plugin is activated, a Redis option will appear under the Settings Menu.

<a href="https://scalegrid.io/blog/wp-content/uploads/2018/05/image4.png"><img class="alignnone size-full wp-image-3142" src="https://scalegrid.io/blog/wp-content/uploads/2018/05/image4.png" alt="Redis Object Cache WordPress Options" width="795" height="858" /></a>
<h3><span style="font-weight: 400;">Step 3: Add WordPress Configurations</span></h3>
<p style="text-align: justify;">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:</p>

<pre>define('WP_REDIS_HOST', '');
define('WP_REDIS_PASSWORD', '');
</pre>
<a href="https://scalegrid.io/blog/wp-content/uploads/2018/05/image5.png"><img class="alignnone size-full wp-image-3143" src="https://scalegrid.io/blog/wp-content/uploads/2018/05/image5.png" alt="ScaleGrid Redis Hosting - Add WordPress Configurations" width="1399" height="787" /></a>
<p style="text-align: justify;">You can find the above options on the ScaleGrid Redis Cluster Details page.</p>
<p style="text-align: justify;">Save the config and enable the Object Cache. If the authentication is correct, you should see the following screen:</p>
<a href="https://scalegrid.io/blog/wp-content/uploads/2018/05/image2.png"><img class="alignnone size-full wp-image-3144" src="https://scalegrid.io/blog/wp-content/uploads/2018/05/image2.png" alt="Redis Object Cache Settings Saved" width="1703" height="641" /></a>
<p style="text-align: justify;">At this point, Redis has been configured as a cache for your WordPress installation.</p>

<h2><span style="font-weight: 400;">Monitoring and Debugging WordPress Queries</span></h2>
<p style="text-align: justify;">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:</p>
<a href="https://scalegrid.io/blog/wp-content/uploads/2018/05/image1.png"><img class="alignnone size-full wp-image-3145" src="https://scalegrid.io/blog/wp-content/uploads/2018/05/image1.png" alt="ScaleGrid Redis Hosting, Monitoring and Debugging" width="933" height="805" /></a>
<p style="text-align: justify;">If you do not see any output on the console, go to the WordPress website and refresh.</p>

<h2><span style="font-weight: 400;">WordPress &amp; Redis Metrics to Track</span></h2>
<p style="text-align: justify;">The major metric to track here is the page load time. Here is the default configuration we're using for the fresh install:</p>

<ul>
	<li>WordPress: 4.9.5</li>
	<li>Redis: v.4.0.2</li>
	<li>WordPress Theme: Twenty Seventeen</li>
</ul>
<p style="text-align: justify;">Based on a fresh installation on the above parameters, we can see almost 50% reduction in page load times:</p>

<ul>
	<li><strong>Without</strong> the Redis Object Cache = <strong><span style="color: #ff0000;">~900ms</span></strong></li>
	<li><strong>With</strong> the Redis Object Cache = <strong><span style="color: #2da964;">~400ms</span></strong></li>
</ul>
<h2><span style="font-weight: 400;">Redis Object Cache Disadvantages</span></h2>
<p style="text-align: justify;">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.</p>
<p style="text-align: justify;">You can also learn how to <a title="Caching tweets using Node.js, Redis, and Socket.io" href="https://scalegrid.io/blog/caching-tweets-using-node-js-redis-and-socket-io-2/" target="_blank">Cache Tweets Using Node.js, Redis, and Socket.io</a> in our earlier article.</p>
<p style="text-align: justify;">As always, if you build something awesome, do tweet us about it <a title="ScaleGrid Twitter" href="https://twitter.com/scalegridio" target="_blank">@scalegridio</a>. If you need help with MongoDB or Redis hosting and management, reach out to us at <a title="Contact ScaleGrid Support" href="mailto:support@scalegrid.io" target="_blank">support@scalegrid.io</a>.</p>