php - Unique High Traffic Visitor Counter

2.4k Views Asked by At

I would like to add an visitor counter to my website.

Max Stats:

1 Million Views per minute

6 Queries per Page loading

So I ask you, Is MYSQL and PHP for an unique Visitor Counter the best solution?

Or should I use something different?

1

There are 1 best solutions below

4
peixotorms On BEST ANSWER

For 1 million views per minute where you count views like a hit counter (moreover with duplicate checking)... php / mysql is not going to work very well (unless you have a big farm of servers).

If you need realtime hit couting, I would do this using a combination of nginx + redis http://redis4you.com/code.php?id=009 + javascript, not using php and mysql at all.

1 - Jquery (or javascript) can check if the user has a cookie and if nor, save a "unique random hash cookie" for each (user + page) (don't just use the ip, because multiple users behind a proxy can be using that same ip, ex: large companies with a proxy for internet, universities, public wifi, etc).

2 - After generating the hash, or if cookie exists, Jquery can decide to do an asyncronous request to a nginx server, using the hash as an identifier.

3 - Nginx can talk directly to redis via upstream (very fast)(https://github.com/openresty/redis2-nginx-module) and it can also check for cookies if needed.

4 - Redis is very fast and can auto increment the values (hits) base on a key value (ex: page id), surviving reboots (unlike memcache).

5 - You can query redis from php easily, (like memcache) to display the hit count on any page.

===

Another possible way (but not realtime) would be setup a box with nginx and enable access log.

1 - On your site, include a javascript which calls a file in that nginx server like this: http://stats.server.com/hit.js?page=12345&userhash=some_md5_hash_here

2 - Setup a cron job on the nginx server to parse those logs for unique views.

3 - Nginx can easily serve 20K requests per second on a E3 1230v2 machine.