Session State Compression

Developing a web application that responds to users requests quickly is a target and a challenge for all web developers. Using sessions in ASP.Net sometimes may cause performance problems. If the data that is to be stored in the session is large, it would consume more server resources. The new future in ASP.net 4 will definitely be our pain killer on this subject. It is very simple configuration. ASP.NET 4 now comes with in-built support for compressing Session data for storing out-of-process sessions. To enable this feature, just set the compressionEnabled attribute to "true" as shown in the code snippet below:

 

<sessionState

  mode="SQLServer"

  stateConnectionString="some connection string..."

  compressionEnabled="true"/>

 

ASP.NET will compress (and decompress) serialized session state by using the .NET Framework System.IO.Compression.GZipStream class. You can simply give it a go and see the difference via configuring SQL DB as a sessionstate server and checking the size of SessionItemLong field in ASPStatesTempSessions table. On text values we can see incredible difference, but if it is an image file the difference will not be that incredible.