Design a Metrics Monitoring & Alerting System (Datadog / Prometheus)
Collect, store, query and alert on hundreds of millions of time series — push vs pull collection, the time-series data model and cardinality, ingestion through Kafka, time-series storage with Gorilla compression, downsampling and retention tiers, the query engine, the alerting pipeline with deduplication and routing, and meta-monitoring.
1. Requirements
Functional
- Collect metrics (counters, gauges, histograms) from 100k hosts and many services.
- Query and graph with aggregation (sum by service, p99 latency over 5 min).
- Alerting rules with notifications (PagerDuty, Slack, email).
- Retention: 15 days raw, 1 year downsampled.
Non-functional
- Ingest ~10M samples/s; 500M active series.
- Dashboards load in < 1–2 s; alerts fire within ~1 minute.
- Highly available, especially alerting; tolerate late/out-of-order data.
2. Estimation
| Quantity | Value |
|---|---|
| Samples | 100k hosts × 1,000 series / 10 s scrape = 10M samples/s |
| Raw size | 16 B/sample → 160 MB/s; with Gorilla (~1.4 B) → ~14 MB/s ≈ 1.2 TB/day |
| 15 days raw | ~18 TB (+ replication) |
| Index | 500M series × ~200 B labels ≈ 100 GB (memory-hungry → shard) |
3. Data model
metric{label1="v1", label2="v2"} → [(t1, v1), (t2, v2), …]
http_server_requests_seconds_bucket{service="checkout", route="/pay", le="0.5"} counter
node_memory_available_bytes{host="ip-10-0-3-7"} gauge
Histograms are stored as bucket counters (or native/exponential histograms) so percentiles can be computed across instances — you cannot average percentiles.
4. High-level design
5. Deep dives
Gorilla compression
timestamps: 1000, 1010, 1020, 1030, 1041
deltas: 10, 10, 10, 11
delta-of-delta: 0, 0, 1 → '0' bit for each regular interval
values: 42.0, 42.0, 42.5 → XOR with previous: 0 → single '0' bit; small diff → few meaningful bits
Sharding and replication
Hash the series ID (labels) to ingesters; replicate ×3 (quorum writes, as in Cortex/Mimir). Queries for recent data hit ingesters; older data comes from blocks in object storage via store gateways, with a query frontend that splits long ranges and caches results.
Downsampling and retention
| Tier | Resolution | Retention |
|---|---|---|
| Raw | 10 s | 15 days |
| 5-minute rollups (min, max, sum, count) | 5 m | 90 days |
| 1-hour rollups | 1 h | 1–2 years |
Late and out-of-order data
Accept samples within a window (e.g., up to 1 hour late) into the head block; reject or route older samples to a backfill path.
Alert rule example
groups:
- name: checkout-slo
rules:
- alert: CheckoutHighErrorRate
expr: |
sum(rate(http_requests_total{service="checkout",status=~"5.."}[5m]))
/ sum(rate(http_requests_total{service="checkout"}[5m])) > 0.02
for: 5m
labels: { severity: page, team: payments }
annotations:
summary: "Checkout 5xx rate above 2% for 5 minutes"
runbook: "https://runbooks.example.com/checkout-errors"
6. Trade-offs
| Decision | Choice |
|---|---|
| Collection | agents scrape locally + push to pipeline (hybrid) |
| Buffer | Kafka for durability and smoothing spikes |
| Storage | custom TSDB blocks + object storage (Prometheus/Mimir style) vs ClickHouse/Druid |
| Accuracy vs cost | downsampling, cardinality limits |
| Alerting | separate HA pipeline; streaming for critical rules; watchdog alert |
See reliability engineering for what to alert on (SLO burn rates).
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.