How stackoverflow maintains the number of views per post?

How does Stack Overflow maintain the number of views per post?

  • I have been an active user on Stack Overflow for the last two years and I really like the way it is designed. For the last few days I have been thinking about the design behind maintaining the number of views per question on the site: If a user visits the same question a second time, the counter won't increment. I am really keen on knowing how they are storing all user information for each visited question and how quickly they inspect whether a user has visited a page already or not.

  • Answer:

    I guess you can refer to the following posts on Meta SE: http://meta.stackexchange.com/questions/36728/how-are-the-number-of-views-in-a-question-calculated http://meta.stackexchange.com/questions/87092/dissecting-the-stack-overflow-views-counter Edit: The below contents were shared in the above mentioned http://meta.stackexchange.com/questions/36728/how-are-the-number-of-views-in-a-question-calculated. So, how does it work? Quite simply, as it 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.

Bhargav Kumar R at Meta Stack Overflow Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.