Connect a web app in Azure App Service to Redis Cache via the Memcache protocol

In this article, you'll learn how to connect a WordPress web app in Azure App Service to Azure Redis Cache using the Memcache protocol. If you have an existing web app that uses a Memcached server for in-memory caching, You can migrate it to Azure App Service and use the first-party caching solution in Microsoft Azure with little or no change to your application code. Furthermore, you can use your existing Memcache expertise to create highly scalable, distributed apps in Azure App Service with Azure Redis Cache for in-memory caching, while using popular application frameworks such as .NET, PHP, Node.js, Java, and Python.

App Service Web Apps enables this application scenario with the Web Apps Memcache shim, which is a local Memcached server that acts as a Memcache proxy for caching calls to Azure Redis Cache. This enables any app that communicates using the Memcache protocol to cache data with Redis Cache. This Memcache shim works at the protocol level, so it can be used by any application or application framework as long as it communicates using the Memcache protocol.

Note

Although this article refers to web apps, it also applies to API apps and mobile apps.

Prerequisites

The Web Apps Memcache shim can be used with any application provided it communicates using the Memcache protocol. For this particular example, the reference application is a Scalable WordPress site which can be provisioned from the Azure Marketplace.

Follow the steps outlined in these articles:

Once you have the Scalable WordPress site deployed and a Redis Cache instance provisioned you will be ready to proceed with enabling the Memcache shim in Azure App Service Web Apps.

Enable the Web Apps Memcache shim

In order to configure Memcache shim, you must create three app settings. This can be done using a variety of methods including the Azure Portal, the classic portal, the Azure PowerShell Cmdlets or the Azure Command-Line Interface. For the purposes of this post, I’m going to use the Azure Portal to set the app settings. The following values can be retrieved from Settings blade of your Redis Cache instance.

Azure Redis Cache Settings Blade

Add REDIS_HOST app setting

The first app setting you need to create is the REDIS_HOST app setting. This setting sets the destination to which the shim forwards the cache information. The value required for the REDIS_HOST app setting can be retrieved from the Properties blade of your Redis Cache instance.

Azure Redis Cache Host Name

Set the key of the app setting to REDIS_HOST and the value of the app setting to the hostname of the Redis Cache instance.

Web App AppSetting REDIS_HOST

Add REDIS_KEY app setting

The second app setting you need to create is the REDIS_KEY app setting. This setting provides the authentication token required to securely access the Redis Cache instance. You can retrieve the value required for the REDIS_KEY app setting from the Access keys blade of the Redis Cache instance.

Azure Redis Cache Primary Key

Set the key of the app setting to REDIS_KEY and the value of the app setting to the Primary Key of the Redis Cache instance.

Azure Website AppSetting REDIS_KEY

Add MEMCACHESHIM_REDIS_ENABLE app setting

The last app setting is used to enable the Memcache Shim in Web Apps, which uses the REDIS_HOST and REDIS_KEY to connect to the Azure Redis Cache and forward the cache calls. Set the key of the app setting to MEMCACHESHIM_REDIS_ENABLE and the value to true.

Web App AppSetting MEMCACHESHIM_REDIS_ENABLE

Once you are done adding the three (3) app settings, click Save.

Enable Memcache extension for PHP

In order for the application to speak the Memcache protocol, it's necessary to install the Memcache extension to PHP--the language framework for your WordPress site.

Download the php_memcache Extension

Browse to PECL. Under the caching category, click memcache. Under the downloads column click the DLL link.

PHP PECL Website

Download the Non-Thread Safe (NTS) x86 link for the version of PHP enabled in Web Apps. (Default is PHP 5.4)

PHP PECL Website Memcache Package

Enable the php_memcache extension

After you download the file, unzip and upload the php_memcache.dll into the d:\home\site\wwwroot\bin\ext\ directory. After the php_memcache.dll is uploaded into the web app, you need to enable the extension to the PHP Runtime. To enable the Memcache extension in the Azure Portal, open the Application Settings blade for the web app, then add a new app setting with the key of PHP_EXTENSIONS and the value bin\ext\php_memcache.dll.

Note

If the web app needs to load multiple PHP extensions, the value of PHP_EXTENSIONS should be a comma delimited list of relative paths to DLL files.

Web App AppSetting PHP_EXTENSIONS

Once finished, click Save.

Install Memcache WordPress plugin

Note

You can also download the Memcached Object Cache Plugin from WordPress.org.

On the WordPress plugins page, click Add New.

WordPress Plugin Page

In the search box, type memcached and press Enter.

WordPress Add New Plugin

Find Memcached Object Cache in the list, then click Install Now.

WordPress Install Memcache Plugin

Enable the Memcache WordPress plugin

Note

Follow the instructions in this blog on How to enable a Site Extension in Web Apps to install Visual Studio Team Services.

In the wp-config.php file, add the following code above the stop editing comment near the end of the file.

$memcached_servers = array(
    'default' => array('localhost:' . getenv("MEMCACHESHIM_PORT"))
);

Once this code has been pasted, monaco will automatically save the document.

The next step is to enable the object-cache plugin. This is done by dragging and dropping object-cache.php from wp-content/plugins/memcached folder to the wp-content folder to enable the Memcache Object Cache functionality.

Locate the memcache object-cache.php plugin

Now that the object-cache.php file is in the wp-content folder, the Memcached Object Cache is now enabled.

Enable the memcache object-cache.php plugin

Verify the Memcache Object Cache plugin is functioning

All of the steps to enable the Web Apps Memcache shim are now complete. The only thing left is to verify that the data is populating your Redis Cache instance.

Enable the non-SSL port support in Azure Redis Cache

Note

At the time of writing this article, the Redis CLI does not support SSL connectivity, thus the following steps are necessary.

In the Azure Portal, browse to the Redis Cache instance that you created for this web app. Once the cache's blade is open, click the Settings icon.

Azure Redis Cache Settings Button

Select Access Ports from the list.

Azure Redis Cache Access Port

Click No for Allow access only via SSL.

Azure Redis Cache Access Port SSL Only

You will see that the NON-SSL port is now set. Click Save.

Azure Redis Cache Redis Access Portal Non-SSL

Connect to Azure Redis Cache from redis-cli

Note

This step assumes that redis is installed locally on your development machine. Install Redis locally using these instructions.

Open your command-line console of choice and type the following command:

redis-cli –h <hostname-for-redis-cache> –a <primary-key-for-redis-cache> –p 6379

Replace the <hostname-for-redis-cache> with the actual xxxxx.redis.cache.windows.net hostname and the <primary-key-for-redis-cache> with the access key for the cache, then press Enter. Once the CLI has connected to the Redis Cache instance, issue any redis command. In the screenshot below, I’ve chosen to list the keys.

Connect to Azure Redis Cache from Redis CLI in Terminal

The call to list the keys should return a value. If not, try navigating to the web app and trying again.

Conclusion

Congratulations! The WordPress app now has a centralized in-memory cache to aid in increasing throughput. Remember, the Web Apps Memcache Shim can be used with any Memcache client regardless of programming language or application framework. To provide feedback or to ask questions about the Web Apps Memcache shim, post to MSDN Forums or Stackoverflow.

Note

If you want to get started with Azure App Service before signing up for an Azure account, go to Try App Service, where you can immediately create a short-lived starter web app in App Service. No credit cards required; no commitments.

What's changed