While we are talking about state management, we consider Session as one of the most useful server side state management mechanisms for a web application. In ASP.NET, mainly we are having two types of state management:
- In Process and
- Out Process
“In process” is the by default session storage mode for ASP.NET Web application and this is taken care of by a 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 “Serialized” first . 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 in web.config, ASP.NET compresses the serialized session data and passes it to session storage and during retrieval, same deserialization and decompression happens on the 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 you 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 all the steps, 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 has two tables ASPStateTempApplications and ASPStateTempSessions. ASPStateTempApplications table contains Application ID and Application Name which is specific to each and every application which is using SQL Server session mode on the particular database. ASPStateTempSessions table has a number 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 user. 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 is 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:
[Serializable]
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 it 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 into 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 below:
So, we can see from here, the total session data size is 640459. Now let’s have a look at 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 Server 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 that there is a good amount of data size compression that 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 the number of active sessions in State Server.
Now, to monitor the session data size, you can use 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 are the sample screen shots for VMMap tool which show 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 thing to remember is that use of compression mode is useful when we are storing large number of data in session because for every request it is going to use Gzip Compression/ Decompression to access the session variable.
You may look into ASP.NET Core Runtime PDC 2009 Video by Stefan for more information related to ASP.NET 4.0 State management improvements.
Thanks!
AJ
Filed under:
ASP.NET 4.0,
General