Skip to content

Troubleshooting guide

Prometheus high cardinality: how to find and fix it

Every unique combination of label values creates a time series. One unbounded label, such as a user ID, can exhaust Prometheus.

3 min read10 sectionsUpdated

On this page

Key points

  • Series count drives memory, not sample volume.
  • Labels multiply.
  • Never put IDs, URLs or emails in labels.
  • Drop at scrape time with relabeling.

What cardinality is

A metric with labels method (5 values), status (10) and route (100) can reach 5,000 series. Adding user_id with 100,000 values brings a theoretical 500 million. Prometheus holds active series in memory, so this translates directly into RAM, and eventually into out-of-memory crashes.

Common causes

  • User IDs, order IDs and emails as labels.
  • Raw URLs rather than route templates.
  • Error messages in a label.
  • Pod names multiplied by many other labels, plus churn from frequent deployments.
  • Histograms with many buckets multiplied by labels.

Find the worst offenders

The status page at /tsdb-status ranks metrics and labels. Query it through the API as well.

bash
# Top metrics and labels by series
curl -s localhost:9090/api/v1/status/tsdb | python3 -c "
import sys,json; d=json.load(sys.stdin)['data']
print(d['headStats']); print(d['seriesCountByMetricName'][:8]); print(d['labelValueCountByLabelName'][:8])"

# Analyse a block offline
promtool tsdb analyze /prometheus

# PromQL: series per metric
# topk(10, count by (__name__)({__name__=~".+"}))

Fixes

Fix instrumentation by using bounded values: a route template, a status class, a small enum. Then drop what you cannot change at scrape time:

metric_relabel_configs: [{ action: labeldrop, regex: user_id }]

Use action: drop to remove whole metrics that nobody uses. Reduce histogram buckets. Set sample_limit on scrape jobs, so a single bad target cannot sink the whole server.

Dropping a label can break things

If you drop a label that was making series distinct, two series can collapse into duplicates, and Prometheus will reject them for that scrape. It is often better to aggregate at the source, or drop the metric. Be careful with queries as well: count by (__name__) across everything is itself expensive, so run it with care on a struggling server. And remember the churn: series from pods that no longer exist still count until they leave the head block, so rapid redeployments inflate memory even with a modest number of labels.

Catch it automatically

Alert on prometheus_tsdb_head_series growth, and review the top metrics monthly. See metric cardinality for the concept, and reducing observability costs.

Frequently asked questions

How many series can Prometheus hold?

It depends on RAM. Millions are possible, though a few KB per active series is a typical estimate.

Can I use a user ID as a label?

No. Put it on traces or logs.

What does sample_limit do?

It fails a scrape that exceeds the limit, protecting the server.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.