Advertisement
Intermediate Redis, HyperLogLog in Redis

How do you merge multiple HyperLogLog structures together to get a combined unique count across them?

Short answer

You use the PFMERGE command, specifying a destination key and the source HyperLogLog keys you want to combine, which creates a new HyperLogLog representing the union of all unique items across the merged structures, letting you calculate combined unique counts, such as total…

Save Redis mock interview
Advertisement
AModel answer
You use the PFMERGE command, specifying a destination key and the source HyperLogLog keys you want to combine, which creates a new HyperLogLog representing the union of all unique items across the merged structures, letting you calculate combined unique counts, such as total unique visitors across several different days.
Example

PFMERGE weekly_unique_visitors day1_visitors day2_visitors day3_visitors PFCOUNT weekly_unique_visitors

Real-world example
An analytics system merges seven daily HyperLogLog structures together at the end of each week using PFMERGE, calculating an estimated total of unique visitors across the entire week without needing to reprocess all the original raw data.
Likely follow-ups
  • Does merging HyperLogLogs preserve accuracy as well as counting unique items directly?
  • Can you merge a HyperLogLog with itself without causing any issues?
Related topics