Working with HyperLogLogs

1. Adding Elements

CommandReturns
PFADD key e [e ...]1 if internal register changed, else 0

2. Counting Unique Elements

CommandDescription
PFCOUNT keyApproximate cardinality
PFCOUNT k1 k2 ...Cardinality of union (temp merge)

3. Merging HyperLogLogs

CommandDescription
PFMERGE dst src1 src2 ...Union into dst (creates if missing)

Example: Monthly unique visitors

PFADD visits:2026-05-19 user1 user2
PFMERGE visits:2026-05 visits:2026-05-01 visits:2026-05-02
PFCOUNT visits:2026-05

4. Understanding Cardinality Estimation

PropertyValue
AlgorithmHyperLogLog with bias correction
Standard error0.81%
Memory per key~12 KB (fixed)

5. Handling Memory Efficiency

CardinalityHLL MemorySET Memory
10K12 KB~500 KB
1M12 KB~50 MB
100M12 KB~5 GB

6. Using for Unique Visitor Tracking

PatternKey Schema
Per-page uniquesuv:page:{slug}:{date}
Rolling 7-dayPFMERGE last 7 daily HLLs

7. Understanding Error Rate

True CountEstimated Range
1,000~992–1008
1,000,000~991,900–1,008,100

8. Comparing with Set-Based Counting

HyperLogLog

  • O(1) add and count
  • 12KB regardless of size
  • ~0.8% error
  • No listing of members

SET

  • Exact count
  • Memory grows linearly
  • Supports membership and listing
  • Best for < 100K unique items