Owing to the stateless character of the HTTP protocol, web applications had to always bear the burden of state management. Thankfully, ASP.NET comes loaded with multiple features that make maintenance of user state easier. Session state is one such powerful feature enabling easy user state management. This particular feature offers a suitable programmatic interface for correlating an application state with a specific user session while taking care of the client session management and state storage at the back-end for the particular application.
If you need to deploy session state in ASP.NET application development, you can consider choosing from the three modes offering a varied range of scalability and performance. Here is a look:
InProc: Referred to as the in-process store, this particular mode offers a fast session state access without the involvement of any sort of marshalling or serialization costs. This is possible as the state maintenance is mostly done within the controlled memory of the ASP.NET. Usually, the state data tends to get lost as the process recycles. However, you can choose to disable the recycling, if it influences the functioning of the application. The in-process store restricts the scalability of the application as it cannot be used in association with several worker processes.
StateServer: This particular service of session state can be easily installed on a remote server that all web servers can access universally or on a local server. This particular approach offers good scalability, but tends to influence the performance of the application as against the in-process mode owing to the involvement of the extra marshalling and serialization required for transferring the state continuously from state store.
SQL Server: The SQL Server offers an easily accessible and scalable solution that is perfectly suitable for large session state. The marshalling and the serialization costs are equal so far as the session state service is concerned. This particular approach offers failover clustering although it does not enjoy a support in the session state default configuration.
Session State – Exploring the Work Approach
The ASP.NET session state allows you to correlate a specific client session of HTTP with an object dictionary featuring state data or a server-side string. A session is usually referred to as a request series introduced by a similar client within a specific time-period and controlled by connecting a session ID with specific clients.
This particular ID is supposed to be produced by the client on request either as a special section of the URL or in a cookie. The server side stores the session data in one of the supported stores of session state. This storage space consists of the SQL Server database, in-process memory, and ASP.NET StateServer service. The SQL Server and the StateServer allows sharing of the session state among several Web servers without the need of server affinity.
Improving the Performance of ASP.NET Session State
Incorporating session state in an application of ASP.NET can add visible overhead to the performance of the application. However, your application can actually benefit from several techniques for reducing session state’s performance impact while, maintaining the desired functionality of state management. Here is a quick look at some of the best practices that enables designing and incorporating high-performing, secure, and scalable solutions for both new and existing session state of ASP.NET:
The InProc mode of the session state holds the record of being the fastest among the integrated modes of state storage. The overhead of this particular mode is restricted from taking out the session ID from a particular request performing a lookup of the cache for the stored application data in the memory space of the application.
In the StateServer and SQLServer, both out-of process modes, session state must be obtained from an external store, before deserializing the same from its representation of binary blob to the in-memory form of state dictionary in the AcquireRequestState stage. The state dictionary needs to be serialized further in the ReleaseRequestState stage and transported to the external storage.
The SessionStateModule usually performs several optimizations for avoiding the overhead wherever it might be possible. The four categories of the optimizations include:
No work of session state will be performed for pages and handlers coming without the requirement mark of session state. For pages featuring the requirement mark of read-only access of session state, only the initial data and the last access mark will be obtained.
No session will start on requests that come without a specific session ID, until any data is saved in the session dictionary.
No state changes will be carried onto the provider at the end of requests that come without an access of session state or with only read access for session variables. On requests of modified session or the mutable session data, the serialization will only be done for the accessed variables.
Direct serialization are done for primitive types while, the method of BinaryFormatter serialization is used for object type serialization.
The above-mentioned strategies actually focus on the potential approaches to increase the performance of the application by extracting maximum advantage of the optimizations:
Disable session state to stay away from overhead completely
Reduce the serialization overhead and state data deserialization
Reduce the out-of-process overhead of the session transfer continuously from the state store.
By making use of the best practices and making the most of the optimizations, you can actually reduce the impact of the session state performance largely in your application.