> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cardinality How NOT to Scale Prometheus

> Guidance on designing Prometheus metrics to avoid high cardinality by using bounded labels, keeping per-entity identifiers in logs or traces, and applying relabeling and aggregation strategies.

This article explains how to design Prometheus metrics to avoid cardinality pitfalls that can cripple performance and raise costs. It uses a payment service example to show safe, bounded label usage versus dangerous, high-cardinality labels.

## Example: a safe initial design

Imagine a payment service where you want to count every payment processed. You add a counter named `payments_processed_total` and choose labels that are bounded and predictable:

* `provider` (Stripe, PayPal, Square)
* `currency` (usd, eur)
* `result` (success, declined, timeout)

In the Prometheus/OpenMetrics exposition format, a few example time series look like this:

```prometheus theme={null}
# example series for payments_processed_total with bounded labels
payments_processed_total{provider="Stripe", currency="usd", result="success"} 42
payments_processed_total{provider="Stripe", currency="usd", result="declined"} 3
payments_processed_total{provider="Stripe", currency="eur", result="success"} 11
payments_processed_total{provider="PayPal", currency="usd", result="success"} 17
# ... and so on for each provider × currency × result
```

With 3 providers × 2 currencies × 3 results you end up with 3 \* 2 \* 3 = 18 distinct time series. Small, bounded, and easy for Prometheus to handle.

## What goes wrong: adding unbounded labels

If someone proposes adding per-entity labels like `customer_id` or `transaction_id`, the cardinality profile changes dramatically.

For example, adding a `customer_id` label yields time series like:

```prometheus theme={null}
payments_processed_total{provider="Stripe", currency="usd", result="success", customer_id="cust_90210"} 1
payments_processed_total{provider="Stripe", currency="usd", result="declined", customer_id="cust_90210"} 0
payments_processed_total{provider="Stripe", currency="usd", result="timeout", customer_id="cust_90210"} 0
# ... and similarly for other customers and other label combinations
```

After only 1,000 unique customers, the 18 original series could expand to roughly 18,000 series. With 1,000,000 customers you could create 18,000,000 series from a single counter (18 × 1,000,000). This explosion is what we mean by high cardinality.

![A slide titled "Prometheus Metric Design Anti-Patterns" showing two graphs: the left plots rising cardinality with example high-cardinality labels (Customer IDs, Transaction IDs, UUIDs, Emails, IPs), and the right shows a curve that flattens out to illustrate a capped number of rows.](https://mintcdn.com/kodekloud-c4ac6d9a/qM9vA-hQoWNCxcKa/images/Grafana-Prometheus-for-Metrics-Driven-Observability/Cardinality/Cardinality-How-NOT-to-Scale-Prometheus/prometheus-metric-design-cardinality-capped.jpg?fit=max\&auto=format\&n=qM9vA-hQoWNCxcKa\&q=85\&s=f594682ee205e91819f73f49cfe4751c)

A useful rule of thumb: if a label can take more than \~100,000 unique values over time, question whether it belongs in your metrics. Prefer bounded labels in metrics and keep high-cardinality identifiers in logs or traces.

## What "cardinality" means in Prometheus

Cardinality = the total number of unique time series Prometheus stores. Each unique combination of metric name and label set is a separate time series. For example, all of these are distinct series:

* `payments_processed_total{provider="Stripe", currency="usd", result="success"}`
* `payments_processed_total{provider="Stripe", currency="usd", result="success", customer_id="cust_90210"}`
* `payments_processed_total{provider="Stripe", currency="usd", result="success", customer_id="cust_90210", transaction_id="txn_a8f3e9"}`

Why this matters:

* Prometheus keeps active time series in memory. Millions of series drastically increase RAM usage and can make your Prometheus server crash and restart.
* Managed platforms (e.g., [Grafana Cloud](https://grafana.com/products/cloud), [Grafana Mimir](https://grafana.com/oss/mimir)) often bill based on active series, so one bad high-cardinality label can increase costs dramatically.

## Quick reference — Good vs Bad label choices

| Label type | Typical cardinality | Example | Use in metrics? |
| - | -: | - | - |
| Bounded, categorical | Low | `provider="Stripe"` | Yes — use for dimensions you will slice by |
| Low-cardinality ID | Low–medium | `region="us-west-2"` | Yes — if limited and meaningful |
| High-cardinality identifier | Very high | `customer_id="cust_90210"` | No — keep in logs/traces |
| Per-request unique values | Extremely high | `transaction_id="txn_a8f3e9"` | No — move to logs/traces |
| PII or IP addresses | Very high / sensitive | `email="user@example.com"` or `ip="10.0.0.1"` | No — avoid for privacy and cardinality reasons |

## Concrete mitigation strategies

* Aggregate at instrumentation time: record counters with only bounded labels (e.g., aggregate per provider/result) and compute per-customer metrics offline if needed.
* Use logs or tracing for unique identifiers: put `customer_id`, `transaction_id`, IPs, emails, and UUIDs in structured logs or traces, not metric labels.
* Use Prometheus relabeling to drop high-cardinality labels before ingestion (e.g., `relabel_configs` in scrape configs).
* Employ recording rules to precompute and store useful aggregations rather than creating many granular series.
* Use exemplars (OpenMetrics/OpenTelemetry) to link individual metric values to traces without adding the trace ID as a label.
* Test and measure: deploy a version of your exporter or service with and without the label and compare active series count, RAM usage, and billing.

Avoid adding per-entity identifiers (customer IDs, transaction IDs, emails, IPs, UUIDs) as metric labels. These values belong in logs or tracing systems — adding them to metrics will likely blow up cardinality and can cause outages or huge costs.

## Example cardinality calculation

* Base series: 3 providers × 2 currencies × 3 results = 18 series
* With 1,000 customers: 18 × 1,000 = 18,000 series
* With 1,000,000 customers: 18 × 1,000,000 = 18,000,000 series

This linear multiplication is why adding an unbounded label multiplies series count by the number of distinct values.

## How to validate in your environment

You can deploy two versions of the same API — one that uses only bounded labels and one that includes an unbounded label — and observe the impact:

* Monitor active series count (Prometheus `prometheus_tsdb_head_series` or your managed provider's metrics).
* Track Prometheus memory usage and CPU.
* Compare query latency for common dashboards.
* Review billing metrics if using a managed metrics backend.

## Links and references

* [Prometheus: Best practices for designing metrics](https://prometheus.io/docs/practices/naming/)
* [Grafana Cloud](https://grafana.com/products/cloud)
* [Grafana Mimir](https://grafana.com/oss/mimir)

You’ll get the best long-term results by preferring bounded labels for metrics and keeping per-entity uniqueness in logs or traces. Deploy, measure, and enforce labeling guidelines to avoid cardinality disasters.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/grafana-prometheus-for-metrics-driven-observability/module/90070254-6cda-4097-b5a2-5c7c271512ee/lesson/db5fc4d1-ae33-41b8-9141-45a6769ace13)

> [card] [Practice Lab](https://learn.kodekloud.com/user/courses/grafana-prometheus-for-metrics-driven-observability/module/90070254-6cda-4097-b5a2-5c7c271512ee/lesson/ef3a525a-538f-46ef-b4c8-4a719e76a3fb)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.