Lesson 4 of 9 · 25m
Cardinality: The Bill You Pay Per Label
Diagnose series explosion, calculate cardinality cost, and enforce label discipline before one deployment multiplies your storage and query latency.
Every observability platform eventually has the same incident: metrics ingest doubles after a harmless-looking deployment, queries slow down, and someone discovers a label named user, request, path, or container_id. Cardinality is not an abstract efficiency concern. It is memory, disk, compaction time, query latency, remote-write bandwidth, and money.
Count series before you count samples
A Prometheus series is roughly a label-set identity plus samples over time. If you scrape 100,000 series every 15 seconds, that is about 6,600 samples per second before replication and remote write. Add one label with ten values and you may multiply series by ten if every combination appears.
A simple multiplication model helps:
services 20
routes 40 per service
methods 4
status classes 5
instances 10
--------------------------------------------
potential 20 * 40 * 4 * 5 * 10 = 160,000 seriesNot every combination exists, but distributions are rarely kind. The worst label is not always the one with the most theoretical values; it is the one whose values occur with many combinations of the others.
Find the offenders
Prometheus itself exposes useful hints:
topk(20, count by (__name__)({__name__=~".+"}))
topk(20, count by (job)({__name__=~".+"}))
sort_desc(count by (route)(http_requests_total))In TSDB-heavy environments, inspect head cardinality and churn:
prometheus_tsdb_head_series
rate(prometheus_tsdb_head_series_created[1h])
rate(prometheus_tsdb_head_series_removed[1h])Creation without matching removal means churn: series are being born constantly, often because instance, pod, request, or build labels change.
The usual sins
The repeat offenders are boringly consistent:
label value source why it explodes
raw URL path /users/123, /users/124, every slug
user/account ID one series per customer
session/request ID infinite series churn
container or pod UID new identity every rollout
build SHA new series family each deploy
exception message unbounded textNormalize paths at the router boundary. /users/{id} is a fine label value if the route set is bounded. /users/12345 is not.
Drop or rewrite before ingestion when appropriate
Prometheus metric relabeling can drop dangerous series at scrape time:
metric_relabel_configs:
- source_labels: [__name__]
regex: "go_memstats_.*"
action: drop
- source_labels: [route]
regex: "/users/[0-9]+"
target_label: route
replacement: "/users/{id}"Be careful. Rewriting at scrape time can paper over broken instrumentation and make different teams disagree about what the service actually exports. Prefer fixing the instrumentation library. Use relabeling as a guardrail and migration mechanism, not as the primary design.
Budget cardinality like production capacity
Set explicit budgets per team or service. A starting point might be 10,000 active series per application instance family, adjusted for platform size. What matters is not the exact number; it is that a developer can answer, before launch, how many combinations a new label creates.
Use CI checks for exported metrics where possible. Capture /metrics from integration tests and fail if forbidden label names or unbounded-looking values appear.
Aggregation is not deletion of evidence
Operators resist dropping labels because they fear losing debugging ability. The answer is placement, not unlimited retention in metrics. Request-level detail belongs in traces and logs. Metrics should preserve bounded operational dimensions: service, route template, method, status class, region, shard.
Field notes
- Cardinality problems often appear hours after deployment as traffic reaches unique combinations, not immediately at startup.
countqueries themselves can be expensive on a bloated TSDB; diagnose during low-traffic periods.- Kubernetes pod labels are useful for scrape lifecycle but dangerous as retained metric dimensions.
- Do not drop data silently. Record dropped or relabeled series counts so operators know the guardrail fired.
- One high-cardinality metric can dominate an otherwise disciplined service.
Was this lesson helpful?