Design Metrics Aggregation System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-04-13

Problem Statement

Designing a system that ingests, stores, and aggregates metrics from multiple sources (e.g., servers, microservices, IoT devices) for monitoring and analytics. Focus is on data structures, modular design, and efficient aggregation.


✅ Functional Requirements


1.Metrics Ingestion
•Ingest metrics in real-time from multiple sources.
•Each metric has name, value, timestamp, and optional tags (e.g., host, region).
2.Aggregation Operations
•Support basic aggregation functions: sum, count, average, min, max.
•Support aggregation over time intervals (e.g., per minute, per hour).
•Optional: support percentile or histogram-based aggregation.
3.Querying
•Query aggregated metrics for a given metric name and time range.
•Optional: query by tags or other dimensions.
4.Storage & Retention
•Store raw metrics for a configurable retention period.
•Aggregated metrics can be stored for longer-term analytics.
5.Concurrency & Scalability
•Handle concurrent ingestion from multiple sources.
•Efficient data structures to support high-frequency metrics.


🔐 Constraints & Assumptions


•Focus on LLD, data structure design, and aggregation logic, not distributed deployment.
•Metrics are numeric (integer or float).
•Single-machine or small-scale multi-threaded ingestion for LLD purposes.


🧱 Key Components & Classes


1.Metric Class
1 2class Metric { 3public: 4 string name; 5 double value; 6 long timestamp; 7 map<string, string> tags; 8};
1.Aggregator Class
•Maintains aggregated values for a metric over time intervals.
1 2class Aggregator { 3private: 4 double sum; 5 double minVal; 6 double maxVal; 7 int count; 8public: 9 void addValue(double value); 10 double getAverage(); 11 double getSum(); 12 double getMin(); 13 double getMax(); 14};
1.MetricsStore Class
•Stores metrics and performs aggregation.
1 2class MetricsStore { 3private: 4 map<string, vector<Metric>> metricsMap; // key: metric name 5 map<string, Aggregator> aggregatorMap; // aggregated values per metric 6 mutex storeMutex; 7public: 8 void ingest(Metric metric); 9 Aggregator query(string metricName, long startTime, long endTime); 10};


🔄 Example Workflow


1.Metric received from source → MetricsStore.ingest(metric) → update aggregator and optionally store raw metric.
2.Periodically or on-demand → query(metricName, startTime, endTime) returns aggregated values.
3.Aggregation example: for metric "CPU_Usage" over 1-minute interval → sum, count, average, min, max.


🧪 Expected Deliverables


•Class diagram showing Metric, Aggregator, and MetricsStore.
•Implementation of ingest, aggregate, and query methods.
•Test cases:
•Ingest multiple metrics concurrently
•Aggregate metrics correctly over time intervals
•Query aggregated metrics for a specific range
•Ensure thread-safe ingestion and aggregation


💡 Discussion Points / Trade-offs


1.In-Memory vs Persistent Storage
•In-memory: fast but volatile
•Persistent: slower but durable
2.Data Structure Choice
•Use maps + heaps or interval trees for fast aggregation and time-range queries.
3.Concurrency
•Use mutexes or concurrent data structures for thread-safe ingestion.
4.Extensibility
•Easy to add new aggregation functions, new dimensions/tags, or rolling window aggregation.


Solution

Comments