2

I would like to ask for feedback for some programming logic concept & ideas. I am building a site where i would like to keep track on how many pageviews a page has daily. I have thought of 2 ways to do it, would like to get feedback from fellow experts on which approach you would recommend.

Approach 1

Keep a field in the document called pageviews. On every page load, i would increment this value by 1. This seems to be the most naive way of doing it. However, i am concern on the database performance. Imagine i have 10000 concurrent users on that page, or a hacker is auto-refreshing this page every millisecond.

Approach 2

Create a file (maybe .txt). And push data into it every pageview. I can tabulate the pageviews based on the data stored in the txt file. Using this approach, there will be no database load, will this be a better approach?

Appreciate more expert views on how you would go about doing this.

1
  • 2
    Whenever your page is loaded there is most likely already going on a lot in your database. Incrementing a counter should possibly not make much of a difference. (Maybe I would avoid to have the counter in the document itself since this would make it necessary to update the whole doc). So basically the problems would be the same anyway. Writing a file is most likely not more performant than what your highly optimized database engine can do. Also there would be Google Analytics or similar services as possible alternatives. Commented Sep 16, 2015 at 9:15

1 Answer 1

2

Your web server probably already keeps an access log somewhere, and so has solved the problem of repeated editing the same file from multiple sources efficiently (e.g. Apache access log, nginx access log). You should be able to use that instead of approach #2, in which the only question is whether approach #1 can be made faster (unlikely) or could record more useful data than your web server does (maybe). There are also third-party page view trackers, such as Google Analytics and Statcounter. I'd try any of those options before attempting to roll your own solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.