Skip to main content

What is HyperLogLog?

HyperLogLog (HLL) in Redis is a probabilistic data structure for counting the number of unique elements (cardinality) with minimal memory use.

In simple terms

If you need to find out:

  • how many unique users visited a site,
  • how many unique IPs / events / IDs there were,
  • while not storing all the values,

HyperLogLog solves this problem very efficiently.

Key properties

PropertyValue
TypeA probabilistic algorithm
PurposeCounting unique values
Memory~12 KB regardless of the number of elements
Accuracy±0.81%
SpeedVery high
Stores the values themselves?No

Important: HLL doesn't return the elements themselves, only an approximate count.

How it works (intuitively)

  1. Elements get hashed
  2. The distribution of bits gets analyzed
  3. A mathematical model estimates the number of unique values

Redis stores a compact statistical representation, not a list of values.

Redis commands for HyperLogLog

PFADD, add elements

redis
PFADD users 1 2 3 4

PFCOUNT, get the unique count

redis
PFCOUNT users

PFMERGE, merge several HLLs

redis
PFMERGE all_users users_day1 users_day2

A real-world example

Unique users per day

redis
PFADD visitors:2025-12-22 user_123 PFADD visitors:2025-12-22 user_456 PFCOUNT visitors:2025-12-22

Even with 10 million users, memory usage stays at ~12 KB.

Comparison with other approaches

Set

redis
SADD users user1 user2 user3 SCARD users
SetHyperLogLog
ExactApproximate
Stores valuesNo
Memory growsMemory is fixed
Fits analyticsYes

When to use HyperLogLog

A great fit for:

  • DAU / MAU / WAU
  • traffic analytics
  • events, clicks, views
  • large systems (millions / billions of IDs)

Not a fit if:

  • 100% accuracy is required
  • you need to iterate elements
  • you need exists, remove

Frequently asked questions

How large is the error margin?

Around 0.81% For example:

  • the real value: 1,000,000
  • the result: ~992,000 - 1,008,000

Can the error margin be reduced?

No, in Redis, the size and accuracy are fixed by the implementation.

Can elements be removed?

No. Only adding is supported.

The short rule of thumb

Need a unique count + a lot of data + minimal memory → HyperLogLog

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.