Logo

Maarten Balliauw {blog}

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

About the author

Maarten Balliauw is currently employed as .NET Technical Consultant at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
View Maarten Balliauw's MVP profile

Search

Latest Twitter

    Follow me on Twitter...

    My projects

    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 Session State Partitioning

    After my previous blog post on ASP.NET Session State, someone asked me if I knew anything about ASP.NET Session State Partitioning. Since this is a little known feature of ASP.NET, here's a little background and a short how-to.

    When scaling out an ASP.NET application's session state to a dedicated session server (SQL server or the ASP.NET state server), you might encounter a new problem: what if this dedicated session server can't cope with a large amount of sessions? One option might be to create a SQL server cluster for storing session state. A cheaper way is to implement a custom partitioning algorithm which redirects session X to state server A and session Y to state server B. In short, partitioning provides a means to divide session information on multiple session state servers, which all handle "their" part of the total amount of sessions.

    Download example 

    Want an instant example? Download it here.
    Want to know what's behind all this? Please, continue reading.

    1. Set up ASP.NET session mode

    Follow all steps in my previous blog post to set up the ASP.NET state service / SQL state server database and the necessary web.config setup. We'll customise this afterwards.

    2.   Create your own session state partitioning class

    The "magic" of this el-cheapo solution to multiple session servers will be your own session state partitioning class. Here's an example:

    using System;

    public class PartitionResolver : System.Web.IPartitionResolver
    {

        #region Private members

        private String[] partitions;

        #endregion

        #region IPartitionResolver Members

        public void Initialize()
        {
            // Create an array containing
            // all partition connection strings
            //
            // Note that this could also be an array
            // of SQL server connection strings!
            partitions = new String[] {      
                "tcpip=10.0.0.1:42424",   
                "tcpip=10.0.0.2:42424",       
                "tcpip=10.0.0.3:42424"
            };
        }

        public string ResolvePartition(object key)
        {
            // Accept incoming session identifier
            // which looks similar like "2ywbtzez3eqxut45ukyzq3qp"
            string sessionId = key as string;

            // Create your own manner to divide session id's
            // across available partitions or simply use this one!
            int partitionID = Math.Abs(sessionId.GetHashCode()) % partitions.Length;
            return partitions[partitionID];
        }

        #endregion
    }

    Basically, you just have to implement the interface System.Web.IPartitionResolver, which is the contract ASP.NET uses to determine the session state server's connection string. The ResolvePartition method is called with the current session id in it, and allows you to return the connection string that should be used for that specific session id.

    3. Update your web.config

    Most probably, you'll have a web.config which looks like this:


    <configuration>

      <system.web>

        <!-- ... -->

        <sessionState

            mode="StateServer"

            stateConnectionString="tcpip=your_server_ip:42424" />

        <!-- ... -->

      </system.web>

    </configuration>

    In order for ASP.NET to use our custom class, modify web.config into:


    <configuration>

      <system.web>

        <!-- ... -->

        <sessionState

            mode="StateServer"

            partitionResolverType="PartitionResolver" />

        <!-- ... -->

      </system.web>

    </configuration>

    You may have noticed that the stateConnectionString attribute was replaced by a partitionResolverType attribute. From now on, ASP.NET will use the class specified in the partitionResolverType attribute for distributing sessions across state servers.

    UPDATE 2008-01-24: Also check out my blog post on Session State Partitioning using load balancing!

    kick it on DotNetKicks.com


    Categories: ASP.NET | C# | General | Software | Webfarm | XML

    Comments

    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 チュートリアル シリーズ

    Chris | Reply

    Monday, September 22, 2008 4:02 AM

    Chris

    What if 1 machine goes down?

    maartenba | Reply

    Monday, September 22, 2008 8:32 AM

    maartenba

    You would have to manage that in creating a session state partitioning strategy. Also check blog.maartenballiauw.be/.../...Load-Balancing.aspx

    Echte Rotterdamer United Kingdom | Reply

    Friday, October 31, 2008 6:37 PM

    Echte Rotterdamer

    If one of the state servers goes down you will get an exception in PartitionResolver for the sessions stored in that server. Read the following post to learn how to fix it:

    forums.asp.net/p/1341297/2717479.aspx#2717479

    RealDolmen blogs | Reply

    Wednesday, December 31, 2008 3:43 PM

    trackback

    Trackback from RealDolmen blogs

    Top blog posts in 2008

    code-inside.de | Reply

    Sunday, March 21, 2010 10:14 PM

    pingback

    Pingback from code-inside.de

    HowTo: Session in ASP.NET & ASP.NET Session State Server | Code-Inside Blog

    Add comment




      Country flag

    biuquote
    • Comment
    • Preview
    Loading