Microsoft Azure provides a platform for ASP.NET applications in the cloud. Very often, these applications are high transaction and mission critical in nature. Therefore, it is very important that these applications are able to scale and that there are no data loss if a web server goes down at any time.
ASP.NET Session State needs to be stored somewhere and its storage becomes a major performance and scalability bottleneck. In Microsoft Azure, you can store ASP.NET Session State in In-proc, SQL Azure (database), Azure Table Storage or in a distributed cache.
In-proc session storage option doesn’t work well in Microsoft Azure architecture. First of all, ASP.NET Session State is not shared between multiple instances of the Web Role in In-proc mode. Secondly, you end up using sticky sessions in Microsoft Azure, which may result in uneven load distribution. Additionally, sticky sessions involve extra configurations on your part as Microsoft Azure doesn’t use sticky sessions by default. Moreover, any Web Role instance going down due to failure or for maintenance will result in session data loss and this is obviously not acceptable.
Azure Table Option
Azure Table Storage is file based ASP.NET Session State provider, which is provided on an ‘as-is basis’ as a code sample, meaning it is not officially supported by Microsoft. It is intended for storing entities that are structured. Even though it is a cheaper option it is still not an ideal place to store ASP.NET Session State primarily because of performance as it is file based.
SQL Database Option
Microsoft Azure SQL Database can also be used as storage for ASP.NET Session State by using conventional ASP.NET SQLServer Mode. But, ASP.NET Session State object is stored in the database as a BLOB and relational databases were never really designed for BLOB storage. This causes performance issues and it is definitely a major scalability bottleneck for your Microsoft Azure ASP.NET application.
Distributed Cache Option
Distributed Cache provides an ideal storage for ASP.NET Session State in Microsoft Azure. For example you can use NCache for Azure which is a Microsoft Azure Distributed Cache for .NET applications. It is extremely fast and more scalable than all other Microsoft Azure options mentioned above and it also replicates sessions so there is no data loss if a cache server ever goes down. Moreover, you eliminate all issues related to session sharing as well as use equal load balancing that ensures full utilization of all of your Azure Web role instances.
How to Configure NCache in Azure ASP.NET Session State Provider?
NCache in Azure has implemented the ASP.NET Session State provider that can be used by Microsoft Azure ASP.NET applications. NCache in Azure uses Microsoft Azure VMs and formulates a dedicated caching tier. ASP.NET applications in Microsoft Azure can then be directed to use this Azure Distributed Cache for ASP.NET Session State storage.
The nice thing about NCache in Azure ASP.NET Session State provider is that it doesn’t require any application code changes. Simply modify your application web.config file as follows to use NCache in Azure as your Distributed Cache for ASP.NET Session State:
<sessionState cookieless="false" regenerateExpiredSessionId="true" mode="Custom"
sessionAppId="NCacheTest" cacheName= "TestCache"
Here are a few important benefits you achieve when you use NCache for Azure as your distributed cache for storing ASP.NET Session State.
- Linear Scalability and Performance: NCache for Azure is based on dynamic clustering protocol, which allows you to add more servers to your cache at runtime. Your application can scale out linearly by adding more servers to your Azure Distributed Cache when your application load grows without changing application architecture.
- Session Replication: NCache for Azure provides reliability support with help of replication. You can take application instances offline for maintenance, patching and for new releases without having to worry about any session data loss.
- High Availability: NCache for Azure provides fault tolerant support of high availability as it is based on hundred percent peer to peer architecture. It is guaranteed that you will not lose any data or have any application downtime in case of any node failure from distributed cache.
Azure Distributed Cache such as NCache in Azure is the best option for storing ASP.NET Session State in Microsoft Azure primarily because of performance, scalability, reliability and high availability features. Microsoft Azure Distributed Cache offered by NCache for Azure is very easy to use and doesn’t require any application code changes.