Azure Redis Cache with Multiple Web Applications

July 8, 2014

no comments

As you probably know, Microsoft released a preview of the Azure Redis a couple of months ago (if not, read here: http://msdn.microsoft.com/en-us/library/dn690523.aspx).

Along with it, Microsoft released a walkthrough of how to use Azure Redis as a session state provider for ASP.NET web applications:

http://blogs.msdn.com/b/webdev/archive/2014/05/12/announcing-asp-net-session-state-provider-for-redis-preview-release.aspx

Most of you probably tried it and found it to be working. However, that blog post does not explain what you should do if you have multiple web applications and you want to use the same Azure Redis cache for all of them.

So first, yes, it’s possible. You have two options to use the same cache service for multiple web apps:

Key Prefix

Create a key prefix for each of your web applications. This is the easiest way to distinguish keys of different apps, and to make sure you don’t accidentally override session data of another app.
To set the prefix of the session key, set the applicationName attribute in the session provider settings, as demonstrated below:

<sessionState mode="Custom" customProvider="MySessionStateStore">
  <providers>
    <add name="MySessionStateStore"
      type="Microsoft.Web.Redis.RedisSessionStateProvider"
      host = "[cache_name].redis.cache.windows.net"
      port = "6379"
      accessKey = "[cache_key]"
      ssl = "false"
      throwOnError = "true"
      retryTimeoutInMilliseconds = "0"
      applicationName = "[your_app_name]"/>
  </providers>
</sessionState>
  1. Database Number

Place each web application in a separate Redis database (read more about Redis databases here: http://www.rediscookbook.org/multiple_databases.html).
The advantage of using databases over application name is that you can use commands such as FlushDb to clean the content of a database. On the other hand, databases are identified by a number (Azure Redis cache has 16 databases – 0-15), so it may be harder to track which web application uses which database number.
To set the database number, set the databaseId attribute in the session provider settings, as demonstrated below (default value is 0):

<sessionState mode="Custom" customProvider="MySessionStateStore">
  <providers>
    <add name="MySessionStateStore"
      type="Microsoft.Web.Redis.RedisSessionStateProvider"
      host = "[cache_name].redis.cache.windows.net"
      port = "6379"
      accessKey = "[cache_key]"
      ssl = "false"
      throwOnError = "true"
      retryTimeoutInMilliseconds = "0"
      databaseId = "1" />
  </providers>
</sessionState>
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*