Design a Metrics Monitoring System (Datadog)
hard 50 minCollect five million numbers a second from 100,000 hosts, keep them for a year, draw dashboards quickly and wake somebody when it matters.
Video lesson · Chapter 1 of 5
A thousand counters arrive as one point
How a metric becomes an alert
1:30Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
A metrics monitoring system collects numbers over time from machines and programs, stores them, draws them and raises alerts. Each stream of numbers is a time series: a name, some labels, and pairs of time and value.
It is a service like Datadog, or like Prometheus with a long-term store and Grafana in front. This is an exercise. It does not describe how any of those companies build theirs.
Why it exists
A person cannot watch 100,000 hosts. Numbers are small, regular and cheap to compare, so a program can watch all of them and call a person only when something is wrong.
The data has an unusual shape: it is appended, hardly ever changed, read by time range, and mostly recent. A store built for that shape needs about a tenth of the space of a plain table of times and values.
A short history
2006
Graphite starts at Orbitz as a side project to store and draw time-series data. Its site says it became open source in 2008.
2012
Prometheus begins at SoundCloud, with labels on every series and scraping over HTTP.
2015
The Gorilla paper in the VLDB proceedings reports 1.37 bytes a point, down from 16, with delta-of-delta times and XOR values.
August 2019
The DDSketch paper shows a percentile summary that can be merged without losing its error bound.
Words to know
- Time series
- One metric name with one exact set of labels, and its points.
- Label or tag
- A key and value such as
service=api. Every new combination is a new series. - Cardinality
- How many different series exist. It decides memory and index size.
- Counter, gauge, distribution
- A number that only grows, a number that goes up and down, and a spread of values such as latency.
- Rollup
- A summary of many points for a longer step: least, most, sum and count.
- Scrape
- The server fetches the numbers (pull), instead of agents sending them (push).
- Flapping
- An alert that fires and clears again and again.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Collect. An agent on each host batches its numbers and sends them every ten seconds
Understand it
A missing slip is not the same as no rain.
Scope
What are we building?
A shared platform for many customer companies (tenants). It collects metrics, stores them for 13 months, draws dashboards and sends alerts. What to measure in the first place is the subject of monitoring basics. Log lines are another problem: see distributed logging.
Collect
Agents, client libraries and cloud integrations, 5 million points a second.
Store
50 million live series, recent points in full, old ones rolled up.
Ask
Search by label, group, add up, draw within 200 ms.
Alert
Thresholds, missing data and unusual values, without crying wolf.
Similar systems
| System | Known for |
|---|---|
| Datadog, New Relic | Hosted platforms with many integrations |
| Prometheus | Open source, scraping, PromQL, one machine |
| Grafana Mimir, Thanos | Long-term stores for Prometheus data |
| Amazon CloudWatch | Metrics of AWS services |
| InfluxDB, VictoriaMetrics | Time-series databases |
| Grafana | Dashboards over many stores |
The finished system
Where the design ends up
This is the last version. The Design tab starts with one service and one database.
Collecting, asking and alerting
The life of a point
Where to read what
| Question | Place |
|---|---|
| How do I open this interview? | Approach |
| What is in and out? | Scope |
| How big is it? | Numbers |
| How do clients talk to it? | API and data |
| Compression, cardinality, alerts? | Deep dives |
| Can I defend it? | Explain and By level |
| Can I practise it? | Mock |
Summary
The whole topic in one minute.
Agents batch points every ten seconds. Gateways check and limit them and write to a log. Pairs of ingesters keep the newest hours in memory, compressed to about 1.5 bytes a point, and put finished two-hour blocks in object storage. Compactors roll old data up to five minutes and one hour.
Queries pick the resolution that fits the time range and join memory with storage. Alert rules are small queries on a schedule, with a waiting time, a separate recovery level and a state for missing data. A watcher outside the system checks that the alerts themselves still work.
- Every new label combination is a new series. Limit it where it is created.
- Neighbouring points are alike, so 16 bytes become about 1.5.
- A rollup keeps least, most, sum and count. Never an average of averages, never an average of percentiles.
- Choose the resolution from the time range: nobody can see 3 million points on one line.
- An alert has states: waiting, firing, recovering, no data.
- Something outside must notice when the monitoring itself stops.
Sources
- Gorilla: a fast, scalable, in-memory time series database (VLDB)
- Prometheus: storage
- Prometheus: alerting rules
- Prometheus: Alertmanager
- Prometheus: histograms and summaries
- Prometheus: metric and label naming
- DDSketch (arXiv)
- Grafana Mimir: ingester
- Thanos: compactor
- Amazon CloudWatch concepts
- Graphite project site