Tuesday, April 22, 2008

Setting up load balanced (web farm) sql session state

When web farm is in place, suddenly some of the straightforward tasks are becoming more compelling. This one is about session state data hosted in the sql server.

By design ASP.NET sql session state implementation relies on one table to keep the application names and one to keep sessions.

The sessionId in the session table is created by concatenation of the asp.net session id and the corresponding AppId. The identifier that is unique across farms is achieved by this, but this is coming for the price of applications having to share the same ID in the IIS metabase. Otherwise, session entry created by one server in the farm can't be matched by the other one. It could be really nice if one would be able to assign this identifier in the web.config session state configuration element.

First step we tried was to just find and replace all metabase entries for this web site/application with id that would be the same on all servers. That didn't work and web site was not operable after this change. Ok, after having the problem narrowed down, google search led to the following:


The suggested script ran alright and reported DONE, BUT sites were not operable after running it (always tried on the original working copy of the metabase).

As time was pressing, we just saved the web site to the file and restored it on the other server. After changing values that should be unique for each server (like IP address), we ended up with working solution.

Bits of extra info from analysis:

Sample of a web site identifier in the metabase:
<IIsWebServer Location ="/LM/W3SVC/9999999991"

Concatenated session identifier in the sessions table, first part is the asp.net session id and second the application id in the applications table (hex):
Where highlighted in red are the matching application ids.

The applications table sample content. Highlighted in green are the different IIS app identifiers before fixing the issue.

AppId AppName
-913704004 /lm/w3svc/1275298296/root/site1
-126137139 /lm/w3svc/496522981/root/site1


Javier Crespo said...

Great stuff, I thought that configuring asp.net sesion to sql mode was enough for using it in a web farm. As you said, it'd be really nice if the application Id could be changed in the web.config file, like the machine key can be changed for viewstate.
I didn't know that you had a blog, I'll be keeping an eye on your posts!
Greetings from Brussels.

Stanislav Dvoychenko said...

Thanks Javi, great to get a message from you, I hope you'll have blog soon we all can read. You've got what to say definitely.