tags:

views:

166

answers:

2

Here's a hypothetical situation I've been wondering about lately. Suppose I have an interactive page created in ASP.NET and Ajax that I want multiple users to be able to manipulate at the same time. What would be the best way to maintain this page's state? Especially if the page was relatively ephemeral, and did not particularly need to be stored in a database?

As a concrete example, suppose I'm implementing a basic game of checkers. The server needs to maintain the state of the board at all times. The clients need to check the board's status periodically, and need to be able to transmit their changes to the board. When all the players leave, or when the game ends, the board is no longer needed and the server can dispose of it.

I could store everything in a database, retrieving it when the clients ask for a board update, and saving it when the server receives an update. But that seems like a lot of extra overhead.

What about static variables? Are they reliable enough/efficient enough for this job? Is there another storage mechanism I could use? Or is this so far outside the limits of what ASP.NET can do efficiently that I should be looking at an entirely different technology if I ever want to implement this?

Update:
One quick update, to see if I can get another round of answers. After a bit of research, here's what I have so far:
Manu has suggested using MemCached independently to store the data. I like that this solution works even in multi-server environments, but I have noted a few drawbacks:
1) MemCached's site states that it's for read-heavy applications (which this isn't, relative to a traditional web app), and that it's "unreliable." I haven't gotten far enough to determine how important these characteristics are (i.e. are writes slow? Is there a propagation delay?).
2) In an implementation somewhat close to my application (storing session data), the MemCached site uses a database behind the cache, even though the data does not need to be persisted.
3) I have some concerns about how usable MemCached would be in a hosted environment.

Looking at static variables, they do seem to be fast. They aren't thread safe, but that can be accounted for. However, they ARE local to the worker process, so I'm not sure how they work with load balancing or a web garden.

Finally, there is at least one implementation of an ASP.NET-based chat client (VERY similar to this example) which uses a database as a back-end. With proper caching, this may just be "good enough."

+1  A: 

This depends alot on usage of this hypothetical system. With reasonable refresh rates (5s perhaps?), and a suitably optimised database, proper keys, etc. this shouldn't put too much strain on your server, especially if you do this via JSON-RPC style calls with differential updates (only give me changes since x time) rather than a full board update.

Not having used ASP.NET, I can't comment on it's static variables, I assume they work something like session variables in PHP, in which case as long as it's atleast as performant as PHP, this method shouldn't be too troublesome either. Infact, it's very likely that ASP.NET stores these variables in some form of database anyway. I can't imagine this being a much worse option than the database option above, if not a better option performance wise, as long as there's some way to share the data between clients.

Matthew Scharley
Static variables are already shared between users, so that wouldn't be an issue. I think they might be scoped to the worker process... I'm just not sure how stable a communication channel they are.
AaronSieb
+1  A: 

try memcached: http://www.danga.com/memcached/

Manu
Are you proposing using a cached database instance to store the board state? Or is there more functionality to memcached that I'm missing?
AaronSieb
you can store all kinds of values in memcached, my suggestion is to store the object representation of the board state directly in memcached.
Manu
The MemCached site seems to be resistant to the idea of being used independently:"The standard practice is to store your session in your [...] database system and then use memcached as a cache." and "If you have a [site] that contains mostly read threads then memcached can help"
AaronSieb