AlgoViz

Design a Metrics Monitoring System (Datadog)

hard 50 min

Collect 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

0:00 / 4:09

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

  1. 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.

  2. 2012

    Prometheus begins at SoundCloud, with labels on every series and scraping over HTTP.

  3. 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.

  4. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Collect. An agent on each host batches its numbers and sends them every ten seconds

Understand it

The almanac of rain
12

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

SystemKnown for
Datadog, New RelicHosted platforms with many integrations
PrometheusOpen source, scraping, PromQL, one machine
Grafana Mimir, ThanosLong-term stores for Prometheus data
Amazon CloudWatchMetrics of AWS services
InfluxDB, VictoriaMetricsTime-series databases
GrafanaDashboards 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

Agents on customer hosts send batches to gateways, which write to Kafka. Pairs of ingesters read Kafka, hold recent points and put blocks in object storage. Compactors roll the blocks up. Engineers ask the query service, which reads ingesters and object storage. Rule workers also ask the query service and pass changes to the alert manager.
Blue is the write path, pink is questions, orange is background work and alerts. Dashed arrows delay nobody.

The life of a point

Points of one series arrive at an ingester, are compressed in memory, become a two-hour block in object storage, then five-minute and one-hour rollups.
Full detail for 15 days, five minutes for 90, one hour for 400.

Where to read what

QuestionPlace
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
Ideas with their own pages: LSM trees, push and pull, log-based queues, stream processing, notification service.

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