Azure Redis Cache

By Nishanth A on November 22, 2016

Azure Redis Cache is based on the popular open-source Redis cache.  It gives you access to a secure, dedicated Redis cache, managed by Microsoft and accessible from any application within Azure.


  • Redis is an advanced key-value store, where keys can contain data structures such as strings, hashes, lists, sets and sorted sets. Redis supports a set of atomic operations on these data types.
  • Redis also supports trivial-to-setup master-subordinate replication, with very fast non-blocking first synchronisation, auto-reconnection on net split and so forth.
  • Updating cache values without having to retrieve the item from the cache.
  • Other features include transactions, publish/subscribe, Lua scripting, keys with a limited time to live and configuration settings to make Redis behave like a cache.
  • Azure Redis Cache uses Redis authentication and also supports SSL connections to Redis.

Azure Redis Cache is available in the following tiers:

  • Basic—Single node, multiple sizes, ideal for development/test and non-critical workloads. The basic tier has no SLA.
  • Standard—A replicated cache in a two node Primary/Secondary configuration managed by Microsoft, with a high availability SLA.
  • Premium—The new Premium tier includes a high availability SLA and all the Standard-tier features and more, such as better performance over Basic or Standard-tier Caches, bigger workloads, disaster recovery and enhanced security.

Additional features include:

  • Redis persistence allows you to persist data stored in Redis cache. You can also take snapshots and back up the data which you can load in case of a failure.
  • Redis cluster automatically shards data across multiple Redis nodes, so you can create workloads of bigger memory sizes (greater than 53 GB) and get better performance.
  • Azure Virtual Network (VNET) deployment provides enhanced security and isolation for your Azure Redis Cache, as well as subnets, access control policies and other features to further restrict access.

Create a Redis Cache using the Azure Management portal

In the Azure Management portal you can create a Redis Cache by selecting Redis Cache-> Create   as shown in Figure1


Next you will need to first select a name (DNS name) that is unique. The resource group your cache will belong to, and the location. You should strive to select a location that is in the same region as the services that will use the cache for best performance. The DNS name for your cache will be {cache name}

Figure 2

After the cache is created you will need two pieces of information from the Redis Cache blade to start using the cache in your code: the host name and an access key. You can access both of these in the Redis Cache blade as shown in Figure3



Using Azure Redis

Storing Session State in Azure Redis    

Sometimes your web application requires state but cookies aren’t sufficient for your needs. Session state provides a server managed state mechanism that can help meet an application’s needs for storing state. Microsoft has provided a way to easily integrate Azure Redis Cache with shared session in .Net applications.

First, you’ll need to install the required Nuget packages into your project. Click on “Manage Nuget Packages” in your project and search for RedisSessionStateProvider. You should see Microsoft.Web.RedisSessionStateProvider by Microsoft. Click on that one and install it. Next you’ll need to configure the applications shared session state to use the Azure Redis Cache you have created. To do this you’ll need to modify the sessionState portion of the Web.config.


Once you have the config done, you’re ready to start utilizing the cache as normal. An example controller is shown below: 


A few notes about using Azure Redis Cache for Session State:

  • Data types must be serializable
  • Make sure to remove the InProc session state provider if you were using that before from the Web.config

Storing Html Output Cache in Azure Redis

The output cache is a great way to improve your sites performance by storing html in a cache to reduce response times and load for your site. Microsoft has also provided a way to easily configure your .Net application to use Azure Redis Cache for an Html output cache.

The first step is to add the Nuget package to your project. Click on “Manage Nuget Packages” in your project and search for RedisOutputCacheProvider. You should see Microsoft.Web.RedisOutputCacheProvider by Microsoft. Click on that one and install it. Next is to configure the output cache to use the Azure Redis Cache by altering the caching section in the Web.config. 


Now you can start defining what you want to store in the output cache. You can do this in the controller or in a view. You can simply specify a controller action result to store in the output cache by adding the OutputCache attribute to the action method.


You can also specify in the view by doing 


This very simple code will store the pages in the cache for the specified amount of time. You can also configure the cache to vary by headers, parameters, etc.

Custom use of the Azure Redis Cache

To configure the Azure Redis Cache for general use, simply install the StackExchange.Redis by Stack Exchange Nuget package. Once installed, you can connect and interact with the cache easily. Below is a sample controller which utilizes a connection to the cache to increment a value. That value is also retrieved and displayed on a different view utilizing the same connection property.



Leave a Reply

Contact us!