views:

611

answers:

7

I am watching the ASP.NET learn videos on asp.net/learn. In this tutorial, they are building a quiz engine. At one point, the narrator explains that we are going to use the Session object to maintain the state between each page (each page contains a question and four answers). He says that "since this is a low traffic website" it is okay to use Session and that he doesn't have the time to implement a more sophisticated method.

I am just wondering what alternate method(s) is he hinting at? And why is session a bad choice for a high traffic website?

+10  A: 

Storing data in a database, or in cookies or some other method that is not directly tying up web server memory.

In addition to load, session also raises issues with the ability to use farms since you would either need to synchronize the session across the farm, or make sessions sticky, which can impact scalability.

Jason Coyne
I agree, when we used session state we ended up doing the same thing. +1
John
+3  A: 

Session data is stored in the RAM of the server, if you have a high traffic site that is going to get full real quick and the last thing you want is that data being swapped to disk.

As gaijin42 says, cookies or a DB are the alternative.

Adam Pope
Session data is only be default stored in memory, but you can store it in a database.
Arkain
+2  A: 

For high traffic websites you might be looking at Memcached. It is a caching mecanism that is stored on the RAM of a remote computer. Just recently a win32 port has been made of the library (was only possible with linux before).

Erick
+1  A: 

I'm not going to repeat what was already mentioned here, but another alternative is using the Application hash. It should be used sparingly, since it will consume memory on your web server as Adam has already mentioned, but it does provide a good way to cache things that are common across ALL your users.

This keeps you from having to go back to your database to retrieve information that most likely was already asked for by someone else.

Another alternative similar to Application is Cache which has more flexibility in terms of when it gets released, duration, etc.

Here's some links in case you're interested: ASP NET Caching Application State

Joseph
+3  A: 

Session as a state storage method is rough in high traffic systems for several reasons.

First, the default Session storage method is in-process, meaning that if you have a load-balanced web farm, you'll constantly 'lose' Session information as a user gets pages served from different servers.

The in-proc Session server also dies when an app pool is recycled, which happens more often on higher traffic servers.

The scalability options for Session data are

  1. Use the freely available ASP.NET Session Server and point all your applications at it
  2. Use SQL Server to store Session data.

Due to the nature of Session data in general, neither of these is a very good option for a very high traffic site (unless you have unlimited money to throw at the hardware).

Harper Shelby
+1  A: 

We use a database for anything high traffic or that will result in large session state. Instead we store a pointer in the real sessionstate that points to our database record. Then the only overhead we have is the bandwidth between the web server and database server which will be much less than between any given user and the web server.

John
I am surprised that webserver to database is considered fast, whereas webserver to client is considered slow. How is your session stored in your database?
MedicineMan
Even a poorly designed database is going to more than likely out perform web server to client because the client will be going over the internet in most cases, unless you are doing intranet apps. Likely the database server and web server will be in the same physical location and even if not they will probably be on a network optimized for fast communication between the two. Another reason we did it this way was to allow your state to follow you to other computers.
John
+3  A: 

For alternatives you can read the article Nine Options for Managing Persistent User State in Your ASP.NET Application.

In the articles the author explains the pros and cons of each method.

From the summary:

ASP.NET provides many different ways to persist data between user requests. You can use the Application object, cookies, hidden fields, the Session or Cache objects, and lots of other methods. Deciding when to use each of these can sometimes be difficult. This article will introduce the aforementioned techniques and present some guidelines on when to use them. Although many of these techniques existed in classic ASP, best practices for when to use them have changed with the introduction of the .NET Framework. To persist data in ASP.NET, you'll have to adjust what you learned previously about handling state in ASP.

eKek0