Skip to content

Redis HyperlogLog

HyperLogLog, a probabilistic data structure, is designed for efficiently estimating the cardinality of sets. This technique prioritizes memory optimization over perfect accuracy, making it ideal for scenarios where conserving memory is crucial.

A practical application of HyperLogLog is in web analytics, where accurately counting unique visitors to a website is essential for measuring engagement. Instead of storing individual visit records, HyperLogLog allows for estimating the cardinality of the set of visitors with minimal memory usage. For example, in Redis, which implements HyperLogLog, the standard error of estimation is typically less than 1%, ensuring reliable results. This approach eliminates the need for memory allocation proportional to the size of the set, offering consistent memory usage regardless of the dataset’s size.

In practice, HyperLogLog reduces memory overhead significantly, requiring only a fixed amount of memory, such as 12 kilobytes in the worst-case scenario, or even less for smaller sets.While you don’t really add items into an HyperLogLog, because the data structure only contains a state that does not include actual elements.


1.Create and Add Elements to the HyperLogLog

You can add elements to the HLL using the PFADD command. This command adds one or more elements to the HLL.

2.Estimate Cardinality

You can estimate the cardinality (count of unique elements) of the HLL using the PFCOUNT command.

3.Merge HyperLogLogs

You can merge multiple HLL structures into a single one using the PFMERGE command. This is useful for combining counts from different sources.

Example Code

# create an empty HLL and check its cardinality> PFADD deliverHLL 
(integer) 1> PFCOUNT deliverHLL
(integer) 0

# create an HLL containing three items> PFADD userHLL mary mike james
(integer) 1> PFCOUNT userHLL
(integer) 3

# merge the two HLLs to one> PFMERGE userHLL deliverHLL