How to Set Up Azure Redis Cache for WordPress
Azure’s Redis Cache is an excellent service that can speed up the performance of web applications. Azure’s Redis Cache is a PaaS offering that allows you to take advantage of Object Caching in WordPress, which allows WordPress running in an Azure App Service to become quicker and more responsive.
Note: You should only use Azure Redis Cache with WordPress if your WordPress Installation is running in an Azure App Service and you want it to have better performance. This should be one of the last things you do to improve the performance of WordPress in Azure app services, as we have written about in a previous blog post about how to make WordPress faster in Azure App Services.
Security Notice: You should only use Azure Redis Cache over port 6380 and to only allow SSL traffic. We want this for security reasons and for reliability reasons for the communication between Azure Redis Cache and WordPress.
What is Object Caching?
Object Caching differs from Page Caching in that Object Caching caches the results of common queries to the database so that your web application can respond quicker. This also takes some pressure off of the database and it will allow your web application to handle more concurrent visitors. WordPress is quite a database dependent application, which means that object caching can have great results, particularly with Azure Redis Cache. When using Azure Redis Cache, the results of common queries to the database are stored in low latency memory, which can greatly improve the performance of your WordPress application running in an Azure app service.
Deploying Azure Redis Cache
The first thing you should do when setting up Azure Redis Cache is to first deploy your Azure Redis Cache service. To do that, log in to the Azure portal and search for Azure Cache for Redis.
You need to create a DNS name for your Azure Redis Cache instance, choose the pricing tier, and choose to NOT unblock port 6379.For security and reliability reasons, you always want to use port 6380, which forces SSL for communication between Azure Redis Cache and WordPress. If this is a production WordPress installation, you should choose something within the Standard Tier, since there is a 99.5% SLA. In this case, we have chosen a C1 instance with 1 GB of cache space. The amount of space you need depends on the size of your database and the size of the results of common queries to your database. This is something you can determine with a little trial and error.
You should also note that you can always increase the tier of your Azure Redis Cache tier, but you cannot scale it down. If you want to scale it down, you will need to deploy a new instance and switch the configuration to the new instance.
Now that Azure Redis Cache has been deployed, we need to install a plugin and add an extension to your app service.
Installing Redis Object Cache Plugin
In order for your wordpress installation to correctly cache objects, you need an object caching plugin that supports Azure Redis Cache. There are a few of them to choose from, but we have used Redis Object Cache. You just need to install and activate this plugin. Once it is activated, you can move on to the next step.
Preparing your Azure App Service
In order for your Azure App Service to know how to cache objects from WordPress to Azure Redis Cache, we need to create an extension. The extension depends entirely on the version of PHP you are using and if your app service is configured to use 32 bit or 64 bit. To create the extension, you first need to download the precompiled Redis DLL for Windows, since we are using an Azure App Service (not Linux). You can get the DLL from the Redis Extension website. From here, click on the latest stable version and click on DLL next to the Windows icon. Be sure to choose the most recent stable version, and choose the x86 version for 32 bit or x64 for 64 bit. You will also want to choose the thread safe version.
Once you have downloaded the DLL, you will want to unzip it. We are now ready to create an extension folder in your App Service directory. To do this, click on Advanced Tools in your app service, and go to the debug console. From here, navigate to D:\home\site and create a new folder called ext. Now, go to the ext folder and upload the php_redis.dll that you just downloaded and unzipped. You can upload this by dragging and dropping the php_redis.dll in your browser.
Once you have uploaded the php_redis.dll to the ext folder, you now need to go back to the D:\site directory and create a new folder called ini. Inside the D:\site\ini folder, you need to create a file called extensions.ini and add the following line to that file:
Now you can save the extensions.ini file. The last thing we need to do is add the Redis Object Cache plugin parameters to the wp-config.php file, which is located in the D:\home\wwwroot directory. Add the following configuration:
define('WP_REDIS_SCHEME', 'tls'); define('WP_REDIS_CLIENT', 'pecl'); define('WP_REDIS_HOST', 'HOST NAME FOR YOUR AZURE REDIS CACHE'); define('WP_REDIS_PORT', '6380'); define('WP_REDIS_DATABASE', '0'); define('WP_REDIS_PASSWORD', 'AZURE REDIS CACHE PRIMARY ACCESS KEY'); define('WP_CACHE_KEY_SALT','UNIQUE-NAME-PER-APP-SERVICE');
The host name can be found on the overview blade of your Azure Redis Cache resource and the primary access key can be found in the Access Key blade. Fill in the correct information to this configuration and save the wp-config.php file. Now restart your app service and enable object caching in the Redis Object Cache plugin. You should now see that the host name in the Redis Object Cache plugin is what you defined it in the wp-config.php file and your Azure Redis Cache memory usage should be above 0. This will force the connection between WordPress and your Azure Redis Cache resource to only communicate over SSL (port 6380), use a TLS handshake for SSL, use the PECL client that we downloaded and installed. This will give you the best results for using Azure Redis Cache and WordPress.
You should also begin to notice that your WordPress instance is faster than it was before. Overall, if you are fine with a small increase in cost, Azure Redis Cache is a great way to improve the performance of not only WordPress, but of most web applications.