HyperLogLog

Difficulty: Advanced

Question

What is HyperLogLog in Redis, how accurate is it, and when would you use it instead of a Set?

Answer

HyperLogLog (HLL) is a probabilistic data structure that estimates the number of unique elements (the cardinality) in a set using a fixed, tiny amount of memory. In Redis each HLL key uses at most about 12 KB, whether you add a thousand or a billion items, with a standard error of 0.81%. The trade-off is that it only gives you a count; you cannot list the elements or check whether a specific one was added.

Commands

  • PFADD key element [element ...] adds items. Returns 1 if the internal registers changed, 0 otherwise.
  • PFCOUNT key [key ...] returns the estimated unique count. With several keys it estimates the size of their union without modifying them.
  • PFMERGE destkey key1 key2 ... stores the union of several HLLs in one key.
  • The PF prefix honours Philippe Flajolet, who co-invented the algorithm.

How it works, intuitively

  1. Each element is hashed to a 64-bit value.
  2. Some of the bits choose one of 16,384 registers.
  3. The remaining bits are examined for the position of the first 1 bit. A long run of leading zeros is rare, so seeing one suggests many distinct items have been seen.
  4. Each register keeps the maximum run length it has observed, using 6 bits. 16,384 registers times 6 bits is 12 KB.
  5. The count is estimated using a harmonic mean across registers, with corrections for small and large ranges.

Because adding the same item twice produces the same hash, duplicates never change the estimate. That is why HLL counts unique items, not total events.

Redis uses a sparse representation for small cardinalities, which takes far less than 12 KB, and switches to the dense 12 KB form as the count grows (controlled by hll-sparse-max-bytes, default 3000). An HLL is stored as a string, so GET returns its raw bytes.

HyperLogLog vs Set vs Bitmap

NeedSetBitmapHyperLogLog
Exact countYesYesNo, about 0.81% error
Memory for 100M uniquesGigabytes12.5 MB if IDs are denseAbout 12 KB
Check membershipYesYesNo
List membersYesHardNo
Works with string IDsYesNeeds integer IDsYes

Where HLL is a great fit

  • Unique visitors per page per day on a content site, using IP or device ID.
  • Unique search queries per hour.
  • Unique users who viewed a product, merged across days with PFMERGE for weekly numbers.
  • Any dashboard metric where 1% error is acceptable and exact numbers would cost too much memory.

When not to use it

  • Billing, payments or anything legal or financial: you need exact numbers.
  • When you need to know who, not just how many.
  • When cardinality is small (a few thousand); a Set is exact and cheap enough.

A common mistake is merging HLLs and then trying to compute an intersection by subtraction. Intersections via inclusion-exclusion amplify the error and can be very wrong when the overlap is small.

How PFADD updates a HyperLogLog. Each element updates at most one small register; memory never grows past about 12 KB.
  1. Element added: For example a device ID.
  2. 64-bit hash: Same element always gives the same hash.
  3. Pick 1 of 16,384 registers: Using part of the hash bits.
  4. Count leading zeros: Keep the max seen in that register.
  5. PFCOUNT estimates: Harmonic mean across all registers.

Code examples

Unique visitors per day and per week

# Track unique device IDs per day
redis-cli PFADD uv:home:2026-10-11 dev-a dev-b dev-c
redis-cli PFADD uv:home:2026-10-12 dev-b dev-c dev-d dev-b

redis-cli PFCOUNT uv:home:2026-10-12           # dev-b counted once

# Union estimate without storing anything
redis-cli PFCOUNT uv:home:2026-10-11 uv:home:2026-10-12

# Store a weekly rollup
redis-cli PFMERGE uv:home:2026-w41 uv:home:2026-10-11 uv:home:2026-10-12
redis-cli PFCOUNT uv:home:2026-w41

# Memory stays tiny
redis-cli MEMORY USAGE uv:home:2026-w41

Only the PFCOUNT and PFMERGE outputs are shown. For small sets the estimate is usually exact. Errors become visible at larger cardinalities but stay around 0.81% on average.

Key points

  • HLL estimates unique counts with a standard error of 0.81%.
  • Max memory is about 12 KB per key regardless of cardinality; small HLLs use a sparse form.
  • PFADD, PFCOUNT and PFMERGE are the only commands you need.
  • You cannot list members or test membership.
  • Union is cheap and accurate; intersection by subtraction is unreliable.

Common follow-up questions

  • Why does HLL use the number of leading zeros in a hash?
  • How would you compute unique visitors for the last 30 days efficiently?
  • When would a Bloom filter be a better choice than HyperLogLog?
  • Why can you not get an accurate intersection from two HLLs?

Interview tips

  • Quote the two numbers: about 12 KB and 0.81% standard error.
  • Explain the leading-zeros intuition briefly; interviewers like to see you understand why it works.
  • Always state when not to use it, such as billing or anything needing exact counts.

Concepts covered

HyperLogLog, Cardinality Estimation, PFADD and PFCOUNT, PFMERGE, Standard Error

More Redis & Caching questions