Bird
Raised Fist0

HyperLogLog for Cardinality Estimation - Counting Unique Users Cheaply

HLDmediumInterview freq: medium
Choose your preparation mode4 modes available
🎯
HyperLogLog for Cardinality Estimation - Counting Unique Users Cheaply
45 min · senior
senior

You are building a cardinality estimation service for a startup with 50 million daily active users (DAU). The business requires near real-time unique user counts for various segments (e.g., daily active users per feature, unique visitors per region) with P99 latency under 200ms. Data must be encrypted at rest and in transit. The system should minimize storage and compute costs by leveraging probabilistic data structures. Offline batch processing is allowed but must not block real-time queries.

  • Support 50M DAU with peak ingestion of 1M unique user events per second
  • P99 query latency under 200ms for cardinality estimates
  • Data encrypted at rest and in transit (TLS + disk encryption)
  • Offline batch aggregation allowed but real-time queries must not be blocked
  • Memory and storage cost must be minimized using probabilistic data structures
📋
Guided Steps
1/6
Step 1
Choosing the Data Structure for Cardinality Estimation
Which data structure will you use to efficiently estimate unique user counts at scale, and why?
💡 Hint
Look for a probabilistic data structure with fixed memory footprint and mergeability.
Use HyperLogLog (HLL) for cardinality estimation because it provides fixed-size sketches (~12KB per counter), supports merge operations for distributed aggregation, and offers acceptable error rates (~1-2%) at scale.
Mid-level: Names HyperLogLog as the data structure. Senior: Explains why HLL is preferred over alternatives like Bloom filters (which only test membership) or exact sets (which are memory prohibitive at 50M DAU).
⚠️Common Wrong Answer
Using exact sets or hash sets for unique counting, which leads to unbounded memory growth and high storage costs at 50M DAU.
Step 2
Ingestion Pipeline Design for High Throughput
How will you ingest and process 1 million unique user events per second to update HLL counters in real-time?
💡 Hint
Consider a distributed streaming platform that supports partitioning and fault tolerance.
Use Apache Kafka as the ingestion backbone with user_id as the partition key to ensure events for the same user route to the same partition, enabling idempotent and ordered updates to HLL sketches.
Mid-level: Mentions Kafka for ingestion. Senior: Explains partition key choice (user_id) to avoid duplicate counting and enable efficient stateful processing downstream.
⚠️Common Wrong Answer
Using a single centralized queue or database for ingestion, which becomes a bottleneck and single point of failure at 1M events/sec.
Step 3
Real-Time Aggregation and Storage of HLL Sketches
Where and how will you store and aggregate HLL sketches to support low-latency queries and cost efficiency?
💡 Hint
Look for a scalable key-value store optimized for fast writes and merges with TTL support.
Use Apache Cassandra with segment_id and time window as the partition key to store serialized HLL sketches, leveraging Cassandra's LSM-tree writes for high throughput and efficient range queries for time windows.
Mid-level: Suggests Cassandra for storage. Senior: Explains partition key design to enable efficient merges and TTL-based expiration of old sketches to control storage costs.
⚠️Common Wrong Answer
Using relational databases for storing sketches, which do not scale for high write throughput and have expensive merge operations.
Step 4
Ensuring Data Security and Encryption
How will you ensure data is encrypted both at rest and in transit throughout the system?
💡 Hint
Consider TLS for network encryption and native encryption features of storage and streaming systems.
Enable TLS encryption for Kafka brokers and clients, use Cassandra's Transparent Data Encryption (TDE) for disk encryption, and enforce encryption in transit for all service-to-service communication.
Mid-level: Mentions TLS and disk encryption. Senior: Details how Kafka's SSL config and Cassandra TDE integrate with key management systems and how encryption impacts latency and throughput.
⚠️Common Wrong Answer
Ignoring encryption or relying on application-level encryption only, which risks data exposure and complicates operational management.
Step 5
API Design for Client Interaction
Design three core API endpoints for clients to submit events, query cardinality, and manage counters.
💡 Hint
Think about event ingestion, query parameters for segments/time windows, and administrative operations.
Define POST /events to ingest user events with user_id and segment_id; GET /cardinality to query estimated unique counts with segment_id and time window; DELETE /counters to reset or expire counters for a segment.
Mid-level: Provides basic REST endpoints. Senior: Explains request/response payloads, idempotency, pagination for queries, and security considerations like authentication and rate limiting.
⚠️Common Wrong Answer
Designing only ingestion or query endpoints without management APIs, leading to operational difficulties in resetting or expiring counters.
Step 6
Handling Offline Batch Processing Without Blocking Real-Time Queries
How will you incorporate offline batch aggregation to improve accuracy without impacting real-time query latency?
💡 Hint
Consider a layered architecture separating real-time and batch data paths with eventual consistency.
Use Apache Spark batch jobs to periodically merge HLL sketches from raw event logs stored in S3, then write aggregated sketches back to Cassandra in a separate namespace, while real-time queries read from the latest merged sketches asynchronously.
Mid-level: Mentions batch jobs for aggregation. Senior: Explains how to isolate batch writes to avoid locking Cassandra partitions and how to merge batch and real-time sketches for consistent query results.
⚠️Common Wrong Answer
Running batch jobs directly on the real-time database causing query latency spikes and availability issues.
🔌
API Design Challenge
Sketch 3 core API endpoints for this HyperLogLog cardinality estimation service.
  • How does the client submit user events for counting?
  • How does the client query cardinality estimates for a segment and time window?
  • How does the client manage or reset counters for specific segments?
📊
Scoring Rubric
▼ Show
Must Have - 60%
  • Use HyperLogLog for cardinality estimation
  • Use Apache Kafka for ingestion
  • Use Apache Cassandra for storage
  • Ensure encryption at rest and in transit
  • Provide REST API endpoints for ingestion, query, and management
Good to Have - 30%
    Bonus - 10%
      ✅
      Model Answer
      ▼ Reveal

      This system uses HyperLogLog sketches to estimate unique user counts efficiently at 50M DAU scale with low latency and strong security. Apache Kafka handles high-throughput event ingestion partitioned by user_id to ensure ordered, idempotent updates. Apache Cassandra stores serialized HLL sketches partitioned by segment_id and time window, supporting fast merges and TTL-based expiration. TLS and Cassandra TDE ensure encryption in transit and at rest. REST APIs allow clients to submit events, query cardinality, and manage counters. Offline batch aggregation with Apache Spark merges raw event logs from S3 into Cassandra without blocking real-time queries.

      • Use HyperLogLog due to fixed memory footprint (~12KB) and mergeability with ~1-2% error
      • Partition Kafka topics by user_id to ensure ordered, idempotent event processing
      • Store HLL sketches in Cassandra partitioned by segment_id and time window for efficient merges and TTL expiration
      • Enable TLS for Kafka brokers and clients and Cassandra TDE for encryption at rest
      • Design REST APIs with authentication, idempotency, and pagination for scalability and security
      • Use Apache Spark batch jobs to merge raw event logs from S3 into Cassandra asynchronously to avoid blocking real-time queries
      • HyperLogLog for fixed-size, mergeable cardinality sketches with ~1-2% error
      • Apache Kafka for high-throughput, partitioned event ingestion with user_id partition key
      • Apache Cassandra for scalable, low-latency storage of serialized HLL sketches with segment_id/time window partitioning
      • TLS encryption for Kafka and service communication plus Cassandra Transparent Data Encryption for data at rest
      • REST API endpoints: POST /events, GET /cardinality, DELETE /counters with authentication and rate limiting
      • Apache Spark batch jobs for offline aggregation of raw event logs stored in S3, writing merged sketches back to Cassandra
      • Partition key design in Kafka and Cassandra to ensure ordered, idempotent updates and efficient range queries
      • TTL and expiration policies in Cassandra to control storage costs and data freshness

      Practice

      (1/5)
      1. A global video streaming service needs to reduce latency for users worldwide by caching content close to them. Which CDN component primarily handles this latency reduction?
      easy
      A. Origin server that stores the master copy of all content
      B. DNS servers resolving domain names to IP addresses
      C. Load balancers distributing requests among origin servers
      D. Edge nodes located in Points of Presence (PoPs) near users

      Solution

      1. Step 1: Identify latency reduction mechanism

        Latency is reduced by caching content closer to users, which is done by edge nodes in PoPs.
      2. Step 2: Eliminate other components

        Origin servers hold master content but are far from users; load balancers and DNS do not cache content.
      3. Final Answer:

        Option D -> Option D
      4. Quick Check:

        Edge nodes cache content near users -> latency reduced [check]
      Hint: Edge nodes cache near users for low latency [check]
      Common Mistakes:
      2. Without the distributed metadata service, what exactly breaks in the blob store's object retrieval process?
      medium
      A. Object retrieval requests block indefinitely waiting for metadata resolution
      B. Clients receive errors because object locations and versions cannot be resolved
      C. Clients can still retrieve blobs but with stale metadata versions
      D. Blobs are retrieved but data integrity checks fail silently

      Solution

      1. Step 1: Trace retrieval dependency on metadata service

        The metadata service provides object location and version info; without it, retrieval cannot resolve where to fetch data.
      2. Step 2: Eliminate other options

        Stale metadata retrieval is impossible without metadata service; blocking indefinitely is unlikely as client errors occur; silent integrity failures require data access, which fails first.
      3. Final Answer:

        Option B -> Option B
      4. Quick Check:

        Metadata service absence causes unresolved object location errors [check]
      Hint: No metadata service -> no object location resolution -> retrieval errors [check]
      Common Mistakes:
      3. For a ChatGPT-like system requiring real-time token streaming to mobile clients with intermittent connectivity, which architecture best balances latency and client compatibility?
      medium
      A. Use gRPC streaming for continuous token delivery
      B. Use WebSocket connections with custom heartbeat and reconnection logic
      C. Use long polling over HTTP/1.1 to simulate streaming
      D. Use HTTP/2 server-sent events (SSE) for token streaming with automatic reconnection

      Solution

      1. Step 1: Analyze client compatibility and connectivity constraints

        Mobile clients often have intermittent connectivity and limited support for gRPC streaming.
      2. Step 2: Evaluate streaming options

        gRPC streaming (A) has limited mobile support; WebSockets (C) require complex reconnection; long polling (D) adds latency and overhead. SSE (B) supports automatic reconnection and is widely supported on mobile.
      3. Final Answer:

        Option D -> Option D
      4. Quick Check:

        HTTP/2 SSE balances latency and mobile client compatibility [check]
      Hint: SSE auto-reconnects, suits mobile better than gRPC or WebSocket [check]
      Common Mistakes:
      4. Without the DAU Estimator component, what exactly breaks in the system's capacity planning process?
      medium
      A. Bandwidth estimation fails because DAU directly affects total data transfer calculations
      B. The system cannot estimate peak QPS, causing under-provisioning of servers
      C. Storage estimation becomes inaccurate because DAU is a key multiplier for daily data volume
      D. User authentication fails because DAU tracks active sessions

      Solution

      1. Step 1: Identify DAU Estimator role

        DAU Estimator provides daily active user count critical for bandwidth calculations as bandwidth depends on total active users.
      2. Step 2: Eliminate other options

        The system cannot estimate peak QPS, causing under-provisioning of servers is incorrect because peak QPS depends on instantaneous requests, not just DAU; Storage estimation becomes inaccurate because DAU is a key multiplier for daily data volume is partially true but storage depends more on total objects, not DAU alone; User authentication fails because DAU tracks active sessions is unrelated to authentication.
      3. Final Answer:

        Option A -> Option A
      4. Quick Check:

        Bandwidth depends directly on DAU for total data transfer [check]
      Hint: Bandwidth depends directly on DAU count [check]
      Common Mistakes:
      5. At 10x scale, which component in a push-based newsfeed system is most likely to fail first and why?
      hard
      A. The fan-out service, because pushing to millions of followers causes write amplification bottleneck
      B. The database, because it cannot handle 10x writes per second
      C. The cache layer, because it runs out of memory storing all user feeds
      D. The web servers, because they cannot handle 10x concurrent HTTP connections

      Solution

      1. Step 1: Analyze database scaling

        Databases can be sharded and scaled horizontally; not usually the first bottleneck.
      2. Step 2: Analyze fan-out service scaling

        Push-based fan-out causes write amplification proportional to follower count; at 10x scale, this bottleneck grows superlinearly and saturates first.
      3. Step 3: Analyze cache and web servers

        Caches can be scaled with eviction and sharding; web servers scale with load balancers and stateless design.
      4. Final Answer:

        Option A -> Option A
      5. Quick Check:

        Fan-out write amplification causes first bottleneck at 10x scale [check]
      Hint: Fan-out write amplification hits first at 10x scale [check]
      Common Mistakes:
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard