Upstash Documentation

PFCOUNT

Get estimated cardinality.
2 min read

Use PFCOUNT to read the estimated number of distinct elements recorded in one or more HyperLogLogs.

With a single key the stored estimate is returned. With several keys the structures are merged on the fly and the cardinality of their union is returned, without modifying any of them, which is how you answer "how many unique users across these seven days" without double counting. The result is an approximation with a standard error of about 0.81%.

The multi-key form does real work on every call, so when the same union is read often it is cheaper to roll the sources up into one key with PFMERGE and count that.

Syntax#

Arguments#

ArgumentRequiredRepeatableDescription
<key>YesYesRedis key targeted by the command.

Response#

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

ProtocolReply
RESP2Integer
RESP3Integer
Note

Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.

Examples#

TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.

Redis CLI
@upstash/redis
upstash_redis
ioredis
node-redis
redis-py
go-redis
jedis
redis-rs