TimLeung
TimLeung

Reputation: 3479

Page View Counter like on StackOverFlow

What is the best way to implement the page view counter like the ones they have here on the site where each question has a "Views" counter?

Factoring in Performance and Scalability issues.

Upvotes: 11

Views: 4627

Answers (7)

Luke101
Luke101

Reputation: 65278

Instead of making a database call everytime the database is hit, I would increment a counter using a cache object and depending on how many visits you get to your site every day you have send the page hits to the database every 100th hit to the site. This is waay faster then updating the database on every single hit.

Or another solution is analyzing the IIS log file and updating hits every 30min through a windows service. This is what I have implemented and it work wonders.

Upvotes: 4

Joel Coehoorn
Joel Coehoorn

Reputation: 416039

I've made two observations on the stackoverflow views counter:

  • There's a link element in the header that handles triggering the count update. For this question, the markup looks like this:
    <link href="/questions/246919/increment-view-count" type="text/css" rel="stylesheet" />
    I imagine you could hit that url to update the viewcount without ever actually viewing the page, but I haven't tried it.

  • I had a uservoice ticket, where the response from Jeff indicated that views are not incremented from the same ip twice in a row.

Upvotes: 11

Eduardo Molteni
Eduardo Molteni

Reputation: 39443

For me the best way is to have a field in the question table and update it when the question is accessed

UPDATE Questions SET views = views + 1 WHERE QuestionID = x

Application Object: IMO is not scalable because the may end with lots of memory consumption as more questions are accessed.
Page_views table: no need to, you have to do a costly join after

Upvotes: 0

stephbu
stephbu

Reputation: 5092

I'm a fan of @Guillaume's style of implementation. I use a transparent GIF handler and in-memory queues to batch-up sets of changes that are then periodically flushed using a seperate thread created in global.asax.

The handler implements IHttpHandler, processes the request parameters e.g. page id, language etc., updates the queue, then response.writes the transparent GIF.

By moving persistent changes to a seperate thread than the user-request you also deal much better with potential serialization issues from running multiple servers etc.

Of course you could just pay someone else to do the work too e.g. with transparent gifs.

Upvotes: 0

Zote
Zote

Reputation: 5379

You can implement an IHttpHandler to do that.

Upvotes: 0

Bob Fanger
Bob Fanger

Reputation: 29907

The counter i optimized works like this:

UPDATE page_views SET counter = counter + 1 WHERE page_id = x
if (affected_rows == 0 ) {
   INSERT INTO page_views (page_id, counter) VALUES (x, 1)
}

This way you run 2 query for the first view, the other views require only 1 query.

Upvotes: 8

Fran&#231;ois
Fran&#231;ois

Reputation: 945

An efficient way may be : Store your counters in the Application object, you may persist it to file/DB periodically and on application close.

Upvotes: 6

Related Questions