Click here to monitor SSC
  • Av rating:
  • Total votes: 28
  • Total comments: 4
Wely Lau

Managing session state in Windows Azure: What are the options?

16 February 2012

Because you can't maintain session state for ASP.NET applications in Azure using the default in-process method, you will need to opt for one of the other available  methods, of which the most popular are  Table storage, SQL Azure, or Windows Azure Caching.

One of the most common questions in developing ASP.NET applications on Windows Azure is how to manage session state. The intention of this article is to discuss several options to manage session state for ASP.NET applications in Windows Azure.

What is session state?

Session state is usually used to store and retrieve values for a user across ASP.NET pages in a web application. There are four available modes to store session values in ASP.NET:

  1. In-Proc, which stores session state in the individual web server’s memory. This is the default option if a particular mode is not explicitly specified.
  2. State Server, which stores session state in another process, called ASP.NET state service.
  3. SQL Server, which stores session state in a SQL Server database
  4. Custom, which lets you choose a custom storage provider.

You can get more information about ASP.NET session state here.

In-Proc session mode does not work in Windows Azure

The In-Proc option, which uses an individual web server’s memory, does not work well in Windows Azure. This may be applicable for those of you who host your application in a multi-instance web-farm environment; Windows Azure load balancer uses round-robin allocation across multi-instances.

For example: you have three instances (A, B, and C) of a Web Role. The first time a page is requested, the load balancer will allocate instance A to handle your request. However, there’s no guarantee that instance A will always handle subsequent requests. Similarly, the value that you set in instance A’s memory can’t be accessed by other instances.

The following picture illustrates how session state works in multi-instances behind the load balancer.

Figure 1 – WAPTK BuildingASP.NETApps.pptx Slide 10


The other options

1.     Table Storage

Table Storage Provider is a subset of the Windows Azure ASP.NET Providers written by the Windows Azure team. The Table Storage Session Provider is, in fact, a custom provider that is compiled into a class library (.dll file), enabling developers to store session state inside Windows Azure Table Storage.

The way it actually works is to store each session as a record in Table Storage. Each record will have an expired column that describe the expired time of each session if there’s no interaction from the user.

The advantage of Table Storage Session Provider is its relatively low cost: $0.14 per GB per month for storage capacity and $0.01 per 10,000 storage transactions. Nonetheless, according to my own experience, one of the notable disadvantages of Table Storage Session Provider is that it may not perform as fast as the other options discussed below.

The following code snippet should be applied in web.config when using Table Storage Session Provider.

<sessionState mode="Custom" customProvider="TableStorageSessionStateProvider"> 
    <add name="TableStorageSessionStateProvider"
        type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageSessionStateProvider" />

You can get more detail on using Table Storage Session Provider step-by-step here.

2.     SQL Azure

As SQL Azure is essentially a subset of SQL Server, SQL Azure can also be used as storage for session state. With just a few modifications, SQL Azure Session Provider can be derived from SQL Server Session Provider.

You will need to apply the following code snippet in web.config when using SQL Azure Session Provider:

<sessionState mode="SQLServer"
sqlConnectionString="Server=tcp:[serverName];Database=myDataBase;User ID=[LoginForDb]@[serverName];Password=[password];Trusted_Connection=False;Encrypt=True;"
cookieless="false" timeout="20" allowCustomSqlDatabase="true" />

For the detail on how to use SQL Azure Session Provider, you can either:

The advantage of using SQL Azure as session provider is that it’s cost effective, especially when you have an existing SQL Azure database. Although it performs better than Table Storage Session Provider in most cases, it requires you to clean the expired session manually by calling the DeleteExpiredSessions stored procedure. Another drawback of using SQL Azure as session provider is that Microsoft does not provide any official support for this.

3.     Windows Azure Caching

Windows Azure Caching is probably the most preferable option available today. It provides a high-performance, in-memory, distributed caching service. The Windows Azure session state provider is an out-of-process storage mechanism for ASP.NET applications. As we all know, accessing RAM is very much faster than accessing disk, so Windows Azure Caching obviously provides the highest performance access of all the available options.

Windows Azure Caching also comes with a .NET API that enables developers to easily interact with the Caching Service. You should apply the following code snippet in web.config when using Cache Session Provider:

<sessionState mode="Custom" customProvider="AzureCacheSessionStoreProvider">
    <add name="AzureCacheSessionStoreProvider"
          type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache"
          cacheName="default" useBlobMode="true" dataCacheClientName="default" />

A step-by-step tutorial for using Caching Service as session provider can be found here.

Other than providing high performance access, another advantage about Windows Azure Caching is that it’s officially supported by Microsoft. Despite its advantages, the charge of Windows Azure Caching is relatively high, starting from $45 per month for 128 MB, all the way up to $325 per month for 4 GB.


I haven’t discussed all the available options for managing session state in Windows Azure, but the three I have discussed are the most popular options out there, and the ones that most people are considering using.

Windows Azure Caching remains the recommended option, despite its cons but developers and architects shouldn’t be afraid to decide on a different option, if it’s more suitable for them in a given scenario.

Wely Lau

Author profile:

Wely is the first Windows Azure MVP in Southeast Asia and is currently working as “Cloud” Solutions Architect in the largest System Integrator in Singapore. Architecting cloud solutions, designing and developing cloud projects, and delivering cloud training are his daily activities as part of driving the adoption of Cloud Computing, specifically on Windows Azure platform. You can visit Wely's website at

Search for other articles by Wely Lau

Rate this article:   Avg rating: from a total of 28 votes.





Must read
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.

Posted by: Wely Lau (not signed in)
Posted on: Monday, December 3, 2012 at 8:02 AM
Message: Hi Hajjat, You can for sure use more than one cache in your application, just need to add them in your web.config. Something like the following:

Then you use the following technique to get specific Cache you preferred. private static DataCacheFactory CacheFactory; CacheFactory.GetCache("b"); Coming back to your question. If you use multiple Azure Cache to store your cached-data per specific group of user, I believe you should able to do that. But if you use Azure Cache for session, I think it may not be a straightforward way to do so. The reason is normally we set the session provider in the web.config file, something like this:
Please refer to some discussion of changing Session State at the runtime:

Posted by: Mohammad Hajjat (not signed in)
Posted on: Monday, December 3, 2012 at 8:02 AM
Message: Hi, nice article. If I have two Azure Caches in two data-centers. Can I programatically assign some requests for some users to use the first cache for storing session state, while using the other cache for the other users? Thanks!

Posted by: Wely Lau (not signed in)
Posted on: Monday, December 3, 2012 at 8:02 AM
Message: Hi Nicholas, ASP.NET Universal Providers from Nugets added necessary assemblies and configurations (Membership, Profile, Session, Roles) inside our project. They will be all pointed to a connection string setting named "DefaultConnection". Simply change the connection string with SQL Azure's should be fine. For example: “data; Initial Catalog=myDatabase; User ID=myUserName; Password=myPassword; Encrypt=true; Trusted_Connection=false; MultipleActiveResultSets=True" providerName="System.Data.SqlClient” Remember also to change to , or else InProc is the default mode. Let me know again should you have any question. Hope this helps. Regards, Wely

Posted by: Nicholas Rogoff (not signed in)
Posted on: Monday, December 3, 2012 at 8:02 AM
Message: Thanks for that overview. There seems to be very little information about this topic. I have just tried using the SQL Azure option, but using the option with the Universal Providers. It sort of half worked! I am a bit confused. Should I be using the mode="SQLServer" with the Universal Providers? Is it reliable in your experience or do I need to go cache? Thanks for your article.


Top Rated

Connecting to SQL Data Warehouse
 The most frustrating thing with any new system is often just working out how to connect to it. Oddly,... Read more...

Getting started with Azure SQL Data Warehouse
 Azure SQL Data Warehouse is an obvious first-step towards migrating on-premise organisational data to... Read more...

Using Azure KeyVault with Node.js
 Some of the most intractable problems of application design are concerned with how to store credentials... Read more...

How to Build and Deploy Azure IaaS VMs with PowerShell
 The whole point of using a cloud service is to be able to use it intensively for a brief period just... Read more...

Azure DocumentDB Server-Side Scripting
 DocumentDB only uses SQL for querying data. To create procedures and functions, you have to flex your... Read more...

Most Viewed

Windows Azure Virtual Machine: A look at Windows Azure IaaS Offerings (Part 2)
 We continue our introduction of the Azure IaaS by discussing how images and disks are used in the Azure... Read more...

An Introduction to Windows Azure BLOB Storage
 Azure BLOB storage is persistent Cloud data storage that serves a variety of purposes. Mike Wood shows... Read more...

An Introduction to Windows Azure Table Storage
 Windows Azure Tables are intended for the storage of large amounts of schemaless data. Tables are just... Read more...

Windows Azure Management Certificates
 Although Windows Azure can be used from the portal, it comes into its own once provisioning,... Read more...

An Introduction to Windows Azure Service Bus Brokered Messaging
 As applications grow, a message queue system soon becomes the best way of achieving scalability. It is... Read more...

Why Join

Over 400,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.