Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Redis HyperLogLog


May 16, 2021 Redis


Table of contents


Redis HyperLogLog

Redis added the HyperLogLog structure in version 2.8.9.

Redis HyperLogLog is an algorithm used to do base statistics, and the advantage of HyperLogLog is that when the number or volume of input elements is very, very large, the space required to calculate the base is always fixed and very small.

In Redis, each HyperLogLog key requires only 12 KB of memory to calculate the base of nearly two to sixty-four different elements. This is in sharp contrast to collections that consume more memory when calculating the base.

However, because HyperLogLog only calculates the base based on the input elements, not stores the input elements themselves, HyperLogLog cannot return the individual elements entered as a collection.


What is a base?

For example, if the dataset is s1, 3, 5, 7, 5, 7, 8, then the base set of this dataset is s1, 3, 5, 7, 8, and the base (not repeating elements) is 5. B ase estimation is the rapid calculation of the base within the acceptable margin of error.


The following example demonstrates how HyperLogLog works:

redis 127.0.0.1:6379> PFADD w3ckey "redis"

1) (integer) 1

redis 127.0.0.1:6379> PFADD w3ckey "mongodb"

1) (integer) 1

redis 127.0.0.1:6379> PFADD w3ckey "mysql"

1) (integer) 1

redis 127.0.0.1:6379> PFCOUNT w3ckey

(integer) 3

Redis HyperLogLog command

The following table lists the basic commands for redis HyperLogLog:

Serial number Commands and descriptions
1 PFADD key element [element ...]
Add the specified element to HyperLogLog.
2 PFCOUNT key [key ...]
Returns the base estimate for a given HyperLogLog.
3 PFMERGE destkey sourcekey [sourcekey ...]
Combine multiple HyperLogLogs into one HyperLogLog