Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Azure, PHP, OpenXML, VSTS, ...

About the author

Maarten Balliauw is an MVP ASP.NET and is currently employed as .NET Software Engineer at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP.
More about me More about me
Send mail E-mail me


Microsoft Most Valuable Professional - MVP - ASP.NET

Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn RealDolmen - Rock-solid passion for ICT
I'm a speaker at TechDays Belgium and TechDays Finland

Search

Latest Twitter

    Follow me on Twitter...

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2010

    ASP.NET load balancing and ASP.NET state server (aspnet_state)

    At one of our clients, we used to have only one server for ASP.NET applications (including web services). Since this machine is actually business-critical and load is constantly growing, the need for a second machine is higher than ever.

    This morning I was asked to set up a simple demo of a load-balanced ASP.NET environment. I already did this in PHP a couple of times, but in ASP.NET, this question was totally new to me. Things should not be very different, I thought. And this thought proved right!

    A bit later, we had a load balancer in front of 2 web server machines. We got everything configured, fired up our webbrowser and saw a different page on each refresh (stating the server's hostname). Load balancing mission succeeded!

    Next thing: session state. In our PHP environment, we chose to centralize all session data in a database. ASP.NET provides the same functionality, but we chose to use the ASP.NET state server for this demo. This proved to be a difficult yourney... But we managed to get things running! Here's how.

    1. Set up the ASP.NET state service

    Pick a server which will serve as the session state server. Fire up the services control panel (services.msc). Select the "ASP.NET State Service" item and make it start automatically. Great! Our state service is running.

    Caveat 1: state server will not listen on any public IP address. So fire up your registry editor, change the following key and restart the ASP.NET state service:

    HKLM\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\AllowRemoteConnections

    Eventually change the port on which the state server will be listening:

    HKLM\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\Port (default: 42424)

    Caveat 2: after changing the AllowRemoteConnections directive, make sure the server's port 42424 is NOT open for the Internet, just for your web servers!

    2. Make both ASP.NET servers use the state server

    Every Web.config file contains a nice configuration directive named "sessionState". So open up your Web.config, and make it look like this:

    <?xml version="1.0"?>
    <configuration>
        <system.web>
            <!-- ... -->
            <sessionState
                mode="StateServer"
                stateConnectionString="tcpip=your_server_ip:42424"
                cookieless="false"
                timeout="20" />
            <!-- ... -->
        </system.web>
    </configuration>

    3. So you think you are finished...

    ...but that's not the case! Our load balancer did a great job, but both servers where returning different session data. We decided to take a look at the session ID in our cookie: it was the same for both machines. Strange!

    Some research proved that it was ASP.NET's <machineKey> configuration which was the issue. Both web servers should have the same <machineKey> configuration. Let's edit Web.config one more time:

    <?xml version="1.0"?>
    <configuration>
        <system.web>
            <machineKey
              validationKey="1234567890123456789012345678901234567890AAAAAAAAAA"
              decryptionKey="123456789012345678901234567890123456789012345678"
              validation="SHA1"
              decryption="Auto"
            />
            <!-- ... -->
            <sessionState
                mode="StateServer"
                stateConnectionString="tcpip=your_server_ip:42424"
                cookieless="false"
                timeout="20" />
            <!-- ... -->
        </system.web>
    </configuration>

    (more on the machineKey element on MSDN)

    Also check MS KB 325056, this was an issue we did not meet, but it might save your day.

    4. Great success!

    Our solution now works! Only problem left is that we have a new single point of failure (SPOF): the ASP.NET state service. But we might just set up 2 of those and fail over both session service machines.

    UPDATE 2008-01-23: Also check out my blog post on Session State Partitioning!

    kick it on DotNetKicks.com


    Categories: ASP.NET | C# | General | ICT | Internet | Webfarm | XML

    Comments

    hanselman.com | Reply

    Thursday, January 31, 2008 8:29 PM

    pingback

    Pingback from hanselman.com

    Scott Hanselman's Computer Zen - Troubleshooting Expired ASP.NET Session State and Your Options

    weblogs.asp.net | Reply

    Friday, February 01, 2008 1:55 AM

    pingback

    Pingback from weblogs.asp.net

    Maarten Balliauw on ASP.NET load balancing and the ASP.NET state server. - Joe On ASP.NET

    blogs.msdn.com | Reply

    Friday, February 01, 2008 1:55 AM

    pingback

    Pingback from blogs.msdn.com

    Joe Stagner - Frustrated by Design ! : Maarten Balliauw on ASP.NET load balancing and the ASP.NET state server.

    geeks.ms | Reply

    Friday, February 01, 2008 2:48 AM

    pingback

    Pingback from geeks.ms

    Maarten Balliauw on ASP.NET load balancing and the ASP.NET state server. - Noticias externas

    radicaldevelopment.net | Reply

    Sunday, February 03, 2008 9:24 AM

    pingback

    Pingback from radicaldevelopment.net

    January 2008 Resources RoundUp - Radical Development

    blogs.msdn.com | Reply

    Tuesday, July 08, 2008 9:01 AM

    pingback

    Pingback from blogs.msdn.com

    JoeOn.net In Japanese : Windows Workflow Foundation チュートリアル シリーズ

    Dale Sackrider, II United States | Reply

    Monday, July 14, 2008 11:33 PM

    Dale Sackrider, II

    Great information, however even after reading the other posts in the series, I still don't see that you have addressed the question of failover:

    Only problem left is that we have a new single point of failure (SPOF): the ASP.NET state service. But we might just set up 2 of those and fail over both session service machines.

    The session service isn't cluster aware and even in your load balanced senerio, if a session service box goes down, you lose those sessions, right?  

    I have a hard time understanding whay anyone would use a single session service box in a web farm.  There seems to be only two reasons for a web farm: capacity and availablity.

    If capacity is a concern, the session service actually added load to the box since it now has to make an external out of process call for sessions information.  If the concern is availablity, having several boxes in the farm would help - until you move all session information to this non-clusterable spof server.

    Am I missing something or is there a way to handle failover for session state using a state server?

    maartenba Belgium | Reply

    Tuesday, July 15, 2008 12:31 PM

    maartenba

    It is indeed true that you lose those sessions. Another option there is to use SQL Server as a session store. This is slihtly slower, but increases session reliability. Another option is to use StateMirror (www.statemirror.com), a commercial product which acts the same as ASP.NET State server but also shares data between multiple state servers.

    RealDolmen blogs | Reply

    Wednesday, December 31, 2008 3:43 PM

    trackback

    Trackback from RealDolmen blogs

    Top blog posts in 2008

    Jef Claes Belgium | Reply

    Tuesday, April 07, 2009 11:48 AM

    Jef Claes

    Good post.

    Add comment




      Country flag

    biuquote
    • Comment
    • Preview
    Loading