Friday, December 17, 2010

ASP.NET SQL based state management: mirroring state management database

Using Microsoft SQL Server for ASP.NET state management you may like to use SQL database mirroring to increase Web application availability while using NLB Web cluster. Basically, ASP.NET supports three ways for application state management:

  • in-process: unsuitable for NLB based architectures, since state data are managed in-process on one Web frontend node and are unavailable for any other nodes in failure case
  • using ASP.NET own state management service: this proprietary service of ASP.NET requires dedicated installation and management, using TCP-based connection from each Web frontend node
  • using SQL database: this case covers management of the state data in a dedicated SQL database available for any node from ASP.NET NLB cluster (configuration assumed)

Once the state management database is going down, all the session data becomes unavailable and may impact availability of the ASP.NET application. To avoid it, there are different approaches: among them usage of mirrored databases as one of the features came with SQL 2005 and higher.

The important things to know while you configure your ASP.NET application to get state managed using SQL mirrored database:

  1. Configure SQL database mirroring properly, refer to manual here (Microsoft)
  2. Pay attention and configure your connection string ON EACH NLB node properly:
    • add allowCustomSqlDatabase=”true” attribute to the sessionState element parameters, do not ask, just add;
    • add “initial catalog=ASPState” section to the sqlConnectionString – even if you use the standard database named ASPState (as installed by default using aspnet_regsql utility), you need this connection string extension;
    • add “failover partner=<name or IP address of the SQL mirror>”;
    • check “data source=<name or IP address of the master SQL server>” for validity;
  3. Since some of state data is encrypted, you have to take care about the encryption process: all the Web frontend nodes must be able to encrypt/decrypt state management data same way – otherwise the encrypted data becomes unusable. For this goal ensure, that all the nodes have same <machineKey …/> setting in web.config of the application. Refer to Microsoft article, how to configure and generate this setting (quick start – use IIS manager)
    image

If you paid enough attention to the conditions listed above, your application should just work in NLB with mirrored SQL state management database behind. Enjoy!

No comments: