Thursday, December 6, 2012

Session State


Memory on the server is not always the best or most scalable place to store session state. Forexample, you might have a load-balanced server farm that routes requests between web serversbased on server load. In this case, you cannot guarantee that a user will always be routed to thesame server, and thus you might lose his or her session information. One solution to this issueis a smarter load balancer that allows for “sticky” sessions that assign users to servers and keepthem there throughout a session. However, this can also be problematic if a server fails or if youneed to take one down.Fortunately, ASP.NET provides a few different session management modes for your appli-cation. These modes are configurable. You can, for example, start out by using an in-memory(InProc) mode and, as your site grows, switch session state to a database or a state server.ASP.NET provides the following session storage options:■  inProc  Stores session state in memory on the web server. This is the default mode. Itoffers much better performance than using the ASP.NET State Service or storing stateinformation in a database server. However, it is limited in load-balanced scenarios whereyou might make a performance trade-off to increase scalability. The InProc mode is agood choice for simple applications. However, applications that use multiple web serversor that persist session data between application restarts should consider using theStateServer or SQLServer modes.■  stateserver  Stores session state in a service called the ASP.NET State Service. Thisensures that session state is preserved if the web application is restarted and alsomakes session state available to multiple web servers in a web farm. ASP.NET StateService is included with any computer set up to run ASP.NET web applications; how-ever, the service is set up to start manually by default. Therefore, when configuringthe ASP.NET State Service, you must set the startup type to Automatic.■  sQLserver  Stores session state in a SQL Server database. This ensures that session stateis preserved if the web application is restarted and also makes session state available tomultiple servers in a web farm. On identical hardware, the ASP.NET State Service outper-forms SQLServer. However, a SQL Server database offers more robust data integrity andreporting capabilities. In addition, many sites run their SQL Server databases on powerfulhardware. You will want to performance-test for your scenario.