Advertisement
Intermediate Redis, HyperLogLog in Redis

How does the internal sparse and dense representation of a HyperLogLog in Redis affect its memory usage as the number of unique items grows?

Short answer

Redis initially stores a HyperLogLog using a compact sparse representation when the number of unique items is still small, automatically converting to a denser, slightly larger fixed size representation once the count of unique items grows beyond a certain threshold, balancing…

Save Redis mock interview
Advertisement
AModel answer
Redis initially stores a HyperLogLog using a compact sparse representation when the number of unique items is still small, automatically converting to a denser, slightly larger fixed size representation once the count of unique items grows beyond a certain threshold, balancing memory efficiency for smaller counts against the need for accuracy at larger scales.
Example

-- Small HyperLogLogs use a more compact sparse encoding -- automatically converting to dense encoding as they grow STRLEN unique_visitors:today -- shows current memory footprint

Real-world example
A team monitoring memory usage notices a HyperLogLog's storage size grows slightly as more unique visitors are added throughout the day, understanding this is the expected transition from the sparse to dense internal representation.
Likely follow-ups
  • At what point does Redis switch from sparse to dense representation?
  • Does this internal representation change affect how you interact with the HyperLogLog commands?
Related topics