Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Masters: What are the advantages / disadvantages of using Session variables?

Tags:

I've done a search on this subject already, and have found the same data over and over-- a review of the three different types of sessions. (InProc, Sql, StateServer) However, my question is of a different nature.

Specifically, what is the advantages/disadvantages of using the built in .NET session in the first place?

Here is why I am asking: A fellow .NET developer has told me to NEVER use the built in Microsoft Session. Not at all. Not even create a custom Session State Provider. His reasoning for this is the following--that if you have the Session turned on in IIS it makes all of your requests happen synchronously. He says that enabling session degrades the performance of a web server.

His solution to this is to create a session yourself-- a class that stores all values you need and is serialized in and out of the database. He advises that you store the unique ID to reference this in a cookie or a querystring variable. In our environment, using a DB to store the sessions is a requirement because all the pages we make are on web farms, and we use Oracle-- so I agree with that part.

Does using the built in Session degrade performance more than a home-built Session? Are there any security concerns with this?

So to sum it all up, what are the advantages/disadvantages?

Thanks to all who answer!

like image 972
Micah Avatar asked Apr 28 '09 18:04

Micah


People also ask

What are the advantages and disadvantages of session?

Session is secure and transparent from user because session object is stored on the server. Disadvantages: 1. Performance overhead in case of large number of user, because of session data stored in server memory. 2.

What is the disadvantage of session state?

The disadvantages of using session state are: - Since data in session state is stored in server memory, it is not advisable to use session state when working with large sum of data. Session state variable stays in memory until you destroy it, so too many variables in the memory effect performance.

Are ASP.NET session variables secure?

Very safe, . NET session variables are not the same as cookie variables which can be viewed from the client side, Session variables in this instance are only accessible from the C# code.


1 Answers

My experience has been that the session is a good means of managing state when you use it appropriately. However, often times it's misused, causing the "never ever use the session" sentiment shared by many developers.

I and many other developers have ran into major performance issues when we mistakenly used the session to store large amounts of data from a database, so as to "save a trip." This is bad. Storing 2000 user records per session will bring the web server to its knees when more than a couple of users use the application. Session should not be used as a database cache.

Storing an integer, however, per session is perfectly acceptable. Small amounts of data representing how the current user is using your application (think shopping cart) is a good use of session state.

To me, it's really all about managing state. If done correctly, then session can be one of many good ways to manage state. It should be decided in the beginning on how to manage state though. Most often times, we've run into trouble when someone decides to just "throw something in the session".

I found this article to be really helpful when using out-of-process modes, and it contains some tips that I would have never thought of on my own. For example, rather than marking a class as serializable, storing its primitive datatype members in separate session variables, and then recreating the object can improve performance.

like image 131
Aaron Daniels Avatar answered Oct 23 '22 07:10

Aaron Daniels