Working with HyperLogLogs
1. Adding Elements
| Command | Returns |
|---|---|
PFADD key e [e ...] | 1 if internal register changed, else 0 |
2. Counting Unique Elements
| Command | Description |
|---|---|
PFCOUNT key | Approximate cardinality |
PFCOUNT k1 k2 ... | Cardinality of union (temp merge) |
3. Merging HyperLogLogs
| Command | Description |
|---|---|
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
| Property | Value |
|---|---|
| Algorithm | HyperLogLog with bias correction |
| Standard error | 0.81% |
| Memory per key | ~12 KB (fixed) |
5. Handling Memory Efficiency
| Cardinality | HLL Memory | SET Memory |
|---|---|---|
| 10K | 12 KB | ~500 KB |
| 1M | 12 KB | ~50 MB |
| 100M | 12 KB | ~5 GB |
6. Using for Unique Visitor Tracking
| Pattern | Key Schema |
|---|---|
| Per-page uniques | uv:page:{slug}:{date} |
| Rolling 7-day | PFMERGE last 7 daily HLLs |
7. Understanding Error Rate
| True Count | Estimated Range |
|---|---|
| 1,000 | ~992–1008 |
| 1,000,000 | ~991,900–1,008,100 |