Efficient way to calculate active users
engineering.helpshift.com
engineering.helpshift.com
Another trade-off used is the space-speed one, which this article is an example of (and a very elegant one at that). If you want to use this then you're going to have to keep intermediate results around in some form.
If you want the best of both worlds (in even less space!) for this particular usecase you can find a pretty good medium by figuring out what the ratio of users:unique users is for some period, then just keep a single count per day, add for your desired period and divide by the ratio found earlier.
This will allow near instantaneous computation whenever the result is required and allows you to push the time-consuming portion of the computation to whenever the system is loaded lightly. The more frequently you compute the ratio the closer your result will be to the actual number.
Of course these are all approximations, if for some reason you require the exact number then you're going to have to simply do more work.
If your last active time table is being fumbled then yes, but I'd worry about all the other parts of your data if that was the case.
Dangerous is not something I would ascribe to a "last active time" table (except if you're using it for security audit purposes)
Secondly, mutable data also means that you have less scope of drawing new insights from historic data. (Primarily because you never have historic data)
Although we are fans of Redis, if you implement it natively you can avoid network latency. Implementing it natively is not a problem because of the commutative nature of HyperLogLog
Further, If one is planning to use Redis it will be better to use built-in HyperLogLog datastructure provided by Redis 2.8.9 as documented here http://antirez.com/news/75
But nearly everyone uses counting users as an example. For this kind of use, I honestly have to ask: at WhatsApp's scale, is 5GB of ram really an issue? It seems like they could probably keep that exact setup and roll it over every minute and not even really tax a modern server.
Or compact it - one bit per person, lookup is just jumping to the address at their ID, counting is just summing, which would probably meet most needs. With this you can handle every person on earth in < 8GB. You can do that with an m3.xlarge on EC2 (15GiB ram) for a measly 25 cents per hour. That's $6/day. That's literally nothing compared to normal server costs.
The problem is not if you're counting one thing (or even 100). The problem is when you want analytics and you want it to scale to 1,000s or 1,000,000s of counters. That may seem ridiculous (who could possibly need that many counters?). But it happens quickly when you say, "How many DAUs do we have? How many from country X? How many using device Y? How many from country X and using device Y?"
Also, to address an idea you mentioned around bitmaps. Bitmaps are great until you have lots of counters and lots of users/things to count. Then the problem is they get very sparse. Imagine user #100,000 does something. You need to allocation 97k of space (lots of zeros behind that 100,000th bit) just to count that one thing. Are bitmaps a good idea? Sure, in a lot of cases they are. The problem is they just break down at some point and that's when these other tricks are really nice.
I'm disappointed. In the first few lines he simply pushes the core issue aside, that is, unique identification. IPs are nowhere near unique identifiers and cookies might be disabled. Once you get unique identification, counting is easy.