Designing a Metrics Monitoring System (Datadog / Prometheus)
Difficulty: Advanced Topics: Time-Series DB, Aggregation Pipeline, Anomaly Detection, Alerting, Dashboard Queries Asked at: Google, Amazon, Microsoft, Uber, PhonePe, Flipkart, Netflix · Prerequisites: Message Queues, Database Indexing, and Caching
1. Understanding the Problem
A metrics monitoring system ingests millions of time-stamped data points per second from thousands of services and infrastructure components (CPU usage, request latency, error rates, business key performance indicators or KPIs), stores them efficiently for weeks to years, lets engineers query and visualize them on dashboards, and fires alerts when something goes wrong. The hard parts: ingesting at massive write throughput without dropping data, querying across billions of data points in sub-second for dashboards, and detecting anomalies in real-time across thousands of metric streams.
Real examples: Datadog, Prometheus + Grafana, New Relic, Amazon CloudWatch, InfluxDB.
2. Naive First Cut
flowchart LR
APP["Application"]
API["API Server"]
DB[("SQL DB<br/>metrics table")]
DASH["Dashboard"]
APP -->|"INSERT INTO metrics..."| API
API --> DB
DASH -->|"SELECT AVG... GROUP BY minute"| DB
Applications write metrics directly to a SQL table. Dashboards query with GROUP BY for aggregation.
Why this breaks:
- At 1M data points/second, INSERT throughput exceeds any single SQL DB capacity
- GROUP BY across billions of rows for a dashboard panel takes minutes, not milliseconds
- Row-based storage wastes space for time-series (timestamps are sequential, values compress well)
- No pre-aggregation means every dashboard refresh re-scans raw data
- No alerting - someone has to stare at the dashboard to notice problems
- Retention of raw data for months costs prohibitively much without downsampling
The rest of the doc evolves this into a purpose-built time-series ingestion pipeline with pre-aggregation, tiered storage, and real-time alert evaluation.
3. Prior Art We’re Drawing From
- Facebook Gorilla (in-memory time-series database, or TSDB) - Uses delta-of-delta encoding for timestamps and XOR encoding for floating-point values to achieve 12x compression. Keeps last 26 hours in memory for low-latency reads. This encoding scheme is now used in Prometheus TSDB and VictoriaMetrics. (Facebook Engineering)
💡 A time-series database (TSDB) is a database specialised for “one number, stamped with a time, over and over.” It gives up general-purpose querying in exchange for writing those points cheaply and scanning a time range fast, which is the only access pattern this system has. - Uber M3 (Metrics Platform) - Processes 500M+ metrics/sec. Uses a tiered architecture: M3 Aggregator pre-aggregates at the edge before writing to M3DB (distributed TSDB). Edge aggregation reduces write volume by 10-100x. (Uber Engineering)
- Prometheus Pull Model - Instead of applications pushing metrics, a central Prometheus server pulls (scrapes) metrics from service endpoints every 15-30s. Simplifies service code but limits scale to what one server can scrape. Thanos and Cortex add horizontal scaling on top. (Prometheus Docs)
- Datadog Alerting Pipeline - Evaluates millions of alert rules per minute using a streaming evaluation engine. Each rule is a stateful computation over a metric stream (e.g., “if avg(latency) > 500ms for 5 minutes, fire”). Rules are sharded across workers by metric name. (Datadog Engineering)
4. Functional Requirements
Core (Top 3)
- Ingest metrics at scale - accept millions of data points per second from services, infra, and custom metrics without dropping data
- Query and visualize - sub-second dashboard queries across time ranges (5 min to 6 months) with aggregation (avg, p99, sum, rate)
💡 p99 is the value 99% of measurements come in under - the slow tail. Engineers watch it instead of the average because an average of 50ms hides the one request in a hundred that took four seconds. - Alert on anomalies - evaluate threshold and anomaly-detection rules in real-time and notify on-call engineers within 60 seconds of a problem
Below the Line
- Custom metric tags and dimensions
- Dashboard sharing and annotations
- SLO tracking (error budget burn rate)
- Log correlation (link metrics to related logs)
- Capacity forecasting
5. Non-Functional Requirements
Core
- Write throughput: 1M+ data points/second sustained
- Query latency: P95 < 500ms for dashboard panel queries (1-hour range)
- Alert latency: Problem detected and notification sent within 60 seconds
- Retention: Raw data 15 days, 1-minute rollups 6 months, 1-hour rollups 2+ years
Discussion
Newest first