Metric Labels and Cardinality: Why Metrics Get Expensive
A developer adds a user_id label to a request counter so the team can "see which customers are busiest". A week later the Prometheus server runs out of memory, restarts in a loop, and every dashboard goes blank in the middle of a sale. The code had no bug; one label did it. This lesson covers labels and cardinality: how labels split one metric into many time series, and why some labels quietly multiply your costs.
Labels split one metric into many series
A label is a key-value pair attached to a metric that says which slice of it a value belongs to. With labels, one metric name can answer many questions. http_requests_total{route="/pay",status="500"} counts failed payment requests, while http_requests_total{route="/cart",status="200"} counts successful cart views.
As you saw in What Is a Metric?, every distinct combination of name and label values is a separate time series. That is what makes labels powerful: you can add up all routes to get total traffic, or pick out only the errors on /pay. This query, using the rate() function from the metric types lesson, draws one line per route:
sum by (route) (rate(http_requests_total[5m]))
sum by (route) adds together every series that shares a route, whatever its method or status, and keeps the route label on the result. Without labels you would need a separate metric name for every route and status pair, and no query could add them up for you.
Cardinality: how many series a metric creates
Cardinality means "how many distinct values". For a label, it is the number of different values the label takes. For a metric, it is the number of series that exist under its name. A label such as status has low cardinality, a handful of values; a label such as user_id has high cardinality, one value per user.
Every series has a cost. Prometheus keeps the recent samples of every active series in memory, plus an index that maps each label value to its series, and it writes all of them to disk. That cost grows with the number of series, not with traffic. A counter that receives a million increments a minute costs the same as one that receives ten.
The worst case is the product of every label's value count. Take a counter with 5 methods, 50 routes, 8 status codes and 200 pods: 5 × 50 × 8 × 200 gives 400,000 series from one metric name. Only combinations that actually occur are created, but a busy service exercises most of them. The pod label is especially costly in Kubernetes, because each deploy gives every running copy a new name, so old series stop and brand-new ones start. This turnover is called churn.
The Prometheus instrumentation guide sets a deliberately strict rule of thumb: keep the cardinality of most metrics below 10, and allow only a handful of metrics across your whole system to go above that.
Labels to avoid, and what to use instead
A good label has a small, bounded set of values that you know in advance: HTTP method, route template, status code, region. A bad label is unbounded, meaning its set of values keeps growing as users and requests arrive. The Prometheus naming guide warns against unbounded values such as user IDs and email addresses. The usual offenders are:
- User IDs and email addresses: one series per customer, and email addresses are personal data that should not sit in a metrics system at all.
- Request IDs and trace IDs: unique for every request, so each series gets one sample and is never used again.
- Raw URLs such as
/orders/98213: one series per order. Use the route template/orders/:idinstead, which most web frameworks can give you. - Error message text: messages often contain IDs, file paths or timestamps, so they rarely repeat.
When you need per-user or per-request detail, put it where it belongs: in a log line, or as an attribute on a trace span. Those systems store one record per event by design. If you still want some breakdown in metrics, group the values into a few buckets, such as plan="free", "pro" or "enterprise" instead of a user ID.
Finding and cutting high-cardinality metrics
Start by measuring. The first query below counts every series Prometheus holds; the second lists the ten metric names with the most series. Run them in the Prometheus web UI.
count({__name__=~".+"})
topk(10, count by (__name__)({__name__=~".+"}))
__name__ is the hidden label that holds each metric's name, so {__name__=~".+"} matches every series. Once you know the worst metric, count the values of a suspect label: count(count by (user_id) (checkout_requests_total)) returns how many different user IDs it holds. The TSDB status page, under the Status menu of the web UI, shows similar top lists without a query.
The lasting fix is in the code: remove the label, or replace it with a bounded group. Until that ships, you can stop the damage at scrape time with metric_relabel_configs, rules that Prometheus applies to scraped series before storing them:
scrape_configs:
- job_name: checkout
static_configs:
- targets: ["checkout:8080"]
metric_relabel_configs:
# Emergency brake: drop the whole metric that carries user_id
- source_labels: [__name__]
regex: checkout_requests_total
action: drop
This discards every series named checkout_requests_total from that job. Dropping just the label with action: labeldrop looks gentler, but it is only safe when the remaining labels still tell series apart. Otherwise many series collapse into one identical name and label set, and Prometheus rejects the duplicate samples instead of adding them up.
Check yourself
A metric has a region label with 4 values and a status label with 6. At most how many series can it create?
24, because the worst case is the product of the value counts: 4 × 6.
Why is url="/orders/98213" a bad label while route="/orders/:id" is fine?
The raw URL creates a new series for every order, while the route template has a single value for all of them.
Traffic to a labelled counter grows tenfold. Does it create more series?
No. More traffic only changes the values; only new label values create new series.