ASP.NET Session State Partitioning

Edit on GitHub

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: SessionPartitioning.zip (2.70 kb)
 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:

[code:c#]

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
}

[/code]

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:

[code:xml]

<configuration>
  <system.web>
    <!-- ... -->
    <sessionState
        mode="StateServer"
        stateConnectionString="tcpip=your_server_ip:42424" />
    <!-- ... -->
  </system.web>
</configuration>

[/code]

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

[code:xml]

<configuration>
  <system.web>
    <!-- ... -->
    <sessionState
        mode="StateServer"
        partitionResolverType="PartitionResolver" />
    <!-- ... -->
  </system.web>
</configuration>

[/code]

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

This is an imported post. It was imported from my old blog using an automated tool and may contain formatting errors and/or broken images.

Leave a Comment

avatar

5 responses

  1. Avatar for Chris
    Chris September 22nd, 2008

    What if 1 machine goes down?

  2. Avatar for maartenba
    maartenba September 22nd, 2008

    You would have to manage that in creating a session state partitioning strategy. Also check http://blog.maartenballiauw...

  3. Avatar for submariner_highlander
    submariner_highlander October 31st, 2008

    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:

    http://forums.asp.net/p/134...

  4. Avatar for fabio parente
    fabio parente July 2nd, 2013

    Great Article! Very interesting!

    But I'd like to implement the session state partitioning on every local machine of the web farm itself. For example, every single machine of the web farm must be its single state machine itself locally. Is it possible?

    If true, I think i will not need a partitioning class. Right?

    Probably i'll need only serializeble data, asp.net state service active (on every single machine???) and change the web config this way:

    <sessionstate mode="StateServer" stateconnectionstring="tcpip=your_server_ip:42424" cookieless="false" timeout="20"/>

    Right?

    (what about cookieless true or false?????)

  5. Avatar for Abdul Hafeez
    Abdul Hafeez October 6th, 2015

    What is the answer of "fabio parente" post