Session State in web farms

Recently I was working on a application that was required to be hosted on multiple web servers. Sounds simple and many of us has already done so may be more than once. One of the key issues that arise is maintaining session states across multiple web servers so that the user request can be routed to any web server. Asp .Net provides very elegant solutions out of the box. We can use Sql Server or an additional State Server. The mechanism used is called OutOfProcess Session Management. Whenever out-of-process session state is specified, it is also important to realize that anything placed into session state is serialized and passed out of the ASP.NET worker process. Thus, any type that is stored in session state must be serializable for this to work properly.
public class YourClassName
// class implementation

StateServer mode stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.

SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.

The .Net Framework Installs a service called ASP .Net State Service which handles the sessions when Out of Process.

aspnet state service

In order to set up SessionState server modify the web.config

<sessionState mode=”StateServer”stateConnectionString=”″ />

42424 : is the default port. It can be updated by setting the registry entry : HKLM\System\CurrentControlSet\Services\aspnet_state\Parameters\Port

To configure SQLServer Mode, you must run InstallSQLState.sql on the database. The primary purpose of this script is to create a table that can store client-specific state indexed by session ID in the tempdb of that SQL Server installation. The ASP state table is created in the tempdb database, which is not a fully logged database, thus increasing the speed of access to the data. In addition to storing the state indexed by the session ID, this table keeps track of expiration times and provides a locking mechanism for exclusive acquisition of session state. The installation script also adds a job to clean out all expired session state daily.

<sessionState mode=”SQLServer” sqlConnectionString=  “data source=;user id=sa;password=” />
Note: no database is specified in the connection string because it is assumed tobe TempDB.

Both the state server and the SQL Server session state options store the state as a byte stream—in internal data structures in memory for the state server, and in a VARBINARY field (or an IMAGE field if larger than 7KB) for SQL Server.

One Response to “Session State in web farms”

  1. Matt Says:

    With tabbed browsing seems multiple browsers (in same base browser window) will ultimately overwrite good/non-expired sessions of another tab. Is there a way to prevent this?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: