views:

479

answers:

3

Hello, I understand that all InProc session data is always gone when its w3wp owner process recycles as it only resides in the w3wp memory.

I wondered though if it is possible to cache the session data when recycling happens somewhere external to the process, and then reinject (and rebuild) the session when it comes back up. That way I'd get the speed of InProc with the reliability of state server-like externalization when necessary. Is this possible?

A: 

Maybe you can store enough information in a cookie (or in ViewState) so that you can re-create a session based on that data in the case the worker process was recycled.

Or you could create your own state server (e.g. a windows service) where you store part of your session, and access this service from the web app via remoting or similar.

M4N
this won't work. Max cookie size is about 4k. Cookies are also a highly inefficient and insecure way to push data back and forth.
Nissan Fan
Yes, but maybe you only need to store some IDs in that cookie (e.g. user-id, current-page, etc.) and can restore the rest of the session from database if required.
M4N
A: 

You should use the SQL Server for Session State if that's your intention. What you want are sessions that can recover from a complete ending of the host processs. Neither inproc or stateserver support that as once they go down they lose all data. You could write your own state method, but that would be laboriuous. Here's directions if you want to do that:

http://www.exforsys.com/tutorials/asp.net-2.0/asp.net-2.0-customizing-the-session-state-mechanism.html

What you may want to do is not use sessions at all, but rather rely more on ViewState. If you have a high traffic site that's a good way to push the workload off to clients.

Nissan Fan
Not a good idea to bloat the page with ViewState to store session information.
David
I have sites with several thousand concurrent users. For high demand sites ViewState is a great way to reduce workload off the server.
Nissan Fan
I'm not sure in your comment if you actually read my post.
Nissan Fan
Session state holds objects in memory. It requires available memory, that's it. ViewState is encoded into each and every web request, and requires the server to read it in and decode it at the beginning of the request, and then encode and spit it out at the end of the request, all while bloating each web request. There is no processing that goes on at the client side, but the client does have to download all that data and then upload it all back again on each request. You increase the workload for the user AND the server.
David
Take 15,000 sessions in a farm and transfer the memory required out as encrypted ViewStates. The performance gain is immense and through the roof. Not to mention the fact that you never have to worry about keeping sessions around for people who come and go. Your approach makes sense for a small website, but when you have 25 servers layer 7 switched and getting about 25,000 unique visitors per hour tell me how well sessions work.
Nissan Fan
+2  A: 

No, this isn't possible. There is no API to "warm up" in-process session state or cache. Such a solution would be unreliable anyway. You couldn't gaurantee that the last thing your app did would be to export its current session state, so you could never count on the imported data to be current anyway.

You can use the out-of-proc state server on the same host as your web application. Then the web application can recycle freely, keeping the state information intact. This is quite a bit faster than using SQL Server to manage sessions, as you don't have to deal with the overhead of SQL or network transit to another machine, the only way it's worse than in-process session state is that data has to marshal across process boundaries, but as long as you don't have a massive amount of rapidly changing session data, this shouldn't be noticeable at all.

There are also other alternatives, such as Microsoft Project Code Named "Velocity" or ScaleOut Software's SessionServer (among others I'm sure) that offer distributed caching mechanisms that can keep session state or cache synchronized between servers in a server farm without having to resort to using SQL Server.

Contrary to what another user posted, I would NOT use ViewState to store session data if at all possible. For one, it's not true session state, if a user moves back or forward it can be lost. Second, it's fairly insecure. Third, it causes massive page bloat if abused.

David