Using Redis as an object cache for Wordpress can seriously speed up database requests. Removing load from the DB server and allowing you to scale with your users by replicting the nodes. Elasticache make its easy and relavity cheap to achieve competative website speeds for dynamic viewers. I will assume you already have wordpress working from an EC2 instance, this will not work from outside of a Amazon Virtual Private Cloud.

Step 1 - Add Wordpress plugin.

Install the Redis Object Cache plugin.

This plugin will give an easy way to flush the cache from the wp-admin as well as handle all the uploading and invalidating of the Redis caching nodes.

Step 2 - Create ElastiCache Instance.

My least favorite part of AWS was learning how to set up permission and keeping everything inside the correct VPC, without fail, I always manage to mess it up the first time. Elasticache was no exception but by making note of your EC2 instance settings you can get through this without having AWS silently fail, forcing you to tear down your entire system trying to the problem.

  • Go to your EC2 dashboard and highlight the EC2 instance that contains your WordPress install to bring up its details.

  • Make a note of your Availability zone, all the security groups, the VPC ID and the subnet ID.

  • Go to Elasticache settings page and on the left-hand menu click on Redis and click Create.

  • Update the Redis Settings as follows.

Redis Settings

Note with these setting this Redis instance which will cost a few hundred euros a month if left on, so be careful and choose a smaller instance if that works for you. Leave the port as the standard and change the name for whatever you want.

Now for the advanced setting, this is where you will need your notes from earlier.

Redis Settings

  • Under Subnet group, select create new from the dropdown. The name and description can be whatever you want. Select the same VPC ID and Subnet group as used for the EC2 instance. Preferred availability zone(s) should also match the EC2 instance.

  • Under Security, pick a security group that is already attached to your to your EC2 instance.

  • Leave everything else the same and click Create at the button of the page.

This will take some time to create so whilst that is happening let’s update the security groups to allow Redis to communicate with EC2 using the standard port.

  • Go to your EC2 dashboard, on the left-hand menu under NETWORK & SECURITY select Security Groups.

  • Find the security group you attached to the Redis instance and click edit. Then click Add Rule.

  • The type should be Custom TCP Rule and if you did not change the Redis port, change the Port Range to 6379 and Source to anywhere.

Redis Secruity Settings

Step 3 - Testing Redis Instance.

Lets make sure everything is working, we will do this by trying to connect to Redis using telnet from our EC2 instance.

  • Go back to you the ElastiCache dashboard and find you redis instance, click on the instance to show more details and make a note of the Primary Endpoint.

  • SSH into your EC2 instance using the terminal.

  • Install telnet by typing sudo yum install telnet

  • Then try to connect to your Redis instance using telnet. telnet staging.ixcg9t.0001.euw1.cache.amazonaws.com 6379 Replace the URL with your primary endpoint.

You should see something like this

[ec2-user@ip-10-0-0-68 ~]$ telnet staging.ixcg9t.0001.euw1.cache.amazonaws.com 6379
 Trying 10.0.0.63...
Connected to staging.ixcg9t.0001.euw1.cache.amazonaws.com.
Escape character is '^]'.

Exit by typing

quit

Step 4 - Connect Wordpress to Redis.

The Redis Object Cache defaults to a local install of Redis. We need to update that to point to our elsticache Primary Endpoint. This is done by defining a PHP constant.

Edit the wp-config.php and add this just before the DB_NAME constant.

define('WP_REDIS_HOST', 'wordpress.ixcg9t.0001.euw1.cache.amazonaws.com');

Update the url to match your primary endpoint.

The last thing we need to do is the access the Redis plugin setting an click Enable Object Cache to activate.