views:

551

answers:

9

I keep hearing that it's bad practise to store large object collections / anything in the session. Often during conversation it's quickly followed by: 'Just turn sessions off'

So what is the general problem with sessions? I use them a fair bit and since they 'real' session is stored behind a strongly typed container I don't really see the issue.

+1  A: 

I had thought that it largely depends on the traffic to your web site. If you are running something like amazon.com, trying to store the user's shopping cart in a session would take huge amounts of IIS allocated memory, bringing down your web server. For smaller web sites, session variables are fine to use in moderation.

Matt Hamsmith
A site as big as Amazon would never use InProc sessions though as they surely are using a web farm of some sort and InProc sessions would fail in that scenario. Yet another reason to use the SQL Server session provider.
Steve Wortham
A: 
  1. Session requires the user to have cookies turned on
  2. If you're working in a web farm, you'll run into trouble.

I guess these reasons don't have anything to do with storing large objects in session, just in using sessions at all.

ScottE
There are many good options for session state with a server farm - SQL server persistence and session state server come to mind.
Andrew Hare
Sure, these only apply to InProc, but have you ever seen cookie-less sessions used? I haven't. SQL / state server also have their limitations.
ScottE
And I think we're both talking about forms authentication as well...
ScottE
Scratch that, I see that you can use sql / state server to manage regular sessions.
ScottE
+1  A: 

Storing large objects in Session is bad, yes, but "large" is relative.

Basically, storing an object in session will keep it in memory until the session expires, so if you have a site with a high user count all storing mega-objects in their session, you'll kill your server pretty quickly.

With that being said, an argument could be made for the idea that if you have objects that are 5k+ in memory and have enough users to actually cap out a server then you can probably afford more hardware anyway.

There are also topics like server clustering and session integrity between boxes in the cluster. Some frameworks handle this, I don't know if .NET does or not.

Jasconius
+7  A: 

There is nothing wrong with session - you just need to be mindful of its limitations. To say "just turn off session" is throwing the baby out with the bathwater.

Andrew Hare
That's where my thinking is, I guess I just wanted to make sure I wasn't missing something really important.
ilivewithian
-1 While this is true, it does not explain the Pros and Cons of Sessions.
Chuck Conway
It's appears the Q'er is more concerned about being correct than the actual answer.
Chuck Conway
+1  A: 

There are two things to be careful of:

  1. Memory consumption: if you store large data objects in session and you have many user you may well run out of memory or at the very least triggering many early recycling of your application
  2. This is only a problem if you have multiple web servers (web farm): the session has to be stored externally (not in process) in a SQL server or a windows service so that it is accessible from different machines. This can be quite slow at times.
Locksfree
A: 

2 major issues come to mind... 1) Persistence of sessions across servers when you start scaling your website 2) Memory usage explosion from storing UI objects in session state

The more serious issue is the tendency to store objects in session. When you store something as innocuous as a Label from a page on your page, you get LOTS of unwanted object attributes as well. You probably just wanted the text of that label stored in your session, but along with it, you get references to the page itself...and all of a sudden, you have a massive usage of memory to store the page, its view state, and lots of unwanted attributes in memory on your server.

Check out this link about storing UI elements in session

jkelley
As for the persistence across multiple servers, that's another reason to avoid the InProc session provider in favor of SQLServer or StateServer.
Steve Wortham
+2  A: 

Hi...

There is a huge difference between storing BIG objects and small objects in a session

The session will stay alive on a server untill it expiers, and that means those big objects pollute your available memory. If you do that with a server under load, or a server that runs many application pools, then this can cause trouble.

You dont need cookies to have a session, since ASP cal also encode that information in the urls. Also you can configure the session store to run out of process, or even to store the information inside a SQL Server (reducing the memory load on the server, and enabeling sessions across a farm)

So basically: Objects are ok - Big objects not

Heiko Hatzfeld
+1 for being the first person to mention memory usage of InProc as well as the alternative of using SQL Server as a session provider.
Steve Wortham
+2  A: 

Here's my take -- sessions are not bad but sometimes they are overused. It can also be harder to understand a web application's flow when it relies on a lot of sessions so of course you should be careful not to get carried away.

However, you should feel free to use them anytime you need to store temporary data to be made accessible across multiple pages. In no other situation should they be used. But that situation is one for which sessions were specifically designed.

Now, if you're worried about memory consumption on the server, that's not necessarily a reason to avoid sessions. But it may be more of a reason to avoid the InProc session provider. In fact I'm not a fan of InProc sessions as they tend to expire prematurely after a certain number of recompiles in your application.

What I actually prefer and nearly always use are SQL Server sessions. They'll be slightly slower, but the benefits are numerous. They'll persist even if the server is rebooted and that makes them a very reliable choice. And of course since they're stored in the SQL file system instead of in memory, they won't make such a big hit on memory.

This article on MSDN talks about the various session providers and also explains how to configure SQL to handle your sessions. If you don't have SQL, just know that even the free SQL Server Express 2008 can be configured as your session provider.

Steve Wortham
A: 

You may want to check out this question as well.

Aaron Daniels