[Post] ASP. NET Session State Partitioning using State Server Load Balancing

Source: Internet
Author: User
Document directory
  • 3. Using the correct state server for a specific session id
From: http://blog.maartenballiauw.be/post/2008/01/ASPNET-Session-State-Partitioning-using-State-Server-Load-Balancing.aspx

It seems like amount of posts on ASP. NET's Session State keeps growing. Here's the list:

  • ASP. NET Session State Partitioning
  • ASP. NET load balancing and ASP. NET state server (aspnet_state)

Yesterday's post on Session State Partitioning used a round-robin method for partitioning session state over different state server machines. The solution I presented actually works, but can still lead to performance bottlenecks.

Let's say you have a web farm running multiple applications, all using the same pool of state server machines. when having multiple sessions in each application, the situation where one state server handles much more sessions than another state server cocould occur. for that reason, ASP. NET supports real load balancing of all session state servers.

Download example

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

What we want to achieve...

Here's a scenario: We have different applications running on a web farm. these applications all share the same pool of session state servers. whenever a session is started, we want to store it on the least-busy state server.

1. Performance counters

To fetch information on the current amount of sessions a state server is storing, we'll use the performance counters ASP. NET state server provides. Here's a code snippet:

If (PerformanceCounterCategory. CounterExists ("State Server Sessions Active", "ASP. NET", "STATESERVER1 ")){
PerformanceCounter pc = new PerformanceCounter ("ASP. NET", "State Server Sessions Active", "", "STATESERVER1 ");
Float currentLoad = pc. NextValue ();
} 2. Creating a custom session id

Somehow, ASP. NET will have to know on which server a specific session is stored. to do this, let's say we make the first character of the session id the state server id from the followingIList:

IList <StateServer> stateServers = new List <StateServer> ();

// Id 0, example session id wocould be 0ywbtzez3eqxut45ukyzq3qp
StateServers. Add (new StateServer ("tcpip = 10.0.0.1: 42424", "sessionserver1 "));

// Id 1, example session id wocould be 1ywbtzez3eqxut45ukyzq3qp
StateServers. Add (new StateServer ("tcpip = 10.0.0.2: 42424", "sessionserver2 "));

Next thing we'll have to do is storing these list id's in the session id. For that, we will implement a customSystem. Web. SessionState. SessionIDManagerClass. This class simply creates a regular session id, locates the least-busy state server instance and assign the session to that machine:

Using System;
Using System. Diagnostics;

Public class SessionIdManager: System. Web. SessionState. SessionIDManager
{
Public override string CreateSessionID (System. Web. HttpContext context)
{
// Generate a "regular" session id
String sessionId = base. CreateSessionID (context );

// Find the least busy state server
StateServer leastBusyServer = null;
Float leastBusyValue = 0;
Foreach (StateServer stateServer in StateServers. List)
{
// Fetch first state server
If (leastBusyServer = null) leastBusyServer = stateServer;

// Fetch server's performance counter
If (PerformanceCounterCategory. CounterExists ("State Server Sessions Active", "ASP. NET", stateServer. ServerName ))
{
PerformanceCounter pc = new PerformanceCounter ("ASP. NET", "State Server Sessions Active", "", stateServer. ServerName );
If (pc. NextValue () <leastBusyValue)
{
LeastBusyServer = stateServer;
LeastBusyValue = pc. NextValue ();
}
}
}

// Modify session id to contain the server's id
// We will change the first character in the string to be the server's id in
// State server list. Notice that this is only for demonstration purposes! (Not secure !)
SessionId = StateServers. List. IndexOf (leastBusyServer). ToString () + sessionId. Substring (1 );

// Return
Return sessionId;
}
}

The class we created will have to be registered inWeb. config. Here's how:

<Configuration>
<System. web>
<! --... -->
<SessionState mode = "StateServer"
PartitionResolverType = "PartitionResolver"
SessionIDManagerType = "SessionIdManager"/>
<! --... -->
</System. web>
</Configuration>

You notice our customSessionIdManagerClass is now registered to beSessionIDManager.PartitionResolverI blogged about is also present in a modified version.

3. Using the correct state server for a specific session id

In the previous code listing, we assigned a session to a specific server. Now for ASP. NET to read session state from the correct server, we still have to usePartitionResolverClass:

Using System;

Public class PartitionResolver: System. Web. IPartitionResolver
{

# Region IPartitionResolver Members

Public void Initialize ()
{
// No need for this!
}

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

// Since we defined the first character in sessionId to contain
// State server's list id, strip it off!
Int stateServerId = int. Parse (sessionId. Substring (0, 1 ));

// Return the server's connection string
Return StateServers. List [stateServerId]. ConnectionString;
}

# Endregion
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.