Using Redis Caching to Speed Up WordPress

Introduction

Redis can be used to speed up WordPress and as a key value-based database, cache and message broker system. It supports multiple data structures like strings, hashes, lists, etc. High availability is provided by Redis and can be used as a standalone database server. It can also be used with relational databases like MySQL to speed performance by caching.

Object Cache

In simple words, object cache-store database queries in some in-memory so next time when the same query is required then the server doesn’t have to utilize the database resources. WordPress has built-in object cache but this is not persistent, which simply means that it only stores the query in the cache as long as the query is running. Cache has no visible significance if that same query runs after a while.

To solve this we can implement an in-memory database layer which will save all our cached queries until the data is updated. Troubling databases will be removed like databases like MySQL on every request and will not only speed up the website but will also save resources.

Now in this tutorial, we are going to implement object cache to WordPress using Redis object cache.

Using Redis Caching to Speed Up WordPress
Using Redis Caching to Speed Up WordPress

Do You Need Redis Object Caching?

A static website where all the content is statically generated without any database query then object caching will just be an extra effort for you as it won’t affect your website’s performance.

If you have a dynamic website that utilizes MySQL resources with every request then you surely need Redis object cache to improve performance and reduce resource usage.

Getting Started

This tutorial will take you through different steps required to install and using Redis caching to speed up WordPress.

Step 1

Installing Redis

We need to have the Redis server up and running so we can integrate it with WordPress. Steps to install Redis depends upon your server. you are using it. I am adding some quick steps to install Redis on the ubuntu server. If you want a full guide for Redis installation then please comment and I will provide the help needed.

To install the latest version of Redis, we have to use apt to install it from the official Ubuntu repositories like any other Ubuntu package.

Follow these commands to install Redis :

  • Command 1: sudo apt update , this will update the apt repository on your server.
  • Command 2: sudo apt install redis-server , this will initiate the process of installing Redis server and its dependencies .
  • Command 3: sudo nano /etc/redis/redis.conf , this will open redis config file with command line text editor. Now we have to look for supervised directive. By default this is set to no. We have to look for supervised directive and change no with systemd.
  • Command 4: sudo systemctl restart redis.service , With this command we will restart Redis service for changes to take effect.
  • Command 5: sudo systemctl status redis , With this command we will check if redis if working fine. If every thing went well without error then we should get the similar output as below :
  redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2018-06-27 18:48:52 UTC; 12s ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
  Process: 2421 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 2424 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 2445 (redis-server)
    Tasks: 4 (limit: 4704)
   CGroup: /system.slice/redis-server.service
           └─2445 /usr/bin/redis-server 127.0.0.1:6379

By default your redis if working at 127.0.0.1:6379.

Step 2

Installing Required Plugin

Any decent caching plugin which supports Redis will do the job just fine. I am suggesting one of the best and most popular choices based on my experience so you don’t end up using something that is not a good fit.

Redis Object Cache Plugin

The plugin is continuously managed and has over 50,000+ downloads and best thing is this plugin is focused on Redis only so this is my top pick for this tutorial.

This plugin by default try to connect to Redis with TCP at 127.0.0.1:6379 and select database 0. You have to of course edit your default configurations according to your requirement.

You will have to edit wp-config.php file with your connection parameters, Nowhere you can use this in a different manner. There are different approaches this plugin allows you to follow

  • Replication (Master-Slave):
define( 'WP_REDIS_SERVERS', [
    'tcp://127.0.0.1:6379?database=5&alias=master',
    'tcp://127.0.0.2:6379?database=5&alias=slave-01',
] );
  • Replication (Redis Sentinel):
define( 'WP_REDIS_CLIENT', 'predis' );
define( 'WP_REDIS_SENTINEL', 'mymaster' );
define( 'WP_REDIS_SERVERS', [
    'tcp://127.0.0.1:5380',
    'tcp://127.0.0.2:5381',
    'tcp://127.0.0.3:5382',
] );
  • Sharding:
define( 'WP_REDIS_SHARDS', [
    'tcp://127.0.0.1:6379?database=10&alias=shard-01',
    'tcp://127.0.0.2:6379?database=10&alias=shard-02',
    'tcp://127.0.0.3:6379?database=10&alias=shard-03',
] );
  • Clustering (Redis 3.0+):
define( 'WP_REDIS_CLUSTER', [
    'tcp://127.0.0.1:6379?database=15&alias=node-01',
    'tcp://127.0.0.2:6379?database=15&alias=node-02',
] );<h3>WP-CLI Commands</h3>

If you want to learn more about these methods please ask in comments and I will provide details in a new post.

Conclusion

Leave a Comment