Design a Metrics Monitoring and Alerting System
Understanding the Problem
📈 What is a metrics monitoring system? A metrics monitoring system collects numeric measurements from servers, containers and applications (CPU usage, request rates, error counts, latency percentiles), stores them as time series, and lets engineers graph them and define alerts on them. When a rule fires, it notifies the right person through a pager, chat or phone call. Datadog, Prometheus with Alertmanager, Amazon CloudWatch and Grafana Cloud are all versions of this.
Interviewers ask this under several names: "design Datadog," "design CloudWatch," "design a telemetry service," "design planet-scale metrics," "design synthetic monitoring" or "design PagerDuty's alert routing." They are all the same system seen from different ends. Telemetry and planet-scale metrics are the ingestion and storage half. Synthetic monitoring is a special kind of data source: instead of a service reporting on itself, we probe it from outside. Alert routing is the last hop, from "a rule fired" to "a human's phone rang." We'll cover all of them, with the storage engine itself kept at the level of a component; it gets its own breakdown in Design a Time Series Database.
Functional Requirements
Start by agreeing on what users can do. Monitoring platforms grow forever (logs, traces, APM, security), so draw the boundary early.
Core Requirements
- Services and hosts should be able to send metrics (counters, gauges, histograms) tagged with labels like
service,regionandhost. - Users should be able to query metrics and view dashboards over any time range, from the last 15 minutes to the last year.
- Users should be able to define alert rules on metrics and have notifications routed to the right on-call engineer, with escalation if nobody responds.
- Users should be able to set up synthetic checks: HTTP or TCP probes against their endpoints, run from several regions every minute.
Below the line (out of scope)
- Logs and traces. They're separate pipelines with different storage (see distributed logging and distributed tracing).
- Anomaly detection and forecasting with ML.
- Billing and the customer-facing admin console.
Non-Functional Requirements
Ask your interviewer for scale. We'll design for a large multi-tenant SaaS: 1 billion active time series, each reporting every 10 seconds, which is 100 million samples per second at steady state. There are 2 million alert rules, and a few thousand engineers looking at dashboards at any moment, more during incidents.
Core Requirements
- The system should ingest 100M samples per second with spikes of 2-3x (deploys and incidents produce bursts of new series and higher rates).
- Alerts should fire within 1 minute of the condition becoming true, and notifications should be delivered within seconds after that.
- Dashboard queries over the last few hours should return in under 1 second at p95; year-long queries in a few seconds.
- The alerting path should be more available than the systems it watches. A monitoring outage during an incident is the worst possible time to be blind.
- Raw data should be kept for 15 days and downsampled data for 13 months.
Below the line (out of scope)
- Exactly-once ingestion. Losing or duplicating a sample now and then is acceptable; metrics are statistical.
- Strong consistency between replicas. A dashboard can be a few seconds behind.
Some quick math. 100M samples per second at the roughly 1.5 bytes per sample that good time series compression achieves is 150 MB/s, about 13 TB per day. Fifteen days of raw data is around 200 TB, which is fine for object storage. The more interesting numbers are elsewhere. The recent data that dashboards and alerts read has to be in memory, and a busy in-memory time series costs on the order of a few kilobytes (its open chunk, label strings and index entries). A billion of them is several terabytes of RAM before replication. And 2 million alert rules evaluated every 30 seconds is about 67,000 queries per second that must hit fresh data.
The math tells you where the design goes. Storage volume is not the hard part; object storage absorbs it. What's hard is that every active series costs memory, which makes the number of series, not the number of samples, the thing to control. Expect your interviewer to push on cardinality.