Blog Post

IIS Support Blog
2 MIN READ

Session State Is Lost in Web Farm If You Use SqlServer or StateServer Session Mode

Jawahar Ganesh S's avatar
Feb 15, 2019

One of the most common issues that users face when hosting their application in a webfarm is “unexpected loss of session state”. We face these issues when we store the session information inproc, especially when we don’t set client affinity in the load balancer level.

This is one of the main reasons why users opt for an outproc session storage technique (like a SQL server or State Server). But, when you are configuring a state server across a web farm, maintaining the session state across multiple servers and applications is a matter of challenge for which you must take care of a little configuration piece while setting your sites.

Let me give you a brief idea on what we can do and why we are supposed to concentrate on such configuration settings while setting up our sites in a webfarm environment.

  • When we make a request, a worker process will be spawned which is always associated with a site ID.
  • The user is assigned with a unique session ID when the session begins. The user will then send that session ID to the server for every request.
  • When the session information is stored in the SQL Server or state server, to uniquely identify the session information for each user specific to each application, it is associated with a primary key.
  • That primary key is a combination of the session ID assigned to the user (that you might see in the request header) and the application ID.

For example, the value looks like: ’13clw2vlrjio0d45opi0qg4500000002’ where 2 is site id

You should ensure that your website is associated with the same site ID in all the web servers which are behind a load balancer to avoid unexpected session state losses.When we are configuring the application in multiple servers and when the site ID’s are different in different servers, this primary key will become different for different servers. That will produce a behavior similar to storing the session inproc.

Conclusion:
Maintaining same site ID for the same application across all the servers in the webfarm is important and will help in maintaining the session state when going with out-of-proc approach in a multi-server environment

Please leave your comments if you have any questions around this or need any clarification.

Author: Nikita Habib

Updated Sep 06, 2021
Version 2.0

2 Comments

  • Marcel8964's avatar
    Marcel8964
    Copper Contributor

    Both "Site ID" of servers in server farm, "Application Path" of the application, and "Validation Key", "Decryption Key" of the application should be the same across different servers in server farm.

     

    Site ID is the number assiged by IIS to a site, viewable from IIS -> [Host Name] -> Sites -> the "ID" column on the main pane ("Sites"). e.g. "Default Web Site" always be assigned with ID 1.  The site id is editable in the "Advance Setting" of the site.

     

    Application Path is the path name under the site, viewable from IIS -> [Host Name] -> Sites -> [Name of Site, e.g. "Default Web Site"] -> [Application Path, e.g. the name of your application.]

     

    "Validation Key", "Decription Key" viewable from IIS -> [Host Name] -> Sites -> [Name of Site, e.g. "Default Web Site"] -> [Application Path, e.g. the name of your application.] -> Machine Key icon on the main pane.  Need to generate in one of the server, and paste to other servers.

  • chadwixk's avatar
    chadwixk
    Copper Contributor

    Is this article conflicting because it states that it uses the website's file path as the key, not the site ID, or am I missing something:

    https://docs.microsoft.com/en-us/previous-versions/aspnet/ms178586(v=vs.100)

     

    "The path for the ASP.NET application in the IIS metabase must be identical on all Web servers that share session state in the SQL Server database. For information on steps to resolve the issue when application paths differ between servers, see article 325056, "PRB: Session State Is Lost in Web Farm If You Use SqlServer or StateServer Session Mode," in the Microsoft Knowledge Base at http://support.microsoft.com/."