Reputation: 1171
What's good practice for a realtime unique counter that won't hurt the database under load?
Upvotes: 2
Views: 3759
Reputation: 28987
You might use a (custom) session handler that stores user-sessions in a database or a caching system, like APC or Memcached.
That way you could count the number of active sessions for your website (each visitor is given a unique session id).
You give sessions a very short session timeout, to prevent users being counted as 'active' even if they are no longer visiting your page.
By using sessions you'll be able to keep track of the number of people currently on your website, even if multiple people share the same IP-address
Upvotes: 0
Reputation: 542
If you're building a website and need counters there's plenty of free to use analytics (Google Analytics, Yahoo Analytics, etc) and counter services which can provide far more information than some simple counter script. This would be my recommendation.
However, if you are building something yourself and have control over the server you could use another tool for actually doing the counting. I have used memcache for this purpose as it supports the "add" and "increment" method, which when called after the other (due to restrictions on the use of the command) can create such high speed realtime counters.
Memcache is an extremely fast (200,000+ requests per second on a slow machine) memory caching solution but is not permanent storage. You will need another solution for storing the numbers counted.
This is my solution for such a task, added to the beginning of a request in its own function:
$m = new Memcache;
$m->addServer('localhost', 11211);
$cacheKey = "performance_".gmmktime();
// Add in case it doesn't exist, no compression, 1 hour timeout
$m->add($cacheKey, (int) 0, false, (60*60));
// Increment cache
$m->increment($cacheKey, (int) 1);
Changing the $cacheKey also allows you to count multiple different things.
I then have another piece of code which collects this information in a cron script each minute and adds it to a database. This solution also works across multiple machines, depending on your implementation needs, allowing for a scalable counter implementation.
Clearly this is only a simple example, but hopefully you can see its use. This code is in use on a large scale multi-server game service running PHP.
Upvotes: 0
Reputation: 1389
Database table visitor_counter
with rows ip
and when
. Then on each page load, check to see if the user's ip is in the database within a given amount of time from when
(datetime field or int field containing timestamp). If the ip is in the database, don't do anything. If it is not, add it. Then periodically prune the values based on time passed since when
. You can use COUNT(ip) for total number of visitors.
And that can be adapted to show visitors in the last X amount of time, and you can add a visits
field to see how many pages the user has loaded, etc.
I'm not entirely sure how this will perform, but in theory it does what you want, I think.
Upvotes: 0
Reputation: 9121
The least stress? Outsource the problem :), e.g., using Google Analytics.
(Google Analytics is not realtime, though. Other providers of analytics tool offer realtime solutions, though, e.g., KISSmetrics)
Upvotes: 1