Saturday, September 29, 2012

Preserving State Between Web Server Instances

One of my web site applications has been giving me headaches.  Every few days, I would encounter an incident when one of the user session seem to lockout the application causing other users to time out.  I used Microsoft IIS' application pool function to separate it from the other apps running on the same server so I can easily restart its pool without affecting the others.  But still, having to restart the pool every so often is a chore.

I decided to spread out the load to several threads by initially setting up a Web Garden.  A Web Garden uses multiple worker processes and uses a queue manager to process incoming requests to the application pool.  Unlike a Web Farm, which comprises of multiple web servers, a Web Garden just uses multiple worker threads in the same multi-CPU server.

The problem I encountered with running apps in a web garden is that values stored in Session variables are local to the worker thread that was assigned to it.  So if IIS decide to send you to a different worker thread in subsequent requests, the Session variables used in the other worker thread will completely disappear.  I thought of using ViewState at first so that the values are stored on the client side and just passed back on every request.  Unfortunately, ViewStates only seem to work for postbacks to the same URL.  Once the URL changes, the previous ViewState value is also gone.

I found this interesting article on how to use OutOfProc Session State servers.  By using the StateServer instance, one can save session variables between apps or even between servers.  Its not as robust as the SQLServer solution, but its relatively lower overhead.  One only need to add the following lines in the web.config's system.web section:


< sessionState mode="stateserver" stateConnectionString="tcpip=127.0.0.1:42424" />

Will be observing if performance improves with this modest adjustment on the design.

1 comment:

Rom Gapuz said...

Hi sir, on a much larger scale if the current setup in not enough you can try saving session state in a memcached server. http://memcachedproviders.codeplex.com