Use Redis as a cache on WordPress

On a busy website, there can be several reasons why your site is rendering slowly.  You might have a lot of poorly developed plugins or a poorly tuned server, some of the reasons could be front end related and some just related to the way WordPress runs and uses the database. A cache can make a big difference on a busy server. It can reduce lookup to the database and speed up the page generation process.

MySQL is a fast database and if you have read-only slaves added in your database cluster your site can benefit tremendously from them. However, for a small business or a blog which cannot afford to use a database cluster, a cache solution will speed up things by a lot. The popular in-memory stores are Memcached and Redis and they are amazingly fast as they store data in the server memory.

The Amazon ElastiCache service supports both Memcached and Redis which can be used to cache frequently used items by WordPress. If you can afford a distributed cache, then it is definitely a good service to consider. If your site is still quite small but you want to optimize it and make it fast, consider installing Redis on your server and using a WordPress plugin which makes use of this cache.

I am not going to go over which solution or service is the best for your site or review Memcached and Redis. I choose to go with Redis and in this post I am going to over the installation procedure for Redis on an Amazon LightSail Bitnami Ubuntu WordPress server.

Procedure

You should have full root access to the server as you will need to install redis and in some cases the php extension. If you don’t already have Redis installed on your server take a look at my earlier post which will give you a good idea on how to go about installing a Redis server.

Steps

Login to your server and create a temporary work directory and cd to it. This directory will serve as our base for compiling the software. After installation is done, you can go ahead and remove the temporary work directory. Use wget to download the software, extract it using tar and cd into the source directory.

mkdir ~/work && cd ~/work
wget https://pecl.php.net/get/redis-VERSION.tgz
tar -xvf redis-VERSION.tgz
cd redis-VERSION
phpize
./configure
make
sudo make install

Replace VERSION. I used redis-4.2.0.tgz the latest at the time of writing this post.

Next step is to enable the module in the php.ini file by adding this line to the end:

extension=redis.so

If you are not sure where your PHP Configuration ini file is you can run this command to confirm its location.

php -ini | grep 'Loaded Configuration'
Loaded Configuration File => /opt/installed_dir/etc/php.ini

Check that the module was correctly installed with the following command:

php -m | grep redis

At this point our installation is complete and our extension is loaded. If possible we should confirm by stopping and starting our site.

sudo /opt/bitnami/ctlscript.sh stop
sudo /opt/bitnami/ctlscript.sh start

Next step is to install a plugin which will enable ObjectCache. I installed the Redis Plugin from https://wordpress.org/plugins/redis-cache/

You can try other Redis plugins if you wish.

Follow the plugin installation guide which is as simple as installing, activating the plugin and enabling the Object Cache under Settings -> Redis.

From the command line if you run the commands to lookup statistics, you should see some data showing cache usage.

redis-cli --stat
Redis cache usage

Disclosure:

I am not promoting any plugin nor am I getting paid to list any plugins here. The views are my own and this post is just to highlight how to use Redis with WordPress. You can choose either Memcached or Redis or any other service not listed here and the appropriate plugin for it.

Further Reading

Photo Credit

unsplash-logoGuillaume Jaillet

Leave a Reply