System DesignExpertarticlePart 1 of 2 in Performance & Optimization Mastery

HyperLogLog at Scale: Billion-Cardinality Estimation

How does Redis track unique visitors in 12KB? The mathematics behind HyperLogLog and its use in real-time streaming analytics.

Sachin SarawgiApril 20, 20262 min read2 minute lesson

HyperLogLog at Scale

Counting unique users (cardinality) for billions of events is a classic trade-off problem. Do you want 100% accuracy (massive memory) or high speed with 99% accuracy (tiny memory)?

1. The Cardinality Problem

To track unique users, you'd usually use a . But storing 1 billion unique in a would take gigabytes of RAM.

2. The Solution: Probabilistic Counting

HyperLogLog (HLL) estimates cardinality using the properties of random binary strings. It counts the number of leading zeros in the hash of every input.

3. Real-world Efficiency

Redis's implementation of HLL uses a maximum of 12KB of memory to estimate cardinality for billions of unique items with an error rate of ~0.81%.

4. Why leading zeros work

If hashes are uniformly distributed, seeing a hash with many leading zeros is rare.
The maximum observed zero-prefix length correlates with set cardinality.
HLL improves accuracy by splitting observations across many registers and combining them with harmonic-mean style estimation.

5. Operational use cases

HLL is ideal for:

  • daily/monthly active users
  • unique visitors by campaign
  • unique IP estimation for abuse detection
  • approximate unique keys in streaming pipelines

It is not ideal when exact billing/audit correctness is required.

6. Mergeability advantage

A major benefit is composability:

  • compute HLL sketches per shard/region/window
  • merge sketches centrally
  • estimate global cardinality without raw event transfer

This is powerful for large analytics systems where raw data movement is expensive.

7. Accuracy expectations

Typical Redis HLL error is ~0.81% with fixed memory footprint.
For many product analytics questions, this is a strong trade-off:

  • tiny memory
  • stable throughput
  • bounded relative error

For compliance-grade numbers, use exact dedupe pipelines instead.

8. Common pitfalls

  • low-quality hash function causing bias
  • using HLL for tiny cardinalities where exact set is cheap
  • mixing incompatible sketch formats
  • treating estimate as exact count in financial reporting

Always document "approximate" in downstream dashboards.

9. Design pattern in streaming stacks

Practical pattern:

  1. event consumers hash entity IDs
  2. update per-tenant/per-window HLL sketch
  3. periodically snapshot sketch to object storage
  4. merge for global views in batch layer

This supports near-real-time unique metrics at low infrastructure cost.

Practical engineering notes

Get the next backend guide in your inbox

One useful note when a new deep dive is published: system design tradeoffs, Java production lessons, Kafka debugging, database patterns, and AI infrastructure.

No spam. Just practical notes you can use at work.

Sachin Sarawgi

Written by

Sachin Sarawgi

Engineering Manager and backend engineer with 10+ years building distributed systems across fintech, enterprise SaaS, and startups. CodeSprintPro is where I write practical guides on system design, Java, Kafka, databases, AI infrastructure, and production reliability.

Continue Series

Performance & Optimization Mastery

Lesson 1 of 2 in this learning sequence.

Next in series

Keep Learning

Move through the archive without losing the thread.

Related Articles

More deep dives chosen from shared tags, category overlap, and reading difficulty.

System DesignExpert

Bypassing the Kernel: User-Space Networking for Sub-Microsecond Performance

Bypassing the Kernel For high-frequency trading (HFT) and ultra-low-latency messaging, even the Linux kernel's networking stack is too slow. 1. The Context Switch Cost Every time a packet moves from the NIC (Network Inte…

Apr 20, 20263 min read
Deep DivePerformance & Optimization Mastery
#networking#dpdk#linux
System DesignAdvanced

API Rate Limiting at Scale: Redis-Based Strategies

API Rate Limiting at Scale with Redis Rate limiting is essential for protecting your APIs from abuse, ensuring fair usage, and preventing cascading failures. Redis is the ideal store for rate limiting because of its spee…

Apr 20, 20262 min read
Deep Dive
#redis#api-gateway#rate-limiting
System DesignAdvanced

Distributed Data Observability: Metrics That Actually Matter

Distributed Data Observability: High-Signal Metrics In a distributed data system, CPU and RAM are "low-signal" metrics. A system can have 10% CPU usage and still be completely stalled. To truly understand the health of y…

Apr 20, 20262 min read
Deep Dive
#observability#monitoring#distributed-systems
System DesignAdvanced

System Design: Designing an Ad Click Aggregator

System Design: Designing an Ad Click Aggregator Ad click aggregation is a massive scale data problem. When billions of users click on ads across the web, those clicks must be aggregated, deduplicated, and stored for both…

Apr 20, 20263 min read
Deep Dive
#system-design#ad-aggregator#analytics

More in System Design

Category-based suggestions if you want to stay in the same domain.