Course contentsShow
← Databases & SQL
Lesson 1291 of 1,95312. Key-Value and Caching EnginesFree lesson

HyperLogLog for Cardinality Estimation

Calculate unique user counts across massive datasets with negligible memory usage.

In this lesson, you will learn how to estimate the number of unique items in massive datasets using a probabilistic algorithm called HyperLogLog (HLL) without consuming gigabytes of memory.

The Problem: Counting Unique Users

Imagine you are building a dashboard for a website that tracks millions of daily active users. If you use a standard SQL COUNT(DISTINCT user_id) or a Redis SET, you store every unique ID. If you have 100 million unique users, you’ll consume gigabytes of RAM just to keep those IDs in memory to ensure you don’t count the same user twice. As your scale grows, this becomes prohibitively expensive.

The HyperLogLog Solution

HyperLogLog doesn't store the actual IDs. Instead, it uses a clever trick based on probability. It hashes each incoming user_id into a long binary string and looks at the "leading zeros."

Think of it like coin flipping: if you flip a coin and see a sequence of heads, the longer that sequence is, the rarer the event. If you see a sequence of 10 heads in a row, you know you’ve likely flipped that coin hundreds of times. HLL observes the maximum number of leading zeros in the hashes it sees; from that length, it mathematically estimates the total number of unique elements that must have passed through the system to produce such a pattern.

How it looks in practice

Using a structure like Redis, you don't add the user to a set; you "add" them to an HLL sketch:

# Adding users to a HyperLogLog register
PFADD daily_uniques "user_123"
PFADD daily_uniques "user_456"
PFADD daily_uniques "user_123" # Duplicate, ignored

# Get the estimated count
PFCOUNT daily_uniques
# Result: 2

The beauty of HLL is that it uses a fixed amount of memory (usually around 12KB) to estimate cardinalities of billions of items with a standard error of less than 1%. You trade a tiny, predictable amount of inaccuracy for massive gains in performance and memory efficiency. You aren't storing the data; you are storing a "fingerprint" of the data's distribution.

Key Takeaway: HyperLogLog provides a memory-efficient way to estimate unique counts by observing hash patterns rather than storing the actual values, allowing you to track millions of items using only a few kilobytes of space.