Making the ASP.NET State Service work across network

Once you start distributing your ASP.NET website across multiple webservers, you're going to need a way to share session state. That is, unless your app is stateless, in which case scaling it should be a breeze!

One of the easiest ways to provide common session state for a small cluster (very dependant on load and hardware specs, but ~10 servers max, per state server), is to use the built-in ASP.NET State Service. It's a free service that's installed alongside the .NET Framework on all Windows servers.

While the InProc session storage is stored directly in the w3wp process, the ASP.NET State Service is an independant process that runs alongside your IIS w3wp processes. The State Service does not have to run on a machine with IIS installed - it can run on a machine dedicated to serving session state for other web servers running IIS.

Performance


Switching from InProc to State Service will have an impact on performance. We now have to cross not only process boundaries, but also machine boundaries. Furthermore, once we go out-of-process, all objects will have to be serialized, requiring extra work and requires all objects to be marked with the attribute.

The State Service performance is heavily reliant on memory. Once physical memory has been exhausted it'll start paging to disk which will kill performance quickly. Make sure to monitor the memory load on your State Service machine(s) and adjust memory accordingly.

Enabling remote connectivity


By default, the State Service will only allow local-to-machine connections. To allow remote connections you'll have to set the HKLM\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\AllowRemoteConnection key to a value of '1'. After changing the AllowRemoveConnection key value, you'll have to restart the State Service service for the change to take effect. Also make sure your firewall allows connectivity to the State Service port (TCP 42424 by default).

Requirements



Scalability


If you start saturating your dedicated State Service machine, it's possible to implement session state partitioning by implementing the System.Web.IPartitionResolver interface. By creating your own implementating, you can route new requests to different state servers and perhaps even check whether the state servers are available or not, to add redundancy. Note however that this will not give you redundancy in case the State Service crashes either due to software or hardware.

kick it on DotNetKicks.com


Comments

Luke Puplett | Aug 26th, 2009, 12:21 PM

Readers may also be interested in Microsoft's fault-tolerant distributed caching project called "Velocity".

http://channel9.msdn.com/shows/ARCast.TV/ARCastTV-Scott-Hanselman-on-scaling-websites-with-caching/

yachika | Jul 13th, 2010, 12:42 PM

Think of yourself as on the threshold of unparalleled success. A whole clear, glorious life lies before you. Achieve! Achieve!

online blackjack | Aug 6th, 2010, 1:53 PM

Thank you for the work you have put into this post, it helps clear up some questions I had.I will bookmark your blog because your posts are very informative.We appreciate your posts and look forward to coming back...

Add comment

After you have posted a comment, an email will be sent to the provided email address. Before your comment is activated, you will have to click the confirmation link within the email.

Name:

Email (only used for validation):

Website (optional):

Message:

Notify me when new comments are added:

Please type the following letters into the box below:  

Post!