Cardinality Management
Finding, fixing, and preventing series explosions
The single most common way Prometheus falls over in production is a cardinality explosion: a deploy adds a label with unbounded values, the number of active series jumps from 500k to 5M in an hour, memory follows, and Prometheus is OOM-killed, taking every alert with it.
Two kinds of problem
High cardinality is many series existing at once. It costs memory and slows every query that touches the metric.
High churn is many series being created and abandoned: a
pod label in Kubernetes where every deploy brings new pod names, or a
version label that changes on each release. At any moment the active
count looks fine, but every series that was active in the head window
still costs memory, the index keeps growing, and long-range queries touch
millions of dead series.
# cardinality: series in the head right now
prometheus_tsdb_head_series
# churn: new series per second; compare with your normal baseline
rate(prometheus_tsdb_head_series_created_total[5m])Finding the culprit
Work from the top down.
1. Which job? Samples per scrape, by job:
topk(10, sum by (job) (scrape_samples_post_metric_relabeling))
# which jobs are creating new series right now?
topk(10, sum by (job) (rate(scrape_series_added[5m])))2. Which metric?
topk(10, count by (__name__) ({job="demo-app"}))3. Which label? For the top metric, count distinct values of each label:
count(count by (route) (http_requests_total))
count(count by (status) (http_requests_total))
count(count by (instance) (http_requests_total))The TSDB status page (Status → TSDB status, or
/api/v1/status/tsdb) does steps 2 and 3 for the whole server at once:
top metrics by series count, top label names by number of values, and
top label pairs by series.
curl -s localhost:9090/api/v1/status/tsdb | jq '{
series: .data.headStats.numSeries,
metrics: .data.seriesCountByMetricName[:5],
labels: .data.labelValueCountByLabelName[:5]
}'Any label with thousands of values is a suspect. A label name like path,
url, user, id, ip, or query near the top almost always is the
cause.
Stopping the bleeding
In an incident, fix it at the scrape, not in the application, because that takes effect on the next reload:
metric_relabel_configs:
# drop the whole metric
- source_labels: [__name__]
regex: "http_request_duration_by_path_seconds_bucket"
action: drop
# or keep the metric but remove the offending label
# (only safe if the remaining labels still identify unique series)
- regex: "path"
action: labeldropThen add a hard limit so it can't happen again silently:
- job_name: demo-app
sample_limit: 5000 # fail the whole scrape if it returns more samples
label_limit: 30 # max labels per series
label_value_length_limit: 200A scrape that exceeds sample_limit fails entirely and up becomes 0.
That sounds harsh, but it's the point: one team's bug turns into one
target-down alert instead of an outage of the whole monitoring system.
labeldrop can make two previously distinct series identical (same
name, same remaining labels). Prometheus then rejects the duplicates
as "duplicate sample for timestamp" errors, and you lose data quietly.
Prefer fixing the label in code, or replacing the value with a bounded
bucket using a replace rule, over dropping it outright.
Fixing it properly
The real fix is in the code that creates the series:
| Cause | Fix |
|---|---|
| Raw URL path as a label | Use the route template (/users/:id). Most framework middleware can provide it. |
| User/tenant/customer IDs | Remove. Use logs or traces for per-entity questions. If you truly need per-tenant metrics, cap to the top N tenants and bucket the rest as other. |
| Error messages as labels | Use an error class or code, from a fixed enum. |
| Too many histogram buckets × labels | Fewer buckets, fewer labels on the histogram than on the counter, or native histograms. |
| Pod/container IDs churning | Expected in Kubernetes. Keep them on raw series, but aggregate them away in recording rules and dashboards. |
Preventing it
- Review label additions like schema changes. A new label on a hot metric multiplies its series.
- Set
sample_limiton every job, sized to about twice its normal count. - Alert on growth, before memory runs out:
- alert: SeriesGrowthHigh
expr: prometheus_tsdb_head_series > 1.5 * (prometheus_tsdb_head_series offset 1d)
for: 30m
labels: { severity: warning }
- alert: ScrapeSampleLimitHit
expr: increase(prometheus_target_scrapes_exceeded_sample_limit_total[5m]) > 0
labels: { severity: warning }Lab: cause an explosion, then fix it
- In
demo-app/app.py, temporarily changeroute = path if path in ROUTES else "other"toroute = path. Rebuild the demo app. - Generate unique paths:
for i in $(seq 1 2000); do curl -s localhost:8080/item/$i >/dev/null; done - Watch
prometheus_tsdb_head_seriesandcount(count by (route) (http_requests_total))jump. Find the cause with the TSDB status page as if you didn't already know. - Stop the bleeding with a
metric_relabel_configsrule that rewrites anyroutenot in the known set toother. Validate, reload. - Fix the code properly, rebuild, and remove the relabel rule.
- Add
sample_limit: 2000to thedemo-appjob and confirm normal scrapes are well under it withscrape_samples_scraped{job="demo-app"}.