Calculating how many visitors are on the same webpage using rolling average

In this example we show how to count how many visitors are on same webpage URL.

There are many methods that you can use to track the users on same URL. The easiest method is to make assumption that if someone hit an URL, he staying there for 5 minutes. The assumption does not sounds good :), however this is how most of popular counters do it.

Evidences suggests that Google doing it same way.

One of the ways to implement this in Redis is with sorted sets (zset). Another, more clear way is to use normal sets and to "roll" them. This technique is sometimes called rolling average or moving average.

$r = new Redis();
$r->connect("", "6379");

$expire = 5; // min, good values are 3 to 10.

$ip     = $_SERVER["REMOTE_ADDR"]; // you can put session or user id here
$page   = md5($_SERVER["REQUEST_URI"]);

$ts     = time();

$count = 0;

for($i = 0; $i < $expire; $i++){
	$time = date("Hi", $ts + $i * 60);
	$r->sadd(  "page:$time:$page", $ip);
	$r->expire("page:$time:$page", $expire * 60 + 1); 
	// we want set to be live at least $expire minutes.
	// we can optimize it with TTL = $i * 60 + 60
	if ($i == 0)
		$count = $r->scard("page:$time:$page");

echo $count . " visitors looking at this page right now!";

In case you put user names in the redis set, you can easily do smembers() or srandmember() to show some usernames as this is done in some forum softwares.

Redis connection note

The example uses generic connection to the Redis server. To make the example work with our service you will need to use code similar to this one:

// change these parameter according to the information in your instance list
$host = '';
$port = 1234;
$password = 'somehashcode';
$db = 0;

$r = new Redis();
$r->connect($host, $port);

blog comments powered by Disqus

Code library

Because we want this page to be useful for memcached users, we tagged with memcached all examples that may be "recreated" for memcached server.

MySQL cache in PHP

How to use Redis for cache MySQL queries in a way similar to memcached
Date: 2011-07

Tags: cache memcached mysql php

How to prevent website to be web-scraped

How to use Redis to prevent web-scraping parsing and web-spam
Date: 2011-07

Tags: memcached webstats security php anti-spam

'Rotating' news

How to do 'Rotating' news list
Date: 2011-07

Tags: php

Show random element

How pool random element from news list
Date: 2011-07

Tags: php

Accounting / Vote example

How Redis can help us with Accounting / Vote / Like / Recommend / +1 clicks
Date: 2011-07

Tags: mysql php

Scoreboard example

How to implement fast hi-score table
Date: 2011-07

Tags: php

Scaling example

How to scale Redis on several servers using sharding
Date: 2011-07

Tags: cloud memcached sharding scaling php

Distributed work pool / pipeline example

How to implement distributed work pool and to scale some work across many computers
Date: 2011-07

Tags: scaling php cloud

Simple realtime web counter

How to collect and store information for page visitors
Date: 2011-07

Tags: webstats php

Emulation of expiration of the set members

How to make set members to expire
Date: 2011-07

Tags: php

Lock example

How Redis can help us with user level locks
Date: 2011-08-23

Tags: memcached lock php

Message queue example

Creating distributed message queue
Date: 2011-09-17

Tags: scaling queue php cloud

Index example

Building reverse index using sets
Date: 2011-11-01

Tags: php

Using Redis for MySQL autoincrement

Using Redis to speed up MySQL inserts
Date: 2011-01-13

Tags: mysql scaling php cloud

Using Redis as "data buffer"

Using Redis for caching MySQL or Cassandra data inserts
Date: 2012-08-04

Tags: cloud mysql sharding webstats scaling php cassandra

Calculating how many visitors are on the same webpage using rolling average

How to produce 'There are 123 visitors on this page' sign
Date: 2011-07

Tags: memcached webstats php

© Jul.2011 - 2019,
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy.