WordPress Redis Object Cache | The Fancy Robot

Set Up Redis Object Cache for WordPress

When it comes to WordPress speed optimizations, there are plenty of options. In fact, it can be a bit overwhelming trying to figure out what the best options are. The average WordPress user goes straight for one of the many plugins, such as W3 Total Cache, WP Rocket, or SP Super Cache. I hate to say it, but your best option is to not use any of them. the issue with those plugins, aside from the security issues (boy have I seen some real messes because of those plugins), is that they are all PHP level solutions. The best solution to speed up WordPress is to use technologies that exist outside of the WordPress ecosystem. Enter Redis

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.

So what does that mean? It means that we can store data that WordPress queries from the database in Redis for quick and easy access, and it’s a persistent data store. This speeds up your site, and greatly reduces server load, by storing all of that data so that WordPress doesn’t have to run multiple queries to the database every time a page is loaded. We call this an object cache.

What’s Wrong With the Built-in WordPress Object Cache?

This question could be answered with a whole new article. Thankfully, Zack Tollman has already written it. It’s a little dated and refers to Memcached instead of Redis, but the overall point of the article still applies today. To sum up, there isn’t anything wrong with the built-in object cache, but a persistent cache is more efficient.

Step 1 – Install Redis

This tutorial assumes you have your site set up on an Ubuntu or Debian server.

We’re going to install Redis using apt. This is the easiest method and it ensures you’re using a stable version. Before installing anything you should always make sure apt is up to date by typing:

sudo apt-get update && apt-get upgrade

Now it’s time to install Redis

sudo apt-get install redis-server

Now you need to check the configuration. Unless you know for a fact that your situation is different, you want to make sure that Redis is only available to localhost. This should be the default configuration but check it anyway just to be sure. This is a very important security measure.

sudo nano /etc/redis/redis.conf

Find this line and remove the # if there is one at the beginning of the line. Make sure there are no other bindings in the configuration.

bind ::1

Then add this to the end of the file

maxmemory 50mb
maxmemory-policy allkeys-lru

Please note: you can increase the maxmemory setting according to your needs, but 50mb will be plenty for most WordPress installs

Save the Redis configuration file and restart Redis

sudo systemctl restart redis

And make sure your configuration is correct

sudo netstat -lnp | grep redis

You should see output similar to this:

tcp    0    0*    LISTEN    6888/redis-server 1

If there is more than one line in the output, check your configuration and make sure that is the only binding in the configuration.

Step 2 – Install WP Object Cache plugin

There are a couple Redis object cache plugins, but the Redis Object Cache plugin is the easiest to set up.

Install the plugin using the standard procedure for install WordPress plugins.

Now go to Settings > Redis and click the “Enable Object Cache” button.

Step 3 – Add salt to wp-config.php (optional)

This setting is really only important if you’ve got more than one resource accessing the same Redis data store (e.g. WordPress multisite install). However, I would recommend setting it anyway, if for no reason other than planning ahead in case you need to add an additional resource on your server in the future.

Open up your wp-config.php file and add this at the end (you can enter any string you want for the salt. I personally use random.org to generate a random string)

define( 'WP_CACHE_KEY_SALT', 'cFGUIzDeyA' );

Make sure you flush and restart your cache after setting the key salt.

That’s all folks!

Now you’ve got a Redis object cache for your WordPress website. There are many configuration options for the plugin that are outside the scope of this article. Please visit https://github.com/tillkruss/redis-cache for plugin documentation and support.

Please leave any questions, comments, or concerns in the comment section!

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
ProWeb365 Recent comment authors
newest oldest most voted
Notify of

This is truly helpful, thanks.