views:

69

answers:

4

Hi,

I have used ASP.NET in mostly intranet scenarios and pretty familiar with it but for something such as shopping cart or similar session data there are various possibilities. To name a few:

1) State-Server session

2) SQL Server session

3) Custom database session

4) Cookie

What have you used and what our your success or lessons learnt stories and what would you recommend? This would obviously make a difference in a large-scale public website so please comment on your experiences.

I have not mentioned in-proc since in a large-scale app this has no place.

Many thanks Ali

+1  A: 

In-proc certainly can have a place in a large-scale application; it just requires sticky sessions at the load balancing level. In fact, the reduced maintenance cost and infrastructure overhead by using in-proc sessions can be considerable. Any enterprise-grade content switch you'd be using in front of your farm would certainly offer such functionality, and it's hard to argue for the cash and manpower of purchasing/configuring/integrating state servers versus just flipping a switch. I am using this in quite large scaled ASP.NET systems with no issues to speak of. RAM is far too cheap to ignore this as an option.

Rex M
It is interesting. Sometimes things that are no-no make a comeback and sticky session seems to be one of them, as you say.
Aliostad
+1  A: 

In-proc session (at least when using IIS6) can recycle at any time and is therefore not very reliable because the sessions will end when the server decides, not when the session actually times out. The sessions will also expire when you deploy a new version of the web site, which is not true of server-based session providers. This can potentially give your users a bad experience if you update in the middle of their session.

Using a Sql Server is the best option because it is possible to have sessions that never expire. However, the cost of the server, disk space, its maintenance, and peformance all have to be considered. I was using one on my E-commerce app for several years until we changed providers to one with very little database space. It was a shame that it had to go.

We have been using the state service for about 3 years now and haven't had any issues. That said, we now have the session timeout set at an hour an in E-commerce that is probably costing us some business vs the never expire model.

When I worked for a large company, we used a clustered SQL Server in another application that was more critical to remain online. We had multiple redundency on every part of the system including the network cards. Keep in mind that adding a state server or service is adding a potential single point of failure for the application unless you go the clustered route, which is more expensive to maintain.

There was also an issue when we first switched to the SQL based approach where binary objects couldn't be serialized into session state. I only had a few and modified the code so it wouldn't need the binary serialization so I could get the site online. However, when I went back to fix the serialization issue a few weeks later, it suddenly didn't exist anymore. I am guessing it was fixed in a Windows Update.

NightOwl888
+2  A: 

The biggest lesson I learned was one I already knew in theory, but got to see in practice.

Removing all use of sessions entirely from an application (does not necessarily mean all of the site) is something we all know should bring a big improvement to scalability.

What I learnt was just how much of an improvement it could be. By removing the use of sessions, and adding some code to handle what had been handled by them before (which at each individual point was a performance lose, as each individual point was now doing more work than it had before) the performance gain was massive to the point of making actions one would measure in many seconds or even a couple of minutes become sub-second, CPU usage became a fraction of what it had been, and the number of machines and amount of RAM went from clearly not enough to cope, to be a rather over-indulgent amount of hardware.

If sessions cannot be removed entirely (people don't like the way browsers use HTTP authentication, alas), moving much of it into a few well-defined spots, ideally in a separate application on the server, can have a bigger effect that which session-storage method is used.

Jon Hanna
Tanks Jon. So where do you keep state? Cookie or custom database?
Aliostad
Ideally, you transfer state and don't need any shared state between client and server. Shared state is always a kludge. If I do need that kludge, then sessions are a good way to do it, and opaque cookies can be better. For identifying users, HTTP authentication rocks. Even when I do use them, I try to keep their use to a minimum and transfer request-relevant state with the request. I'm practical rather than dogmatic though; I'll go to a lot of effort to avoid shared state, but I won't ban it outright.
Jon Hanna
@Jon - HTTP Authentication? I am intrigued. Could you post a code sample of how this could be used to identify a user between requests?
NightOwl888
@NightOwl, whether the authentication is Basic (never use without HTTPS) Digest (be careful), Windows, Kerberos or whatever it will have to identify the user. In ASP.NET it would normally affect the Page.User property (if you roll your own it might not, but really should), and from there you hit whatever backing store (usually DB) has information on that user. A lot of session systems basically care about the same thing. I'd also trust any of these except basic over session-based auth (they have their flaws, but so does session-based, and generally more so). The big downside is people...
Jon Hanna
... tend to react negatively to the way browsers present such authentication, out of lack of familiarity. For this reason I tend to use it more often when the audience is limited ("admin" level users who learn about the system to get their job done, rather than general public users). In doing one small job (identifying who the user is, the rest of the code can find what it needs about the user when it needs to) rather than grabbing a large bag of stuff like most session systems, it tends to scale better. It should still be limited to where its needed (nothing scales better than doing nowt).
Jon Hanna
A: 

If you are concerned about security, state server is a no-no. State server performs absolutely no access checks, anybody who is granted access to the tcp port state server uses can access or modify any session state.

In proc is unreliable (and you mentioned that) so that's not to consider.

Cookies isn't really a session state replacement since you can't store much data there

I vote for a database based storage (if needed at all) of some kind, it has the best possibility to scale.

Onkelborg