How and where you choose to store state information for your application can have a major impact on how scalable your application is. Scalability refers to the ability of an application to service requests from increasing numbers of users without any significant decrease in performance or response time.
The following conditions are among the factors affecting application scalability that result from poor decisions in state management:
Failure to disable session state when not used If you’re not using session state within your application, disable it by changing the mode attribute of the sessionState configuration section of Web.config to off. For applications that make limited use of session state, ensure that any pages that don’t use session state include the EnableSessionState=false attribute as a part of the page’s @ Page directive.
Misuse of Application.Lock( ) The Application.Lock( ) method prevents access to all application values for any user other than the user whose request resulted in the call to Application.Lock( ). This continues until the Application.Unlock( ) method is called. You should minimize the time that these values are locked by calling Application.Lock( ) immediately prior to updating a value and calling Application.UnLock( ) immediately afterward. If there is a value to be added to application state that might require some time to calculate, calculate the value and save the value into a variable, and then lock the application, add the value from the variable, and unlock the application.
Storing references to single (or apartment) threaded objects Storing references to non-free-threaded objects at application or session level can interfere with IIS thread management capabilities and can result in severe scalability problems. Avoid storing non-free-threaded objects at application or session level.
In-process session state To scale a Web application beyond a certain point, it’s often necessary to use a Web farm to allow multiple servers to handle requests. In-process session state, the default, is not available to all servers on a Web farm. To use session state with a Web farm, you should set your application’s Session mode to either StateServer or SQLServer as described in the next section.
Overuse of session or application storage Storing numerous object references or large datasets at the application level, and particularly at the session level, can rapidly exhaust the physical memory on a Web server, causing it to substitute virtual memory for storing additional information. This can have a dramatic effect on the performance of an application. You should always ensure that any use of application state (and more important, session state) will not exceed the physical memory resources available on the Web server. Also remember that other applications will be using some of these resources.