Skip to content

Lesson 6 of 9 · 26m

Cardinality Death: How Metrics Systems Go Down

Why unbounded labels explode memory and cost, how to find the worst offenders, and the containment tactics that keep Prometheus ingestible.

Every experienced metrics person has a cardinality scar. Mine involved a well-meaning engineer adding request_id to a latency histogram "temporarily for debugging." Within an hour the Prometheus process was consuming memory like a database under a full table scan, queries timed out, and the on-call lost visibility during a real deploy. Nothing crashed because the code was wrong; it crashed because the data model was respected too literally.

The multiplication problem

Cardinality is multiplicative:

3 services × 20 instances × 6 methods × 40 routes × 5 statuses = 72,000 series per metric

Add a histogram with 12 buckets and that becomes roughly 864,000 bucket series, plus count and sum. Add a user ID label with 50,000 users and you are no longer operating a monitoring system; you are operating a random-string archive.

Prometheus keeps active series in memory for fast ingestion and querying. Exact limits depend on version, hardware, compression, and retention, but the operational lesson does not: cardinality growth eventually becomes latency, memory pressure, slow compaction, or death.

Find the real offenders

Start with cardinality by metric name:

topk(20, count by (__name__) ({__name__=~".+"}))

Then inspect labels inside one offender:

count by (route) (http_request_duration_seconds_bucket)
count by (instance) (http_requests_total)

In Grafana or the Prometheus expression browser, compare series count over time. A step change after a deploy is usually instrumentation. A slow leak is often lifecycle churn: instance IDs, pod names, ephemeral containers, or dynamic queue names.

The forbidden label list

Never put these in metric labels:

  • request IDs, trace IDs, span IDs
  • user IDs, emails, session IDs
  • timestamps or dates
  • unbounded URLs or SQL statements
  • exception messages
  • pod names when a stable workload label exists
  • full hostnames of massively ephemeral autoscaling inventory unless you intentionally need instance-level debugging

IDs belong in logs and traces. Metrics are for aggregates. Correlation across signals belongs in exemplars or trace context, not in millions of label values.

Control routes before they control you

A common compromise is route templates:

/v1/users/{user_id}/orders

not

/v1/users/918273/orders

Framework instrumentation often exposes the raw path by default. Configure route templating centrally; a middleware copied per service will drift.

Defensive controls at ingestion

Use scrape limits as blast-radius containment:

scrape_configs:
  - job_name: api
    sample_limit: 20000
    target_limit: 500
    label_limit: 40
    label_name_length_limit: 120
    label_value_length_limit: 512

These limits do not fix bad design; they keep a bad exporter from taking down the server. Alerts should fire on limit hits because dropped samples mean incomplete truth.

metric_relabel_configs can drop toxic metrics or label values after scrape parsing. Use it as a circuit breaker, not as the primary design process.

Recovery is ugly

Deleting high-cardinality data is not always immediate or simple. Depending on deployment, you may use the admin API to delete series, shorten retention, or rebuild storage. If memory pressure prevents startup, you may be deleting blocks manually. This is why prevention matters: cardinality incidents are easy to cause and miserable to reverse.

Field notes

  • Review metric diffs in pull requests like schema migrations.
  • Budget cardinality per service and per metric. "How many series will this create?" is a launch-review question.
  • A single bad histogram is worse than a dozen bad counters because every label combination multiplies across buckets.
  • Keep one low-cardinality debug mode behind a temporary flag if you truly need detailed diagnostics; expire it automatically.

Was this lesson helpful?

Sign in to rate