System design case study C25 · TSDB · Alerting

Design a Metrics and Alerting System

Every system in this book needs dashboards and alerts. Here we build the thing that watches everything else: millions of time series, written every few seconds, queried by people at 3 AM when it matters most.

What you'll be able to explain
  • Model metrics as time series with labels, and reason about cardinality
  • Design high-throughput ingestion and compressed time-series storage
  • Serve fast range queries with downsampling and tiered retention
  • Evaluate alert rules reliably and route notifications without spam
  • Keep the monitoring system up when the systems it watches are down

Scope

Functional

  • Services and hosts push (or expose for scraping) numeric metrics: counters, gauges and histograms with labels (service, region, status).
  • Query: "p99 latency of checkout by region over the last 6 hours", graphed on dashboards.
  • Alert rules: "error rate > 2% for 5 minutes" → notify on-call via push, SMS or chat, with de-duplication and silencing.
  • Retention: raw data for 15 days, downsampled data for 13 months.

Non-functional

  • Ingest 10M samples/s from 100,000 hosts.
  • Dashboard queries over the last hours return in < 1 s.
  • Alerting must keep working during large incidents, which are exactly when load spikes.

Out of scope: logs and traces (separate pipelines), anomaly detection with ML.

Size

  • 10M samples/s × 86,400 ≈ 864 billion samples/day.
  • Raw: 16 B/sample (8 B timestamp + 8 B value) ≈ 14 TB/day. With time-series compression (~1.3–2 B/sample), ~1.5 TB/day; 15 days ≈ 23 TB.
  • Active series: e.g. 100,000 hosts × 500 series ≈ 50M active series, each needing an in-memory index entry and a head chunk.
  • Queries: a few thousand dashboards auto-refreshing → ~2,000 queries/s, many over the same recent data.

Decision: an append-only, compressed, time-partitioned store sharded by series, with an inverted index on labels and aggressive caching of recent data.

The data model

A series is a metric name plus a set of labels; each series is a stream of (timestamp, value) samples.

http_requests_total{service="checkout", region="mumbai", status="500"}
  → (10:00:00, 18231) (10:00:10, 18240) (10:00:20, 18262) …
Type Example Query pattern
Counter Requests served (only goes up) Rate per second over a window
Gauge Memory in use, queue depth Current value, average, max
Histogram Request latency in buckets Percentiles (p50, p99) computed from buckets

Cardinality explosion. Each unique combination of label values is a new series. Adding a user_id or order_id label turns 50 series into 50 million, blowing up memory and the index. Labels must have bounded values (service, region, status code). Per-user detail belongs in logs or traces, not metrics.

Architecture

CLIENTHosts + servicesagents / exportersNETWORKCollectorsscrape or receiveSERVICEDistributorshash series → ingesterWORKERIngestersrecent data in memory + WALOBJECT STOREObject storagecompressed 2 h blocksWORKERCompactormerge + downsampleSERVICEQuery servicefan-out + cacheCLIENTDashboardsWORKERRule evaluatorSERVICEAlert managerdedupe, routereplicate ×3flush blockslast 2 holderfiring alerts
Metrics system: collectors → distributor → ingesters (in-memory head) → object-storage blocks; queriers read both.

The 30-second summary

Say it in the interview

"Metrics are series identified by name plus bounded labels. Collectors send samples to distributors, which hash each series onto a ring of ingesters with 3× replication and enforce per-tenant limits. Ingesters keep two hours in memory with a WAL, then flush compressed, indexed blocks to object storage, where a compactor merges and downsamples them for 13-month retention. Queries fan out across ingesters and blocks with result caching and strict limits. Alert rules run in replicated evaluators feeding an alert manager that de-duplicates, groups, inhibits and routes. The whole stack runs on separate infrastructure with a dead man's switch."

Follow-up questions the interviewer may ask

  1. How do you compute a p99 across 200 hosts?
  2. A team adds a high-cardinality label. What happens?
  3. Push or pull?

Model answers to each of these are in the book.