tags:

views:

278

answers:

3

I have seen several websites that show you a real time update of what's going on in the database. An example could be

  • A stock ticker website that shows stock prices in real time
  • Showing data like "What other users are searching for currently.."

I'd assume this would involve some kind of polling mechanism that queries the database every few seconds and renders it on a web page. But the thought scares me when I think about it from the performance standpoint.

In an application I am working on, I need to display the real time status of an operation that a user has submitted. Users wait for the process to be completed. As and when an operation is completed, the status is updated by another process (could be a windows service). Should I query the database every second to get the updated status?

A: 

It's not necessarily done in the db. As you suggested that's expensive. Although db might be a backing store, likely a more efficient mechanism is used to accompany the polling operation like storing the real-time status in memory in addition to finally on the db. You can poll memory much more efficiently than SELECT status from Table every second.

Also as I mentioned in a comment, in some circumstances, you can get a lot of mileage out of forging the appearance of status update through animations and such, employ estimation, checking the data source less often.

Edit

(an optimization to use less db resources for real time)

Instead of polling the database per user to check job status every X seconds, slightly alter the behaviour of the situation. Each time a job is added to the database, read the database once to put meta data about all jobs in the cache. So , for example, memory cache will reflect [user49 ... user3, user2, user1, userCurrent] 50 user's jobs if 1 job each. (Maybe I should have written it as [job49 ... job2, job1, job_current] but same idea)

Then individual users' web pages will poll that cache which is always kept current. In this example the db was read just 50 times into the cache (once per job submission). If those 50 users wait an average 1 minute for job processing and poll for status every second then user base polls the cache a total of 50 users x 60 secs = 3000 times.

That's 50 database reads instead of 3000 over a period of 50 min. (avg. one per min.) The cache is always fresh and yet handles the load. It's much less scary than considering hitting the db every second for each user. You can store other stats and info in the cache to help out with estimation and such. As long as fresh cache provides more efficiency then it's a viable alternative to massive db hits.

Note: By cache I mean a global store like Application or 3rd party solution, not the ASP.NET page cache which goes out of scope quickly. Caching using ASP.NET's mechanisms might not suit your situation.

Another Note: The db will know when another job record is appended no matter from where, so a trigger can init the cache update.

Despite a good database solution, so many users polling frequently is likely to create problems with web server connections and you might need a different solution at that level, depending on traffic.

John K
Please see my comments added to my question. If I use the caching mechanism, the real time update is not "real" and the cached data becomes stale pretty quickly (in my use case)
DotnetDude
The edit is a solution to incur less db reads yet keep it real time.
John K
... and the solution can apply to the situation where the central db is being updated from various places.
John K
This sounds like a good solution to implement. The first thing that comes to mind is to use something similar to a memcache, but it requires significant effort in setting it up. What kind of cache do you suggest that may not require too much resources/time/effort?
DotnetDude
I agree with you that memcache would be overkill in this situation. A cache I use is Microsoft's Caching Application Block (with sample code) http://msdn.microsoft.com/en-us/library/cc309103.aspx from their Enterprise Library download http://msdn.microsoft.com/en-us/library/cc467894.aspx It plugs into .NET. Or maybe ask another question on SO about a simple cache to use outside the ASP.NET layers.
John K
A: 

Maybe have a cache and work with it so yo don't hit the database each time the data is modified and update the database every few seconds or minutes or what you like

Yassir
A: 

The problem touches many layers of a web application.

On the client, you either use an iframe whose content autorefreshes every n seconds using the meta refresh tag (HTML), or a javascript which is triggered by a timer and updated a named div (AJAX).

On the server, you have at least two places to cache your data:

One is in the Application object, where you keep a timestamp of the last update, and refresh the cached data as your refresh interval elapses.

If you want to present data from a database, keep aggregated values or cache relevant data for faster retrieval.

devio