9

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.

2
  • 4
    You've been active on SO for two years and somehow you've never been aware of meta all this time?
    – BoltClock
    Jun 16, 2015 at 5:10
  • 1
    I never met a person (someone like you) to tell me this :D Jun 16, 2015 at 5:31

1 Answer 1

11

I guess you can refer to the following posts on Meta SE:

Edit: The below contents were shared in the above mentioned link.

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.

2
  • @Anthon, for motivating me to learn those areas. :p
    – Joy Rex
    Jun 16, 2015 at 5:07
  • Even on Meta sites, links do die. I'd strongly suggest you quote (attribute) those answers in your own answer. Jun 16, 2015 at 8:59

You must log in to answer this question.

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