Details explanation on Compression Enabled Session for SQL Server and State Sever Session Mode in ASP.NET 4.0

While we are talking about state management we consider Session is one of  most useful server side  state management mechanism for a web application.  In ASP.NET mainly we are having two type of state management  1. In Process  and 2. Out Process . In process is the by default session imagestorage mode for ASP.NET Web application and this is taken care by worker process in IIS. When it comes under Out Process we can use either of state server or SQL Server to persist session data. In case of In Process, session data stored in In memory of worker process. But when we are talking about OutProc session mode, we need to ensure that session data should be Serializedfirst . So, when we are moving session data from Web Server to Out Process Server ( State Server or SQL Server ) it can be a performance overhead based on the size of data that we are storing in Session.

ASP.NET 4.0 comes with a new option for compressing the Session data with Out Process Session mode. To enabling this functionality we need to add “compressionEnabled=”true” attribute with the SessionMode in web.config .


When Compression mode is enabled is web.config, ASP.NET  compress the serialized session data  and passed it to session storage and during retrieval same  deserialization and decompression happens in server side. ASP.NET 4.0 used System.IO.Compression.GZStream class to compress the session mode.


Now I am going to explain how you can use compression enabled session for SQL Server and State Server Session mode

Compression Enabled Session in SQL Server Session Mode :

Before start using SQL Server session mode you have to configure your SQL Sever Database to store Session Data. To configure SQL Server you have to run aspnet_regsql.exe command from Visual Studio Command Prompts.


Once you followed the all step, you will find  ASPState database created in your SQL Server.

Step by Step Guide on SQL Server Session Mode Configuration – Exploring Session in ASP.NET

Know bit details about the ASPState Database

SQL Server use ASPState database to store the session information for SQL Server Session mode. ASPState database having two tables ASPStateTempApplications and ASPStateTempSessions. ASPStateTempApplications  table contains Application ID and Application Name which is specific to each and every application which are using SQL Server session mode on the particular database. ASPStateTempSessions table having numbers of fields to store the session related information which includes [SessionId], [Created], [Expires], [Timeout]  etc. [SessionItemShort] and [SessionItemLong] actually contains the session data for every users. If the session data size is < = 7000 KB it will be stored in SessionItemShort field and anything > 7000 KB will be stored in SessionItemLong field

Let’s consider a simple student class which having Roll, Name and Bio field. We will store this Student object inside session for both the CompressionEnabled=”true” and CompressionEnabled=”False” cases.

By default CompressionEnabled is set to False

Here is the student class.

/// <summary>
/// Student Class
/// </summary>
internal sealed class Student
    public int Roll { get; set; }
    public string Name { get; set; }
    public string Bio { get; set; }

For showing the data compression I will be storing a huge amount of data in a single student object to see how much size its occupied in SQL Server and then will check the data size with compression enabled true option.

Below is the sample dummy code, where I am storing a chunk of data in to session and my session mode is set to SQL Server in Web.config.

 StringBuilder sb = new StringBuilder();
 for (int i = 0; i < 20000; i++)
sb.Append("Compression Enabled Session Demo");
Student student = new Student { Roll = 1, Name = "Abhijit Jana", Bio = sb.ToString() ; 
Session["Info"] = student;

Below is the web.config entry for SQL Server Session mode.


One you are done with the setup, you can simply run the code and check your SQL Server to see the session data..


Now, calculate the session data size as shown in below


So, we can see from here, the total session data size is 640459 . Now let’s have a look the session data size when compression enabled is true.

So, first set “CompressionEnabled=”true”” in web.config and again store the data in session to calculate the data size


Once we are done with configuration, first clear the session data from SQL Sever and run the application again to store session data. After that calculate the session data size again in SQL SErver


Well now session data size is “7158 KB” 

So, we can see there is an good amount of data size compression happened when we are using compression enabled session in ASP.NET 4.0.

Compression Enabled Session in State Server Session Mode :

State Server uses a stand-alone executable file which runs as a windows service and this is totally  Independent to IIS and can also run on a separate server.   Before working with state server session mode you have to make sure your aspnet_state.exe services is running. You can check it from Run > Services.msc


Once your services is running, you have to change the web.config session configuration for State Server session mode. Below is the config code snippet for State Server session mode .


Note : 42424 is the default port where aspnet_state.exe runs. Before putting the stateConnectionString you may check the port number used by aspnet_state using netstat command.

Once you are done with above setting you are good to go with storing the session data in State Server.  You can use Performance monitor tool to see number of active session in State Server.


Now, to monitor the session data size you can use of  VMMap Tool . VM Map toll will give you the details memory uses by the state server services. First check for the memory used using CompressionEnabled=”false” then test for “CompressionEnabled=”False”.  You will get the difference  amount of session data size for compression Enabled mode true and false.  below is the sample screen shots for VMMap tool which showing the memory uses by aspnet_state.exe


Make Sure, while testing for Session Data size in compression mode true and false, you are restarting the state server process so that all the session data can be set to blank.

One more important things to remember that use of compression mode is useful when we are storing large number of data in session because for every request it will going to use Gzip Compression/ Decompression going to be used to access the session variable.
You may look into ASP.NET Core Runtime PDC 2009 Video by Stefan for more information related with ASP.NET 4.0 State management improvements.



  1. Very Useful article and it hepled me a lot.I have read lot of articles regarding the state management and this is the best i have seen before.


  2. Thanks a lot And Really A very good article , explained session from the heart 🙂 , And Really appreciated sharing this much knowledge.. Again thanks.

    Learn and resolved many doubt regarding sql server session mode.


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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: