Library
35 min read

Design a metrics and monitoring system

Every app you have used has a screen somewhere showing its health: requests a second, errors, how long pages take, how full the disks are. When a number crosses a line, someone's phone rings at 3am. The system behind that screen and that phone call is a metrics and monitoring system.

It has an unusual requirement: it has to be the last thing standing, because its whole job is to tell you everything else is broken. It also has an unusual workload. The writes never stop and never slow down. Every server, every ten seconds, forever, whether anyone is looking or not. Watch one number travel from a server to a chart, and to that phone.

ServicesService discoveryScrapersCardinality guardTime series DBDownsamplerQuery layerDashboardsAlert evaluatorNotifierOn call

Targets. Service discovery tells the scrapers which services exist right now. Nobody keeps that list by hand.

A few words this page uses everywhere:

  • A metric is a number you measure again and again: requests a second, CPU used, queue length. Each measurement, a timestamp plus a value, is a point (also called a sample).
  • A time series is one metric's points in time order, like one line on a chart.
  • A label is a name="value" tag on a metric, such as svc="api" or code="500". Every distinct combination of labels is its own time series.
  • Cardinality is how many distinct series exist. It is the number that kills these systems.

A small worked example. A metric http_requests has two labels: svc with 3 values and code with 4 values. That is 3 x 4 = 12 series. Now someone adds customer_id, which has 10,000 values. The same metric is now 3 x 4 x 10,000 = 120,000 series, from a one line change. That is cardinality, and it kills these systems suddenly, from a change nobody reviewed carefully.

Metrics are one of three ways to see inside a running system, next to logs and traces. Logs vs metrics vs traces explains what each is for, and why per customer detail belongs in logs or traces rather than in a metric label.

Step 1: Understand the problem#

Six questions turn "build monitoring" into a design. Two of the answers below are the ones that matter, and neither is about how many metrics a second you can take. Find them.

You askThey sayWhat it settles
How many machines, and how many metrics does each expose?Twenty thousand hosts, about five hundred series each.About 10 million active series, which is a storage engine decision. It is also the number that a single careless label can multiply.
At what interval, and does it ever stop?Every ten seconds, forever, whether anyone is watching or not.A constant write rate with no idle period. Compaction (merging small files on disk into bigger ones) has no quiet hour to run in, so background work has to survive alongside full load.
Who controls what gets labelled?Every engineer, in their own code, with no review.The failure mode of the whole system. Anyone can add a label, so limits and rejection have to be built in rather than left to policy.
How long do we keep it, and at what resolution?Days at full resolution, a year rolled up.Retention (how long data is kept) becomes a pipeline of downsampling stages rather than a delete job. Downsampling means replacing many fine points with fewer coarse ones. It also decides what a long range query is allowed to read.
Does alerting run on this same system?Yes, and it must work while everything else is on fire.A separate evaluation path with its own capacity. During an incident a hundred engineers are loading dashboards, and that is exactly when alerts must not queue behind them.
Is it allowed to depend on the platform it monitors?No.Separate cluster, separate identity, separate service discovery, and paging that leaves your infrastructure. A monitoring system that goes blind with the thing it watches is decoration.

What you are building#

Functional requirements
What the system does.
  • Ingest numeric time series from tens of thousands of hosts. Counters (only go up, like total requests), gauges (go up and down, like memory used) and histograms (counts per bucket, like how many requests took under 100ms), at second or ten second resolution.
  • Query and aggregate over time ranges. Both a dashboard covering a month and an alert covering five minutes.
  • Evaluate alert rules continuously. This is the part that has to work during an incident.
  • Retain data with declining resolution. A year of raw seconds is neither affordable nor useful.
Non-functional requirements
How well it has to do it. The numbers you commit to.
  • 10 million active time series.
  • 1 million data points per second: 10 million series, one point each every 10 seconds.
  • Alert evaluation within 30 seconds of the data arriving.
  • The monitoring system must not depend on the systems it monitors.
Out of scope
What you cut, and say so out loud.
  • Distributed tracing and logs. Different shapes, different storage, and each is its own design question.
  • Anomaly detection. It sits on top of the query layer.
  • Incident management and paging workflow, which is a product rather than an infrastructure problem.

Back of the envelope, and the number that bites#

The defaults below are the numbers from the questions above: 20,000 hosts x 500 metrics is 10,000,000 series. One point every 10 seconds is 1,000,000 points a second. At 16 bytes a point (8 for the timestamp, 8 for the value) and 8,640 points a day (86,400 seconds / 10), raw data is about 1.4 TB a day. If this kind of estimate is new, the back of the envelope chapter walks through the method.

Cardinality
20 thousand
500
1
Active time series20k x 500 x 1 = 10,000,000
Points per second at 10s resolution1,000,000
Raw per day at 16 bytes a point1.4 TB
Compressed, roughly 10x138 GB
Index memory at ~1KB a series10.0 GB
10,000,000
series, before anyone adds an interesting label

Now drag the third slider. It represents one engineer adding one label to one metric, and it multiplies everything above.

Drag the third slider from 1 to 200 and watch. That slider is one engineer adding customer_id to a metric so they can debug one account. It is a one line change, it passes review, and it multiplies the entire system by two hundred.

Look at which rows go amber. Disk grows, but compression absorbs a lot of it. The row that fails first is index memory: the lookup table that finds a series by its labels, held in RAM so queries are fast. It needs an entry for every series, however few points that series has.

Why cardinality hurts more than volume

Doubling the points per series costs you disk roughly linearly, and compression absorbs a lot of it. Doubling the number of series doubles the index, doubles the open write buffers, doubles the compression state held in memory, and makes every query touch twice as many streams. Volume is a storage problem. Cardinality is a memory problem, and memory is the thing you run out of suddenly rather than gradually.