Design Heavy Hitters / Top K Exceptions system from logs

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2022-01-25

Problem Statement

Designing a Heavy Hitters (Top-K) detection system that processes logs (e.g., exceptions, errors, API failures) in real time. The system should identify the most frequent exceptions within a given time window (e.g., last 1 min, 1 hr, 1 day) and make this data available for dashboards, alerts, and root cause analysis.


Functional Requirements


1.Log Ingestion
•Ingest application logs (exceptions, stack traces, error codes) from distributed services in real time.
•Support structured + unstructured logs.
2.Exception Classification
•Normalize and classify exceptions (group similar stack traces, error codes, or messages).
•Handle high cardinality logs (millions of unique error messages).
3.Top-K Detection
•Find the Top-K most frequent exceptions in sliding / tumbling windows.
•Support approximate algorithms (Count-Min Sketch, Misra-Gries) for memory efficiency.
•Support exact computation for small datasets.
4.Storage & Querying
•Maintain aggregated counts of exceptions per time bucket.
•Store results for historical trend analysis (e.g., exception X was top-1 in last 24h).
•Allow querying across services, regions, tenants.
5.Alerting & Visualization
•Trigger alerts when a specific exception enters the Top-K.
•Provide dashboards to show trends, heavy hitters, and exception drill-downs.


Non-Functional Requirements


•Scalability: Must handle logs at millions of events per second.
•Low Latency: Top-K results should be computed in near real time (< few seconds delay).
•Accuracy: Approximate results acceptable within a defined error bound (ε, δ).
•Fault Tolerance: Logs should not be lost during network/cluster failures.
•Cost Efficiency: Optimize storage by aggregating instead of storing raw logs indefinitely.


Core Components


•Log Shippers (Fluentd/Filebeat/Agents) → Forward logs from app servers.
•Message Queue / Stream (Kafka/Kinesis/Pulsar) → Durable log pipeline.
•Stream Processing Layer (Flink/Spark Streaming/Kafka Streams)
•Normalize & classify logs.
•Apply Count-Min Sketch + Heap (for approximate Top-K).
•Maintain rolling aggregates per time window.
•Hot Storage (Redis, Cassandra, DynamoDB) → Store aggregated counts for fast queries.
•Cold Storage (S3, HDFS, BigQuery) → Persist historical logs for deep analysis.
•Query Service / API → Expose Top-K exceptions via REST/GraphQL.
•Dashboard & Alerts (Grafana, Kibana, custom UI) → Display results and trigger alerts.


Example Flow


1.Application generates logs → shipped via Fluentd → Kafka.
2.Stream processor consumes Kafka, extracts exception signatures, updates Count-Min Sketch + Heap.
3.Periodic Top-K snapshots written to Redis + persisted to cold storage.
4.Query Service fetches results for dashboards.
5.Alerts triggered if new exception dominates the Top-K list.


Key Trade-offs to Discuss


•Approximate vs Exact Counting: Count-Min Sketch saves memory but may overcount.
•Windowing Strategy: Sliding window (more accurate) vs tumbling window (cheaper).
•Exception Grouping: Hashing stack traces vs ML-based clustering for better normalization.
•Storage Cost: Retaining raw logs vs only storing aggregates.


⚡ This problem blends log ingestion (Kafka), real-time Top-K computation (Sketch + Heap), and alerting/analytics — very relevant for system design + streaming data interviews.

Solution

Comments