PFADD-&-PFCOUNT
Count unique items (cardinality) using only 12KB of memory - even for billions of items. The ultimate space-time trade-off for analytics.
You'll Learn
- What HyperLogLog is and why it exists
- PFADD, PFCOUNT, PFMERGE syntax
- Trade-off: memory vs accuracy
- Real-world use cases
See Your Data, Not Terminal Text
Redimo visualizes every Redis data type beautifully. Edit inline, undo mistakes, stay safe in production.
1. What Is HyperLogLog?
HyperLogLog (HLL) is a probabilistic data structure that estimates the count of unique elements. It uses only 12KB of memory regardless of cardinality - you can count billions of unique items in the same space as counting 100.
The trade-off: accuracy. HLL has a standard error of ~0.81%. If the true count is 1,000,000, HLL might report anywhere from 991,900 to 1,008,100. For analytics and monitoring, this is usually acceptable.
Syntax
PFADD key element [element ...] # Add elements (returns 1 if cardinality changed)
PFCOUNT key [key ...] # Get estimated unique count
PFMERGE destkey sourcekey [sourcekey ...] # Merge multiple HLLsHLL Properties
- • Fixed 12KB memory per key
- • ~0.81% standard error
- • O(1) add and count
- • Mergeable (union operation)
Perfect For
- • Unique visitor counting
- • Distinct search queries
- • Unique event tracking
- • IP address counting
2. The CLI Way (Hard Mode)
HyperLogLog in CLI looks simple, but the internal data is completely opaque - you can't see what's been added:
Basic HyperLogLog Operations
127.0.0.1:6379> PFADD unique:visitors:2024-12-25 "user:1001" "user:1002" "user:1003"
(integer) 1
127.0.0.1:6379> PFADD unique:visitors:2024-12-25 "user:1001" "user:1004"
(integer) 1
127.0.0.1:6379> PFCOUNT unique:visitors:2024-12-25
(integer) 4
127.0.0.1:6379> GET unique:visitors:2024-12-25
"HYLL\x01\x00\x00\x00\x00..." # Binary garbage!Merging Time Periods
127.0.0.1:6379> PFADD unique:visitors:2024-12-24 "user:1001" "user:2001" "user:3001"
(integer) 1
127.0.0.1:6379> PFADD unique:visitors:2024-12-25 "user:1001" "user:4001" "user:5001"
(integer) 1
127.0.0.1:6379> PFMERGE unique:visitors:week unique:visitors:2024-12-24 unique:visitors:2024-12-25
OK
127.0.0.1:6379> PFCOUNT unique:visitors:week
(integer) 5
# user:1001 counted only once (deduplication works across merge!)The Problem
3. The Redimo Way (Smart Mode)
Redimo recognizes HyperLogLog keys and displays the estimated count prominently. No confusion with the binary internal representation. The "String" type badge shows this is stored as a string, but PFCOUNT gives you the meaningful data.
What Redimo Does for HyperLogLog:
- ●Binary Display Handling: Instead of showing unreadable binary, Redimo shows the data as a String with length info.
- ●Type Recognition: HyperLogLog is stored as String type, and Redimo displays this correctly.
- ●No Confusion: You won't accidentally try to edit the binary data as text.
CLI Pain Points
- • GET returns binary garbage
- • Can't see individual elements
- • Only PFCOUNT gives useful info
- • Easy to confuse with strings
- • No indication it's HLL
Redimo Solutions
- • Clean binary data display
- • Shows String type correctly
- • No confusing binary editing
- • Data size visible
- • Run PFCOUNT via CLI panel
4. Memory Comparison
The magic of HyperLogLog is its constant memory usage. Here's how it compares to exact counting:
| Method | 100 Items | 1M Items | 1B Items | Accuracy |
|---|---|---|---|---|
| Set | ~5 KB | ~50 MB | ~50 GB | 100% exact |
| Sorted Set | ~5 KB | ~70 MB | ~70 GB | 100% exact |
| HyperLogLog | 12 KB | 12 KB | 12 KB | ~0.81% error |
When 0.81% Error Matters
Not acceptable: Financial transactions, exact inventory counts, compliance reporting
5. Common Patterns
Daily Unique Visitors
# Track unique visitors per day
PFADD uv:2024-12-25 "ip:1.2.3.4" "ip:5.6.7.8" "ip:9.10.11.12"
# Same IP visiting again doesn't increase count
PFADD uv:2024-12-25 "ip:1.2.3.4"
# Get daily count
PFCOUNT uv:2024-12-25Weekly/Monthly Rollups
# Daily unique visitors
PFADD uv:2024-12-23 "user:1" "user:2" "user:3"
PFADD uv:2024-12-24 "user:2" "user:4" "user:5"
PFADD uv:2024-12-25 "user:1" "user:5" "user:6"
# Weekly rollup - union of daily HLLs
PFMERGE uv:week:52 uv:2024-12-23 uv:2024-12-24 uv:2024-12-25
# Get weekly unique count (users counted only once!)
PFCOUNT uv:week:52
# Returns: 6 (not 9)Multiple Dimensions
# Track uniques by page AND by overall site
PFADD page:/home "user:1001" "user:1002"
PFADD page:/pricing "user:1001" "user:1003"
PFADD site:overall "user:1001" "user:1002" "user:1003"
# Or aggregate pages into site total
PFMERGE site:calculated page:/home page:/pricing
PFCOUNT site:calculated # 3 unique users6. Pro Tips
HLL Keys Are Just Strings
Practical Tips
# Add expiration to daily HLLs
PFADD uv:2024-12-25 "user:1001"
EXPIRE uv:2024-12-25 604800 # 7 days
# Check if element MIGHT have been added (not supported)
# HLL doesn't support membership queries!
# Use a Bloom filter if you need "might be in set" checks
# PFADD returns 1 if cardinality changed, 0 otherwise
PFADD hll "already-added" # Returns 0 (probably)
# Estimate error range
# If PFCOUNT returns 1000000:
# - True value is likely between 991,900 and 1,008,100
# - 99% confidence: 976,000 to 1,024,000Cannot List Elements
Redimo Bonus
Ready to Count?
HyperLogLog is a specialized tool - use it when you need it.
Download Redimo for better Redis/Valkey management.