Friday, January 16, 2009

ASP.Net Session State Management

Here i will try to detail the important ASP.Net session state management, my topic will targer Windows server 2003 with IIS6.

Web server?
IIS (Internet Information Service) running on a software port like 80 for Http and 443 for Https or any other port set bt the administrator, it can host more than one web site and each web site can hold more than one web application. Web server provides application pooling and hanldes all web request from the internet and forward it to respective web site's application pool for processing and response. IIS can host classic asp applications, asp.net applications, php applications, etc.

Application Pooling?
The Windows IIS web server, has application pools where different web applications or web sites can be pooled. When we say pooled it means it is loaded in the process, so the web requests are processed. Why pooling? becuase the application will handle multiple web requests, the application is loaded once, and remains in the pool for quite some time to process subsequent requests and when remains idel for too long it is unloded. The pooling helps better manage server resources, so the CPU and Memory is not consumed uselessly. An application pool can hold more than one web application, and IIS can have more than one application pools. Application pools it self is comprehensive topic and it provides many things to tune your web application performace.

Worker Process?
As we discussed above, application pooling is done in IIS, Worker Process is actually a program that executes in the system memory for a specific application pool, if we have three application pools and the pools are active than we will have three process running it the system memory, these are the processes which handle the requests and send response. The "w3wp.exe" program can be easily found in the task manager, this is the program running behind the application pool.
We can define more than one worker process in an application pool, by default we have one worker process in an application pool, this means there is only one queue for my web requests, so ten request will execute one at a time.
If i increase the number to 2, so now my application pool has 2 worker process, this means there are two queues for my web requests, so ten request will execute two at a time. Ofcourse the CPU time will be divied between the two worker processes, this means to many worker processes will cut down the overall response time if i have a moderate server, therefore it depends upon the hardware of the server that how many worker processes it can hold.

Each worker process has its own memory as well, so be carefull while configuring this number.

Now as we have a good background of the web server, application pooling and worker process, we can move forward to our actually topic, that is the Session Sate Management.

What is Session?
HTTP is a stateless protocol. Each request is serviced as it comes; after the request is processed, all of the data is discarded. No state is maintained across requests even from the same client.
However, it is very useful to maintain state across requests for certain solutions. ASP.NET enables you to maintain both application state and session state through use of application and session variables respectively.
Session is a way of persisting or storing client data on web server between subsequent request from the client. Here client is the browser or internet browsing application which is making requests to our web server.
Web server can host different web sites, each web site can maintain its own sessions, regardless of the web server. Sessions are maintained for sites, like we would want to track the users shopping cart temporarily until the user checks out, this can be done in the session of the client.
Sessions are stored in the system memory of the web server, and the memory is allocated by the "w3wp.exe" worker process program. Information stored in client session dont lose unless the session is destroyed on the server or the user signs out. We have timeout set for sessions, default is 20 minutes, if a session remains idle for more than 20 minutes it is automatically destroyed, this is becuase there is no user interaction, so the session remained inactive. As sessions are created it consumes system memory, and when sessions are destroyed the system memory is released. Session variables are automatically discarded after they are not used for the time-out setting that is specified in the Web.config file. On each request, the time out is reset. The variables are lost when the session is explicitly abandoned in the code.

Session ID:
When a session is initiated on first request, the server issues a unique session ID to the user. To persist the session ID, store it in an in-memory cookie (which is the default), or embed it within the request URL after the application name. To switch between cookie and cookieless session state, set the value of the cookieless parameter in the Web.config file to true or false.

1. Cookieless
2. Cookie

In cookieless mode, the server automatically inserts the session ID in the relative URLs only. An absolute URL is not modified, even if it points to the same ASP.NET application, which can cause the loss of session variables.

Session State:
Now what is session state, and why i am highlighting it? because knowning what is session is not enough, Asp.Net provides different ways to manager session state according to the requirements of the web applications. We can store session in following, this is configured through web configuration file of a web application:

InProc or Worker Process: In-Proc mode stores values in the memory of the ASP.NET worker process. Thus, this mode offers the fastest access to these values. However, when the ASP.NET worker process recycles, the state data is lost.

StateServer: Alternately, StateServer mode uses a stand-alone Microsoft Windows service to store session variables. Because this service is independent of Microsoft Internet Information Server (IIS), it can run on a separate server. You can use this mode for a load-balancing solution because multiple Web servers can share session variables. Although session variables are not lost if you restart IIS, performance is impacted when you cross process boundaries.

SqlServer: If you are greatly concerned about the persistence of session information, you can use SqlServer mode to leverage Microsoft SQL Server to ensure the highest level of reliability. SqlServer mode is similar to out-of-process mode, except that the session data is maintained in a SQL Server. SqlServer mode also enables you to utilize a state store that is located out of the IIS process and that can be located on the local computer or a remote server.

Configuring Session State:
You can configure session state in the configuration section of the Web.config file. The configuration section appears similar to the following:

<configuration>
  <system.web>
   <sessionstate mode="InProc" timeout="20" cookieless="false"
     stateconnectionstring="tcpip=127.0.0.1:42424"
     sqlconnectionstring="data source=127.0.0.1;user id=username;
    password=password">
  </system.web>
</configuration>


Reference: Asp.Net State Management

No comments:

Post a Comment