HAMZA KAREEM / CONCEPTS hamzawi.dev

Lesson 1452 min read

Metric Cardinality Explosion

one label can cost more than your whole app

Developer toolingdeveloper tooling / observability

The myth

"Metrics are just counters. Adding a label is basically free — it's one more string. If it helps me debug, add it."

Actually

A label is not a column, it's a multiplier. In Prometheus-style systems every unique combination of label values is its own time series, with its own RAM, its own index entry, its own chunks on disk. You don't add a field. You multiply how much you store.

The arithmetic that gets you paged

One metric:

http_requests_total{method, status, endpoint, region}

method 5 × status 8 × endpoint 40 × region 3 = 4,800 series. Totally fine.

Now someone adds user_id to "see per-customer latency":

http_requests_total{method, status, endpoint, region, user_id}

4,800 × 50,000 users = 240 million series

Prometheus needs roughly 1–3 KB of RAM per active series. 240M × 2 KB ≈ 480 GB for ONE metric. Your pod has 8 GB. It OOMKills, restarts, replays the WAL, OOMKills again — and you lose every other metric with it, including the ones you needed to diagnose the outage.

Labels that look innocent and aren't

  • user_id, order_id, request_id, trace_id — unbounded by definition
  • raw path: /orders/8831 is a new series per order. Normalize to /orders/:id
  • error_message — DB errors and stack traces are effectively infinite strings
  • pod_name / container_id — bounded at any instant, but every deploy churns the whole set. Dead series still occupy memory until they age out, so 10 deploys/day multiplies your footprint across the lookback window
  • anything time-derived — never

The multiplication is the whole danger: bolting one 100-value label onto a metric that already has 2,000 series gives you 200,000.

The rule that keeps you safe

Labels are for slicing, not for identifying. Ask: "would I ever write group by on this?" You group by status and region. You never group by user_id — you look up one user, and that's a logs/traces question. Metrics answer "how many, how fast, by category". Traces answer "what happened to this one request".

Find it before it finds you

Worst offenders by series count:

topk(10, count by (__name__)({__name__=~".+"}))

Cardinality of one label on one metric:

count(count by (user_id) (http_requests_total))

Guardrail in the scrape config — drop the label instead of dying:

metric_relabel_configs:
  - regex: user_id|request_id
    action: labeldrop

Note it runs at ingestion, so the exporter still pays the cost; fix the instrumentation too.

Your exercise

Take the metric you query most on your own dashboards. List its labels, estimate how many distinct values each can EVER take, and multiply them out — write the number down. Then for each label ask: have I ever actually grouped by it? Pick the one you haven't, and work out what you'd genuinely lose by deleting it.