HyperLogLog is a probabilistic data structure that estimates the cardinality of a set.
HyperLogLog is a probabilistic data structure that estimates the cardinality of a set. As a probabilistic data structure, HyperLogLog trades perfect accuracy for efficient space utilization.
The Redis HyperLogLog implementation uses up to 12 KB and provides a standard error of 0.81%.
Counting unique items usually requires an amount of memory proportional to the number of items you want to count, because you need to remember the elements you have already seen in the past in order to avoid counting them multiple times. However, a set of algorithms exist that trade memory for precision: they return an estimated measure with a standard error, which, in the case of the Redis implementation for HyperLogLog, is less than 1%. The magic of this algorithm is that you no longer need to use an amount of memory proportional to the number of items counted, and instead can use a constant amount of memory; 12k bytes in the worst case, or a lot less if your HyperLogLog (We'll just call them HLL from now) has seen very few elements.
Conceptually the HLL API is like using Sets to do the same task. You would
SADD every observed element into a set, and would use
SCARD to check the
number of elements inside the set, which are unique since
SADD will not
re-add an existing element.
While you don't really add items into an HLL, because the data structure only contains a state that does not include actual elements, the API is the same:
Every time you see a new element, you add it to the count with
> pfadd hll a b c d (integer) 1 > pfcount hll (integer) 4
Some examples of use cases for this data structure is counting unique queries performed by users in a search form every day, number of unique visitors to a web page and other similar cases.
Redis is also able to perform the union of HLLs, please check the full documentation for more information.
Anonymous unique visits of a web page (SaaS, analytics tools)
This application answers these questions:
- How many unique visits has this page had on this day?
- How many unique users have played this song?
- How many unique users have viewed this video?
Storing the IP address or any other kind of personal identifier is against the law in some countries, which makes it impossible to get unique visitor statistics on your website.
One HyperLogLog is created per page (video/song) per period, and every IP/identifier is added to it on every visit.
- Add some items to the HyperLogLog:
> PFADD members 123 (integer) 1 > PFADD members 500 (integer) 1 > PFADD members 12 (integer) 1
- Estimate the number of members in the set:
> PFCOUNT members (integer) 3
PFADDadds an item to a HyperLogLog.
PFCOUNTreturns an estimate of the number of items in the set.
PFMERGEcombines two or more HyperLogLogs into one.
The HyperLogLog can estimate the cardinality of sets with up to 18,446,744,073,709,551,616 (2^64) members.