views:

202

answers:

5

More and more sites are displaying the number of views (and clicks like on dzone.com) certain pages receive. What is the best practice for keeping track of view #'s without hitting the database every load?

I have a bunch of potential ideas on how to do this in my head but none of them seem viable.

Thanks, first time user.

A: 

You could set up a flat file that has the number of hits in it. This would have issues scaling, but it could work.

If you don't care about displaying the number of page views, you could use something like google analytics or piwik. Both make requests after the page is already loaded, so it won't impact load times. There might be a way to make a ajax request to the analytics server, but I don't know for sure. Piwik is opensource, so you can probably hack something together.

Buddy
yeah we are looking for something that can scale, so probably no files. could have issues with locking and what not.I'm looking to display the number so analytics software isn't what I'm looking for. Granted, local analytics software could do this but it still saves to db, could be slow
PStamatiou
+2  A: 

I would try the database approach first - returning the value of an autoincrement counter should be a fairly cheap operation so you might be surprised. Even keeping a table of many items on which to record the hit count should be fairly performant.

But the question was how to avoid hitting the db every call. I'd suggest loading the table into the webapp and incrementing it there, only backing it up to the db periodically or on webapp shutdown.

Chris
A: 

If you are using server side scripting, increment it in a variable. It's likely to get reset if you restart the services so not such a good idea if accuracy is needed.

alok
+1  A: 

One cheap trick would be to simply cache the value for a few minutes.

The exact number of views doesn't matter much anyway since, on a busy site, in the time a visitor goes through the page, a whole batch of new views is already in.

Tiberiu Ana
that doesn't solve the problem of writing to the database on every pageview.
Seun Osewa
A: 

One way is to use memcached as a counter. You could modify this rate limit implementation to instead act as general counter. The key could be in yyyymmddhhmm format with an expiration of 15 or 30 minutes (depending on what you consider to be concurrent visitors) and then simply get those keys when displaying the page.

Nice libraries for communicating with the memcache server are available in many languages.

Anon