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 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 Valkey 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.
HLLs in Valkey, while technically a different data structure, are
encoded as a String, so you can call GET
to serialize a
HLL, and SET
to deserialize it back to the server.
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:
PFADD
.PFADD
command, you can use the
PFCOUNT
command. If you need to merge two different HLLs,
the PFMERGE
command is available. Since HLLs provide
approximate counts of unique elements, the result of the merge will give
you an approximation of the number of unique elements across both source
HLLs.127.0.0.1:6379> PFADD bikes Hyperion Deimos Phoebe Quaoar
(integer) 1
127.0.0.1:6379> PFCOUNT bikes
(integer) 4
127.0.0.1:6379> PFADD commuter_bikes Salacia Mimas Quaoar
(integer) 1
127.0.0.1:6379> PFMERGE all_bikes bikes commuter_bikes
OK
127.0.0.1:6379> PFCOUNT all_bikes
(integer) 6
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.
Valkey 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:
Note: 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.
PFADD
adds an item to a HyperLogLog.PFCOUNT
returns an estimate of the number of items in
the set.PFMERGE
combines two or more HyperLogLogs into
one.See the complete list of HyperLogLog commands.
Writing (PFADD
) to and reading from
(PFCOUNT
) the HyperLogLog is done in constant time and
space. Merging HLLs is O(n), where n is the number of
sketches.
The HyperLogLog can estimate the cardinality of sets with up to 18,446,744,073,709,551,616 (2^64) members.