What counts as a 'view'? It can't be a view by unique users because some questions have over 100k views. I don't think it counts as a view every time you reload a page. Is it a view per user per day? Is it a view only after there is an edit, comment or answer?


As TARS stated in his comment, a view is basically a unique IP visit, 15 minutes apart. This has been discussed at length on Meta:SE (see below)

As to how some questions have hundreds of thousands of views, that's the power of google and the "Hot Network Questions" bar.

I've recently investigated the functioning of the Stack Overflow views counter because I wanted to implement something similar (which I did). My ramblings on the matter are here: Dissecting the Stack Overflow views counter

So, how that thing works? Quite simply, as I turned out to be.

Every question page has that counter link embedded in it:

http://stackoverflow.com/posts/3590653/ivc/[Random code]

which is hit with every page load (either cached or not).

There is some sort of a throttling mechanism in action. It saves the information about a question view per visitor like in pairs:

  • for anonymous users, it is IP + QuestionNr.

  • for authenticated users it is UserNr + QuestionNr.

This information is saved in an expiring cache entry for about 15 minutes. If a subsequent hit sees the entry is still there it discards the new hit. If it is already gone it allows for a new record.

Every time a new hit is registered, it is also added to a memory buffer in addition to the expiring cache entry. The buffer itself also expires after a few minutes or after it is filled up to a certain size, whichever happens first. When it expires, everything it has accumulated is written into the database in bulk. They call it a "buffered write scheme". I like the term. Basically the buffer entries are grouped per question and then just added to the sum of the questions views, no particular table to store every visit details (too much to store), like:

UPDATE Question
SET Views = Views + @NewViews
WHERE Nr = 36278

And the same for every question which has any views registered in the buffer. To optimize and minimize the database access you send the entire data for multiple questions to your update query in one run. You can format the data as XML, join to it inside the query and perform the update in one statement.

That's pretty much it.

I haven't been able to figure out what the [Random code] in the counter url does, but that's okay. Without that mysterious part, I have implemented this scheme under ASP.NET MVC + SQL Server about two weeks ago for a project I'm currently working on. I've got it running on my development machine since then and it's worked like a charm. Views are properly registered as they should. :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .