English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Redis HyperLogLog

Redis in 2.8.9 version added the HyperLogLog structure.

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

In Redis, each HyperLogLog key only needs to spend 12 KB of memory, and can calculate a number close to 2^64 base of different elements Numbers. This is in sharp contrast to the set formed when the number of elements increases, causing more memory consumption in the calculation of the base.

However, because HyperLogLog only calculates the cardinality based on input elements and does not store the input elements themselves, so HyperLogLog cannot return individual elements like a set.

What is Cardinality?

For example, the dataset {1, 3, 5, 7, 5, 7, 8}, Then the cardinality set of this dataset is {1, 3, 5 ,7, 8}, Cardinality (unique elements) is5. The cardinality estimate is to quickly calculate the cardinality within an acceptable error range.

Online Examples

The following examples demonstrate the working process of HyperLogLog:

redis 127.0.0.1:6379>  PFADD  w3codeboxkey  "redis"
1)  (integer) 1
redis 127.0.0.1:6379>  PFADD  w3codeboxkey  "mongodb"
1)  (integer) 1
redis 127.0.0.1:6379>  PFADD  w3codeboxkey  "mysql"
1)  (integer) 1
redis 127.0.0.1:6379>  PFCOUNT  w3codeboxkey
(integer) 3

Redis HyperLogLog Commands

The following table lists the basic commands of redis HyperLogLog:

Serial NumberCommand and Description
1PFADD key element [element ...]
Add specified elements to the HyperLogLog.
2PFCOUNT key [key ...]
Return the cardinality estimate value of the given HyperLogLog.
3PFMERGE destkey sourcekey [sourcekey ...]
Merge multiple HyperLogLog into one HyperLogLog