The Redis PFCOUNT command returns the cardinality estimate of the given HyperLogLog.

Syntax

The basic syntax of the PFCOUNT command is as follows:

1
PFCOUNT key [key ...]

Available Since

Redis version 2.8.9 and later.

Time Complexity

  • O(1) when called with a single key.
  • O(N) when called with multiple keys, where N is the number of keys.

ACL Categories

@read, @hyperloglog, @slow

Return Value

An integer representing the cardinality of the given HyperLogLog.

If multiple HyperLogLogs are provided, returns the approximated cardinality of the union of the HyperLogLogs passed, by internally merging the HyperLogLogs stored at the provided keys into a temporary HyperLogLog.

Note: as a side effect of calling this function, it is possible that the HyperLogLog is modified, since the last 8 bytes encode the latest computed cardinality for caching purposes. So PFCOUNT is technically a write command.

Example

The following example demonstrates the usage of the PFCOUNT command:

1
2
3
4
5
6
7
8
9
10
11
127.0.0.1:6379> PFADD hll_key_1 redis mysql hbase
(integer) 1
127.0.0.1:6379> PFCOUNT hll_key_1
(integer) 3
127.0.0.1:6379> PFADD hll_key_2 java go python
(integer) 1
127.0.0.1:6379> PFCOUNT hll_key_2
(integer) 3
127.0.0.1:6379> PFCOUNT hll_key_1 hll_key_2
(integer) 6
127.0.0.1:6379>

Here’s a breakdown of each command:

  • PFADD hll_key_1 redis mysql hbase

This command adds the three values (redis, mysql, and hbase) to the HyperLogLog with the key hll_key_1. It returns 1, indicating that the operation successfully added one or more unique elements to the HyperLogLog.

  • PFCOUNT hll_key_1

This command returns the approximate number of unique elements in the HyperLogLog with the key hll_key_1. It returns 3, indicating that hll_key_1 contains approximately 3 unique elements.

  • PFADD hll_key_2 java go python

This command adds the three values (java, go, and python) to the HyperLogLog with the key hll_key_2. It returns 1, indicating that the operation successfully added one or more unique elements to the HyperLogLog.

  • PFCOUNT hll_key_2

This command returns the approximate number of unique elements in the HyperLogLog with the key hll_key_2. It returns 3, indicating that hll_key_2 contains approximately 3 unique elements.

  • PFCOUNT hll_key_1 hll_key_2

PFCOUNT can also take multiple keys and return the approximate number of unique elements across all the HyperLogLogs combined. It returns 6, indicating that when counting across multiple HyperLogLogs, Redis calculates the approximate union of the unique elements in all specified keys.

Performances

Users should take in mind that single-key and multiple-keys executions of PFCOUNT command are semantically different and have different performances.

The PFCOUNT command performs efficiently with a single key due to caching, which helps avoid recalculating the cardinality frequently. This allows for hundreds of operations per second. However, when called with multiple keys, PFCOUNT requires an on-the-fly merge of the HyperLogLogs, which is slower and does not allow for caching of the union’s cardinality. As a result, the command may take longer (on the order of milliseconds) with multiple keys, and should not be overused.