How do you add items to a HyperLogLog and get an estimated count of unique items using Redis?
Short answer
You use PFADD to add one or more items to a HyperLogLog structure, and PFCOUNT to retrieve the current estimated number of unique items that have been added, with Redis automatically creating the HyperLogLog structure the first time you add an item to a new key.
Advertisement
AModel answer
You use PFADD to add one or more items to a HyperLogLog structure, and PFCOUNT to retrieve the current estimated number of unique items that have been added, with Redis automatically creating the HyperLogLog structure the first time you add an item to a new key.
Example
PFADD page_views:homepage 'visitor1' 'visitor2' 'visitor3' PFCOUNT page_views:homepage
Real-world example
A marketing analytics dashboard tracks the estimated number of unique visitors to each page on a website, adding visitor identifiers with PFADD and displaying the running estimate using PFCOUNT.
Likely follow-ups
- What data type does PFCOUNT actually return?
- Does adding the exact same item twice affect the count?
Related topics