awesome-everything RU
↑ Back to the climb

Observability

Metrics and cardinality: the cost model of a time-series database

Crux How Prometheus-model metrics work, why cardinality is the central cost driver, and what happens when a single label explodes series count.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at middle altitude — in the sky
◷ 14 min

A team adds a customer_email label to their request_total counter to track per-customer error rates. Within two hours the Prometheus server runs out of memory and 15 minutes of alerts go missing. The label looked harmless.

How a metric is stored

The canonical model is Prometheus. A metric is identified by a (metric_name, label_set) pair, where the label set is a small fixed collection of key=value pairs. Every unique combination of label values creates one time series — a stream of (timestamp, value) samples, typically scraped every 15 s.

Four standard metric types:

  • Counter — monotonically increasing integer. Only a rate-of-change (per second) is meaningful. Example: http_requests_total.
  • Gauge — instantaneous reading that can go up or down. Example: memory_used_bytes, queue_depth.
  • Histogram — distributes observations across pre-defined buckets for percentile queries. Example: http_request_duration_seconds with buckets [0.01, 0.05, 0.1, 0.5, 1.0, 5.0].
  • Summary — pre-computed per-replica percentile. Rarely used for new instrumentation because it cannot be aggregated across replicas.

The compute model is pre-aggregation at the source: the application increments counters in-process, the scraper ships them to the TSDB, and the TSDB stores deltas and serves queries by walking pre-rolled-up buckets. This is why metric queries return in tens of milliseconds across years of history: the heavy lifting happened at write time, not read time.

TypeGoes up/downUse forExample
CounterUp onlyRate of eventshttp_requests_total
GaugeBothCurrent statememory_used_bytes
HistogramUp only (buckets)Latency distribution, percentileshttp_request_duration_seconds
SummaryPre-computedPercentile at source (not aggregatable)Legacy; prefer histogram

Cardinality: the central cost driver

Each unique combination of label values creates a separate time series in the TSDB. The math is multiplicative:

  • route (20 templates) × method (5 verbs) × status_class (4 classes) = 400 series — fine.
  • Same metric + user_id (100 k active users) = 100 k × 20 × 5 × 4 = 40 million series — Prometheus OOM on 16 GB.

Prometheus stores roughly 3 KB per active series in the head block. At 40 M series that is 120 GB, far beyond any commodity machine’s memory.

Both Grafana’s published guidance and the Prometheus instrumentation documentation say the same thing: label only with values from a small fixed set — route templates not full URLs, status classes not status codes, customer segments not customer IDs. Anything with unbounded values belongs in logs (free cardinality) or traces (sampled), not metrics.

The exemplar bridge

When a histogram records a slow observation, the client can attach one exemplar — a trace_id from the request that produced the observation. Grafana renders exemplars as dots on the histogram heatmap; clicking a dot opens the corresponding trace.

Exemplars (standardised in Prometheus 2.32, early 2022) solve the gap that defined the pre-exemplar era: “metrics say something is slow, but I have no example request to drill into.” They make it possible to keep user_id out of metric labels — cardinality stays bounded — while still being able to navigate from an aggregate spike to one concrete request via the trace.

Prometheus cardinality and cost numbers
Prometheus bytes per active series (head block)
~3 KB
Safe-zone series count on 16 GB box
~5 M
Cardinality multiplier per high-card label
100x to 10 000x
Typical scrape interval
15 s
Series created by route × method × status_class (20×5×4)
400
Additional series if user_id (100 k) is added
40 M
Quiz

A team adds customer_email to their request_total counter. The Prometheus server runs out of memory within hours. Why?

Quiz

What does an exemplar on a histogram metric do?

Quiz

Which metric type should you use for 'checkout request duration, so you can query p99 across all replicas'?

Recall before you leave
  1. 01
    Explain in your own words why metrics are cheap to query but blind, while logs are powerful but expensive.
  2. 02
    A metric has labels: route (30 templates), method (5), status_class (4), region (3). Is this safe? What would make it unsafe?
  3. 03
    Why use an exemplar instead of just adding user_id as a metric label?
Recap

A Prometheus metric is identified by a (name, label_set) pair; each unique combination of label values creates one time series stored at roughly 3 KB in the head block. Pre-aggregation at the source is what makes queries fast across years, but it means metrics can only answer questions about dimensions you labelled in advance. Cardinality — the product of all label-value counts — is the dominant cost driver: a single unbounded label like customer_email on a busy metric can multiply series count from thousands to tens of millions, OOM-ing the server. The four metric types are counter (rate-of-change), gauge (instantaneous), histogram (percentile-queryable distribution), and summary (pre-computed, non-aggregatable). Exemplars bridge the aggregate metric to a concrete trace, eliminating the need to label with high-cardinality identifiers.

Connected lessons
appears again in167
Continue the climb ↑Logs and volume: the cost model of structured logging
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.