A Prometheus server that ran on the same memory limit for a year gets OOM-killed twice in one afternoon. Nobody touched the scrape config. What changed is a pull request that added a customer_id label to a request counter, merged on Tuesday and rolled out to every pod by Wednesday. Prometheus high cardinality is the most common way a healthy Prometheus falls over, and it is almost always caused by a handful of labels rather than by traffic.
This guide covers what a series actually is, why label values multiply instead of add, how to find the metric responsible in a few minutes, and the fixes that work: changing instrumentation, relabeling at scrape time, moving per-request detail into traces and exemplars, and dropping attributes in the OpenTelemetry SDK before they ever become labels.
What a series is
Prometheus identifies a time series by its metric name plus the complete set of label names and values. These are two different series, even though they share a metric name and differ in one value:
http_requests_total{method="GET", route="/api/orders", status="200"}
http_requests_total{method="GET", route="/api/orders", status="500"}Each distinct combination gets its own entry in the index, its own chunks in the in-memory head block, and its own samples on disk. The memory a Prometheus server needs is driven mainly by how many series are active in the head block, not by how many samples each series has. A thousand series scraped every 15 seconds are cheap. A million series scraped every 15 seconds need a much larger machine, and queries that touch them get slower in proportion.
Cardinality is simply the number of distinct series. When people say a metric "has high cardinality", they mean one or more of its labels takes so many values that the series count runs into the hundreds of thousands or millions.
How label combinations multiply
The upper bound on a metric's series count is the product of the number of distinct values of every label, multiplied by the number of targets that expose it. A modest HTTP counter shows how quickly that grows:
- 5 methods x 40 routes x 10 status codes = 2,000 combinations per instance.
- Scraped from 30 pods, each pod adds its own
instanceandpodlabels: 60,000 series. - Make it a histogram with the Go client's 11 default buckets plus
+Inf,_sumand_count: 14 series per combination, so 840,000. - Add a
customer_idlabel with 5,000 values and the theoretical ceiling is in the billions.
Not every combination occurs in practice (no route returns every status code), so the real number sits below the product. Labels like customer_id are what push it toward the ceiling, because they combine with everything else on the metric.
Churn is cardinality spread over time
Some labels are bounded at any one moment but keep changing. In Kubernetes, every deployment replaces pod names, so a metric with a pod label creates a fresh set of series on each rollout. The old series stop receiving samples but stay in the head block until the next head compaction. Ten deploys in an afternoon can multiply the head series count even though the number of running pods never changed. Container IDs, build hashes and short-lived job names behave the same way.
The usual suspects
- Identifiers: user, customer, session, request and trace IDs.
- Raw paths:
/orders/84213instead of the route template/orders/{id}. - Free text: error messages, SQL statements, user agent strings.
- Network detail: client IP addresses and ephemeral ports.
- Churning infrastructure names: pod names, container IDs and job run IDs on metrics that do not need them.
Finding the offenders
The TSDB status page
Start with Status > TSDB Status in the Prometheus UI. It shows head block statistics (the number of series and chunks, and the time range covered) followed by the top metric names by series count, the label names with the most distinct values, the label names using the most memory, and the label-value pairs attached to the most series. The same data is available from the HTTP API, and the limit parameter returns more than the default ten rows:
curl -s 'http://localhost:9090/api/v1/status/tsdb?limit=20' \ | jq '.data.seriesCountByMetricName, .data.labelValueCountByLabelName'
Read the two lists together. A metric at the top of the series count list tells you where to look; a label at the top of the value count list usually tells you why.
PromQL for cardinality
When you need more detail than the status page gives, query the TSDB directly. These are the queries worth keeping in a runbook:
# Top 10 metric names by series count
topk(10, count by (__name__)({__name__=~".+"}))
# How many distinct values one label has on one metric
count(count by (route) (http_requests_total))
# Scrape targets exposing the most samples after relabeling
topk(10, scrape_samples_post_metric_relabeling)
# Targets creating the most new series (churn) over the last hour
topk(10, sum_over_time(scrape_series_added[1h]))The first query matches every series in the database, which makes it one of the most expensive things you can ask Prometheus to do. Run it as an instant query, not over a range, and use the status page instead on a server that is already struggling. scrape_samples_post_metric_relabeling and scrape_series_added are synthetic series Prometheus records for every target on every scrape, so they point at the service responsible even when you do not know which metric to look at. If you are new to the count by pattern, our PromQL for beginners guide walks through aggregation operators.
Watch the total before it pages you
prometheus_tsdb_head_series is the number of series in the head block, and it belongs on the dashboard of every Prometheus you run. Alert on sudden growth rather than on an absolute number, so the alert fires on the day the bad label ships instead of the day memory runs out:
# Head series grew by more than half compared with yesterday prometheus_tsdb_head_series > 1.5 * (prometheus_tsdb_head_series offset 1d) # Rate of series creation, the churn signal rate(prometheus_tsdb_head_series_created_total[5m])
Fixing high cardinality
1. Change the instrumentation
The best fix is upstream. Label with the route template, not the raw path: most framework instrumentations already do this, and the OpenTelemetry http.route attribute is defined as the template. Turn unbounded values into bounded ones: status codes into classes like 2xx and 5xx where the exact code does not matter, customers into a plan tier, user agents into a browser family. Ask one question about every new label in code review: what is the largest number of values it can ever have?
2. Drop or rewrite labels with metric_relabel_configs
When you cannot change the code quickly, metric_relabel_configs runs after each scrape and before samples are stored. It can drop a label, drop a whole metric, or rewrite values:
scrape_configs:
- job_name: api
static_configs:
- targets: ['api:8080']
# Guardrails: fail the scrape instead of ingesting a series explosion
sample_limit: 50000
label_limit: 30
label_value_length_limit: 200
metric_relabel_configs:
# Remove a label that should never have been added
- action: labeldrop
regex: customer_id
# Drop a metric family nobody queries
- source_labels: [__name__]
regex: grpc_server_handling_seconds_bucket
action: drop
# Collapse raw order paths into one value
- source_labels: [path]
regex: '/orders/[0-9]+'
target_label: path
replacement: '/orders/:id'Three things to know before you rely on this. First, relabeling saves memory and storage, but the target still renders every series and Prometheus still parses them, so scrape cost is unchanged. Second, labeldrop must leave every series unique. If two series differed only in customer_id, dropping it produces two samples with identical labels and timestamps; Prometheus rejects the duplicates and counts them in prometheus_target_scrapes_sample_duplicate_timestamp_total. Relabeling cannot sum the two counters back together, so for a label like that the real fix is in the code. Third, sample_limit fails the entire scrape when exceeded and up drops to 0 for that target. That is the point: a loud failure on one job is better than a silent memory climb on the whole server.
If Prometheus forwards to remote storage, write_relabel_configs applies the same rules to what leaves the server, so you can keep a metric locally for debugging without paying to store it twice:
remote_write:
- url: https://app.aiaxoniq.com/otlp/api/v1/prom/write
headers:
x-license-key: YOUR_LICENSE_KEY
write_relabel_configs:
- source_labels: [__name__]
regex: 'go_gc_.*|promhttp_.*'
action: drop3. Move per-request detail to traces and exemplars
Most cardinality problems are a metric being asked to do a trace's job. Metrics answer "how many and how fast" for a population; traces answer "which request, for which customer, and why". A span can carry customer.id, the order number and the full URL, and its cost scales with request volume and your sampling rate, not with the number of label combinations. Keep the metric at route and status, and put the identifiers on the span.
Exemplars connect the two. An exemplar attaches a trace ID to an individual histogram or counter sample without making it a label, so it adds nothing to the series count. Prometheus stores exemplars when started with --enable-feature=exemplar-storage, and they are scraped from targets that expose the OpenMetrics format. On a latency panel, an exemplar is the dot you click to open the slow request that pulled the p99 up.
4. Drop attributes with OpenTelemetry views
If your metrics come from an OpenTelemetry SDK, every attribute on a measurement becomes a label after export. A view tells the SDK which attribute keys to keep for an instrument; measurements are aggregated across the dropped keys inside the process, so counters stay correct and nothing collides downstream. In Python:
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.view import DropAggregation, View
views = [
# Keep three attributes on the server duration histogram;
# anything else recorded on it is aggregated away in the SDK.
View(
instrument_name="http.server.request.duration",
attribute_keys={"http.request.method", "http.route", "http.response.status_code"},
),
# Stop exporting an instrument entirely.
View(instrument_name="app.cache.debug_lookups", aggregation=DropAggregation()),
]
# reader: your PeriodicExportingMetricReader
provider = MeterProvider(metric_readers=[reader], views=views)Java has the same control as View.builder().setAttributeFilter(...), and Go as the AttributeFilter field on metric.Stream. Deleting the attribute later in the Collector is not equivalent: by then the SDK has already produced one data point per combination, and removing the attribute leaves several points sharing one identity, the same collision problem as labeldrop.
A cardinality budget that holds
- Review every new label for its maximum number of values, and reject identifiers.
- Label with route templates and bounded classes, never raw paths or free text.
- Set
sample_limitandlabel_limitper scrape job so one bad deploy fails loudly. - Graph
prometheus_tsdb_head_seriesand alert on day-over-day growth and on series creation rate. - Check the TSDB status page monthly, before it becomes an incident.
- Put per-request identifiers on spans and link them back with exemplars.
- In OpenTelemetry, use views to keep only the attributes you query.
Where aiAxonIQ fits
Part of the pain above comes from storage design: Prometheus holds every active series in an in-memory index, so series count maps directly to memory. Metrics & Infrastructure in aiAxonIQ accepts Prometheus remote_write (1.0 and 2.0) and native OTLP metrics, stores them in per-tenant OpenObserve streams without cardinality throttling, and runs PromQL on the upstream Prometheus engine over those streams. Query autocomplete scores label cardinality as you type, which puts the offenders in front of you while you write the query. That makes labels like a tenant ID or a container ID practical to keep. A request ID still belongs on a span, for the reasons above; our post on ClickHouse for observability covers why a column store handles wide label sets differently.