Design a metrics/log ingestion system (like NewRelic/Splunk) with alerts and visualizations (focus on time/keyword-optimized storage)

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-11-08

Problem Statement

Modern distributed applications produce logs and metrics at high volume and velocity. Companies need a system like Splunk, NewRelic, or Datadog to:


•Ingest logs/metrics from diverse sources.
•Store them efficiently (optimized for time-based queries and keyword searches).
•Provide real-time alerts on anomalies.
•Enable dashboards & visualizations for observability.


You need to design such a system focusing on scalable ingestion, optimized storage, alerting, and querying.


✅ Functional Requirements


1.Log/Metrics Ingestion
•Collect logs from apps, servers, containers, APIs.
•Support multiple formats (JSON logs, plain text, syslog, Prometheus metrics).
•Handle high throughput (millions of events/sec).
2.Storage & Indexing
•Store data in a way optimized for:
•Time-based queries (last 5 mins, last 1 hr, last 7 days).
•Keyword/full-text search (error, userId=1234).
•Support data retention policies (hot vs cold storage).
3.Query & Visualization
•Provide a query API (e.g., search logs where level=ERROR in last 1 hr).
•Dashboards to visualize metrics (time-series charts, heatmaps, counters).
4.Alerting
•Allow users to define rules like:
•“Trigger alert if ERROR logs > 100 in 5 mins.”
•“CPU > 80% for 3 consecutive intervals.”
•Send alerts via email, Slack, PagerDuty, etc.
5.Scalability & Multi-Tenancy
•Support multiple teams/customers with isolated data.
•Scale horizontally across thousands of servers.


❌ Non-Functional Requirements


•Low Latency: <5s from log ingestion to searchable index.
•High Availability: System should tolerate node failures.
•Scalability: Ingest petabytes/day.
•Durability: No log loss, even during crashes.
•Query Efficiency: Must handle both time-series queries (metrics) and keyword search (logs).


🧱 System Components


1.Ingestion Layer
•Agents/collectors (like Fluentd, Logstash, Vector) running on hosts.
•Streams logs/metrics to Kafka (or Pulsar) for durability & buffering.
2.Processing & Enrichment
•Stream processors (Flink/Spark/Kafka Streams) for parsing, enrichment (add metadata: host, region, app).
•Apply filters to drop debug/noise logs.
3.Storage Layer
•Hot Storage: Optimized for search & real-time queries.
•Logs → Elasticsearch / OpenSearch / ClickHouse.
•Metrics → Time-series DB (Prometheus, InfluxDB, TimescaleDB).
•Cold Storage: Cheap, long-term storage (S3, HDFS).
4.Indexing
•Inverted index for log keywords (fast search).
•Columnar storage with time-partitioning for metrics (fast aggregations).
5.Query & Visualization Layer
•Query service providing APIs (SQL-like or custom DSL).
•UI dashboards for:
•Logs (search bar, filters, highlighting).
•Metrics (graphs, heatmaps, counters).
6.Alerting Engine
•Rule-based engine scans streams or queries periodically.
•Supports thresholds, anomaly detection, moving averages.
•Sends notifications to external channels.


🔄 Example Flow


1.Application generates logs (ERROR: DB connection failed).
2.Agent ships logs → Kafka.
3.Stream processor enriches (host=app1, region=us-east).
4.Data stored in Elasticsearch (logs) and Prometheus (metrics).
5.User runs query: search error logs in last 1 hr from region=us-east.
6.Alert rule triggers if >500 ERROR logs in 5 mins.
7.UI dashboard visualizes error rate spike.


⚖️ Trade-offs / Design Considerations


•Storage Cost vs Query Speed: Hot storage = fast but expensive; cold storage = cheap but slower.
•Indexing Strategy: Full indexing (fast search, expensive writes) vs partial indexing (cheaper, slower queries).
•Batch vs Streaming: Alerts on logs need near real-time streaming; analytics may tolerate batch jobs.
•Multi-Tenancy: Index per-tenant vs shared index with metadata-based isolation.
•Retention Policy: Keep logs in hot storage for 7 days, cold storage for 6 months.


🧪 Open Discussion Points


•How do we handle exploding cardinality (e.g., userId in logs with millions of values)?
•Should metrics & logs be stored in the same system (unified observability) or separate?
•Can we add ML-based anomaly detection (beyond static alert rules)?
•How do we optimize time-range queries (sharding by time vs hash)?


👉 This is a classic HLD interview problem testing knowledge of streaming pipelines, indexing, time-series databases, storage optimizations, and alerting.

Solution

Comments