SESSION in asp.net

What is Session ?

Web is Stateless, which means a new instance of the web page class is re-created each time the page is posted to the server. As we all know HTTP is a stateless protocol, it can't hold the client information on page. If user inserts some information, and move to the next page, that data will be lost and user would not able to retrieve the information. So what we need? we need to store information. Session provides that facility to store information on server memory. It can support any type of object to store along with our custom object. For every client Session data store separately, means session data is stored as per client basis. Have a look at the following diagram. 
explor2.jpg
Fig : For every client session data store separately
State Management using session is one of the asp.net best features, because it is secure, transparent from users and we can store any kind of object with in it. Along with advantages, some times session can causes performance issue for heavy traffic sites because its stored on server memory and clients read data from the server itself. Now lets have a look at the advantages and disadvantages of using session in our web application.   

Advantages and Disadvantages of Session ?

Following are the basic advantages and disadvantages of using session. I have describe in details with each type of session at later point of time. 
Advantages :  
  • It helps to maintain user states and data to all over the application.
  • It can easily be implemented and we can store any kind of object. 
  • Stores every client data separately. 
  • Session is secure and transparent from user.
Disadvantages :  
  • Performance overhead in case of large volume of user, because of session data stored in server memory.
  • Overhead involved in serializing and De-Serializing session Data. because In case of StateServer and SQLServer session mode we need to serialize the object before store. 
Besides these, there are many advantages and disadvantages of session that are based of session Types. I have Discussed all of them.

InPorc Session Mode :  

This is the default session mode in Asp.Net. Its stores session Information in Current Application Domain. This is the best session mode which is based on web application Performance. But main disadvantage is that, It will lose the data if we restart the server. There are some more advantages and disadvantages of InProc session mode. I will come to those points again .

Overview of InProc Session Mode :

As I have already discussed  InProc mode session data will be stored on the current application domain. So It is easily and quickly available. 

So, InProc session mode store its session data in a memory object on that application domain. This is handled by worker process in application pool. So If we restart the server we will lose the session data. If Client request for the data , state provide read the data from In-Memory Object and return it to the client. In web.config we have to mention Session mode  and also we have to set the Timeout.   
explor3.gif
This Session TimeOut Setting keeps session alive for 30 minute. This can be configurable from Code behind too. 
Collapse
Session.TimeOut=30; 
 There are two type of session events available in asp.net Session_Start() and Session_End. It is the only mode that supports the Session_End() event. These events will call after the session timeout period is over. The general flow for the InProc Session State is some thing like this. 
Now, when the Session_End() will call that depends on Session Time Out. This is a very fast mechanism because no serialization occurs for storing and retrieving data, and data are staying inside the same application domain. 

When Should we use InProc Session Mode ? 

InProc is the default session mode. It can be very helpful for a small web sites and where the number of user are very less, We should avoid InProc in case of Web Garden (I will come to this topic in details) Scenario .

Advantages and Disadvantages

Advantages :
·         It store Session data in memory object of current application domain. So  accessing data is very fast and data is easily available.
·         There is not requirements of serialization to store data in InProc Session Mode.
·         Implementation is very easy, just similar to using View State.
Disadvantages :  
although InProc Session is fastest, common and default mechanism, It has lots of limitation.
·         If the worker Process or application domain recycles all session data will be lost.
·         Though its fastest, but more session data and more users can affects performance, because of memory.
·         we can't use it in web Garden scenarios .
·         This session mode is not suitable for web farm scenarios also.
So as per above discussion, we can conclude InProc is very fast session storing mechanism but suitable for small web application. InProc Session Data will get lost if we Restart the server, application domain recycles It is also not suitable for Web Farm and Web Garden Scenarios.
Now have a look that what are the other option  available to overcome these problem. First Come to StateServer Mode. 

Out-Proc Or StateServer Session Mode :  

er uses a stand-alone Windows Services, which is Independent to IIS and can also run on a separate server. This session state is totally managed by aspnet_state.exe. This server may runs on the same system, but it's out side of that main  application domain where your web application is running. This allow if you restart your asp.net process restarted your session data will be alive. This approaches has several disadvantages due to the overhead of serialization and de-serialization, its also increases the cost of data access because of every time when user retrieves session data, our application hits a different process.

Advantages and Disadvantages.

So based on the above discussion
Advantages : 
·         Its keeps the data separate from IIS so, any Issue with IIS does not hamper Session data.
·         It is useful in web farm and web garden scenarios.
Disadvantages :
·         Process is slow due to Serialization and De-Serialization
·         State Server always need to be up and running.

SQL Server Session Mode

This session mode provide us more secure and reliable Session management in asp.net. In this session  mode, the Session data is serialized and stored in the SQL Server database.  Main disadvantages of this session storage methods is overhead related with Data Serialization and De-Serialization. It is the best option for using in the web farms

Advantages and Disadvantages 

Advantages :
  • Session data do not  affected if we restart the IIS.
  • It is the most reliable and secure session management.
  • It keeps data located centrally ,  It can be easily accessible from other application.
  • It is  very useful in web farm and web garden scenarios.
Disadvantages : 
  • Processing is very slow in nature.
  • Object serialization and de-serialization creates overhead  for application.
  • As the session data is handled in different server, so we have to take care of SQL server. It should be always up and running.

0 comments: