views:

41

answers:

2

In order to improve speed of chat application, I am remembering last message id in static variable (actually, Dictionary).

Howeever, it seems that every thread has own copy, because users do not get updated on production (single server environment).

private static Dictionary<long, MemoryChatRoom> _chatRooms = new Dictionary<long, MemoryChatRoom>();

No treadstaticattribute used...

What is fast way to share few ints across all application processes?

update

I know that web must be stateless. However, for every rule there is an exception. Currently all data stroed in ms sql, and in this particular case some piece of shared memory wil increase performance dramatically and allow to avoid sql requests for nothing.

I did not used static for years, so I even missed moment when it started to be multiple instances in same application.

So, question is what is simplest way to share memory objects between processes? For now, my workaround is remoting, but there is a lot of extra code and I am not 100% sure in stability of this approach.

A: 

I'm assuming you're new to web programming. One of the key differences in a web application to a regular console or Windows forms application is that it is stateless. This means that every page request is basically initialised from scratch. You're using the database to maintain state, but as you're discovering this is fairly slow. Fortunately you have other options.

If you want to remember something frequently accessed on a per-user basis (say, their username) then you could use session. I recommend reading up on session state here. Be careful, however, not to abuse the session object -- since each user has his or her own copy of session, it can easily use a lot of RAM and cause you more performance problems than your database ever was.

If you want to cache information that's relevant across all users of your apps, ASP.NET provides a framework for data caching. The simplest way to use this is like a dictionary, eg:

Cache["item"] = "Some cached data";

I recommend reading in detail about the various options for caching in ASP.NET here.

Overall, though, I recommend you do NOT bother with caching until you are more comfortable with web programming. As with any type of globally shared data, it can cause unpredictable issues which are difficult to diagnosed if misused.

nizmow
I am answering just because this may help others. It looks ike you did not understand my idea completely. 1) This information is not user dependent and session state doesn't not fit 2) Cache has different values in different processes. Maybe sqldependency will work, but I want to eliminate all DB calls at all, when nothing changed
Sergey Osypchuk
To clarify, you want to share data between two web applications?
nizmow
A: 

So far, there is no easy way to comminucate between processes. (And maybe this is good based on isolation, scaling). For example, this is mentioned explicitely here: http://stackoverflow.com/questions/993417/asp-net-static-objects/993436#993436

When you really need web application/service to remember some state in memory, and NOT IN DATABASE you have following options:

  1. You can Max Processes count = 1. Require to move this piece of code to seperate web application. In case you make it separate subdomain you will have Cross Site Scripting issues when accesing this from JS.

  2. Remoting/WCF - You can host critical data in remoting applcation, and access it from web application.

I am trying #2 and it looks to be working solution.

Sergey Osypchuk