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
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.
Find the worst offenders
The status page at /tsdb-status ranks metrics and labels. Query it through the API as well.
# 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.
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.
Related reading
- Goroutine leaks in Go: how to find and fix them: Find and fix goroutine leaks in Go: read pprof goroutine profiles, spot blocked channels and missing context cancellation, and use goleak in your tests.
- High load average on Linux: what it means and how to fix it: Understand Linux load average: how it relates to CPU count, why I/O wait raises it with idle CPUs, and how to find the processes responsible using vmstat.
- 404 Not Found: how to find and fix broken pages: Find and fix 404 errors: broken internal links, moved pages, case-sensitive paths and soft 404s, with commands and advice on what actually affects SEO.
- 500 Internal Server Error: how to find the cause: A 500 error means your code failed. Find the exception behind it fast with logs, traces and error tracking, and learn how to prevent repeats.
- High iowait on Linux: how to find the cause: Diagnose high iowait on Linux with iostat and iotop: read await and %util correctly, find the process doing the I/O, and fix slow queries and swap.
- How to find a Node.js memory leak: Detect a Node.js memory leak from heap metrics, compare heap snapshots to find what retains memory, fix the usual causes and alert before a crash.
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.