Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

PFADD

Last updated
PFADD key [element [element ...]]
Group
HyperLogLog
Complexity
O(1) per element
Arity
-2 (at least 2 words, counting the command name)
Flags
write, fast
Keys
first 1, last 1, step 1
HotKV Cloud
All plans
On-Premise
no license needed

PFADD adds elements to the HyperLogLog at key, creating it if needed. A HyperLogLog estimates how many distinct elements it has seen, using a small, fixed amount of memory (at most about 12 KB) whatever the count, with a typical error below 1%. Use it to count unique visitors, search terms or events when an exact set would be too large.

Called with no elements, PFADD only creates an empty HyperLogLog if the key does not exist. HyperLogLogs are stored as strings, so TYPE reports string. The key keeps its time to live.

Case RESP2 RESP3
The estimate changed, or the key was created Integer 1 Integer 1
Nothing changed Integer 0 Integer 0

Errors:

  • WRONGTYPE Key is not a valid HyperLogLog string value. when the key holds a string that is not a HyperLogLog.
  • INVALIDOBJ Corrupted HLL object detected when the HyperLogLog data is damaged.
  • WRONGTYPE Operation against a key holding the wrong kind of value when the key holds another type.
127.0.0.1:6379> PFADD visitors alice bob carol
(integer) 1
127.0.0.1:6379> PFADD visitors alice
(integer) 0
127.0.0.1:6379> PFCOUNT visitors
(integer) 3

Same as Redis 7 and Valkey 8, and the stored format is the same: a HyperLogLog moved between HotKV and Redis with DUMP and RESTORE keeps working.

PFCOUNT, PFMERGE, PFDEBUG