[Azure] WordPress and Azure Redis Cache
I a quest to optimize the performance of my WordPress powered site, I thought I’d give Redis Cache a go. Redis Cache has become the industry default when it comes to caching in a key-value style. Azure has a Redis Cache offering which you can use to enhance the performance of any app. If you’re not familiar with caching: it’s a mechanism to eliminate timely data retrieval actions. Caching can be implemented several different places: you web browser uses caching so that it does not have to retrieve all files on every load. Redis Cache is usually more targeted towards scenario’s like database connections. Instead of getting items from a database table, which usually is relatively slow, you can get them from the cache instead.
Creating a Redis Cache instance
This is the easiest step, as with a lot of “create x on Azure” steps. You simply head over to the Azure Portal and create a new Redis instance. Also similar to other Azure steps, you’ll need to select your subscription, resource group and a pricing tier (no, it’s not free).
Preparing your WordPress instance
To use Redis Cache, you’ll need to install a plugin. I’ve chosen to use the Redis Object Cache plugin. This one supports multiple clients to connect with Redis. In order to use one of these clients, you’ll need to install extensions for PHP so that the clients become available. Here’s what you need to do:
- Download the extension you prefer. I went with the redis php extension. When hosting on Windows, you’ll need the precompiled DLL which you can download here. Make sure you pick the correct version for the PHP version you’re running (which you can check in the application settings).
- Open up Kudu by clicking the “Advanced Tools” option in your app service instance.
- Create a new folder ext in the D:\home\site path.
- Upload the DLL for the extension there, you can simply drag & drop it in any modern browser.
- Next, create a folder ini, also in the D:\home\site path.
- Here you’ll need to create a new file called extensions.ini which will point PHP to the extension we just uploaded. Copy/paste the following:
; Enable Extensions extension=d:\home\site\ext\php_redis.dll
Configuring the plugin
With all the prerequisites set-up, we can now configure the plugin. This plugin does not feature a UI for configuration so instead we’ll need to edit the config file ourselves.
- Again go to Kudu
- Navigate to d:\home\site\wwwroot and edit wp-config.php
- Copy / paste the following:
// Redis Cache plugin configuration define('WP_REDIS_SCHEME', 'tcp'); define('WP_REDIS_HOST', '<redishost>'); define('WP_REDIS_PORT', '6379'); define('WP_REDIS_DATABASE', '0'); define('WP_REDIS_PASSWORD', '<yourpass>'); define('WP_CACHE_KEY_SALT', '<prefix>');
Some explanation of the settings you need to change:
- <redishost>: replace this with the URL of your redis instance, which would be <name>.redis.cache.windows.net
- <yourpass>: open up “Access Keys” for your Redis instance and copy/paste the primary access key
- <prefix>: this is especially useful when you’ve also got a staging instance of your website and you don’t want to create an additional cache instance. The prefix will allow you to prefix all of the cache keys to prevent multiple WordPress instances using the same cache keys (and thus getting each others data).
- I also tried to secure the connection by using the SSL port (6380 by default), but that didn’t work for me. If you’ve got that figured out, drop the solution in the comments please!
Verifying it all works
Now that you’ve got everything set-up and configured, you can enable the plugin from the WordPress admin section. It’s found under Settings > Redis. When the connection is up & running, it should display the following:
You can also verify that your cache is being populated with items by downloading the Redis Desktop Manager and inspecting the redis databases in your instance.
Note: using Redis Cache is just one of the ways to improve the performance of your site. There are numerous plugins for WordPress which help to improve the speed of your database, implement front-end caching and other optimizations. You’ll need to pick the one(s) right for you.
Leave a Comment