Skip to main content
← PROBLEMS / ENGINEERING
An end-to-end domain learning journey

Controlling Prometheus
High-Cardinality Metrics.

Unbounded labels (user_id, trace_id, raw URL paths) multiplied by 18 histogram series per tuple exploded active series from 420K to 18.4M, OOM-killing Prometheus every 9 minutes.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

To keep the architecture evolvable without sacrificing reliability, drop offending series at scrape time via `metric_relabel_configs`, enforce `sample_limit`, normalize routes to templates (`/orders/{id}/items`), and move per-user/trace dimensions to logs and traces.

TL;DR30-second simplified summary
▾

Core Mechanics: Prometheus stores every unique (metric_name, label_set) as an independent time series in its in-memory TSDB head block (~5-8 KB RAM per active series).

Visual & Structural Flow: Depicts Unbounded Labels (18.4M Series) vs. Relabeling + Route Templates (App Pods (50) -> Scrape Relabeler -> TSDB Head (64GB)); Low-Cardinality Metrics vs. High-Cardinality Logs & Traces (HTTP Middleware -> Scrape Guard -> Prometheus TSDB -> Logs / OTel Traces).

Trade-Offs & Production: Two common mistakes prolong the outage: Using action: labeldrop on user_id: Stripping user_id merges 1,000 distinct series with identical timestamps into one series. Stabilize Prometheus immediately and prevent future regressions: Immediate Stabilization: Add metric_relabel_configs (action: drop on http_server_requests_seconds.*.

Failure Modes & Application: Cardinality limits protect every production observability platform: Multi-Tenant Prometheus / Mimir / Thanos: Enforce per-tenant max_series_per_metric and sample_limit at the distributor.

Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why Prometheus TSDB cannot survive unbounded label cardinality.

Why this matters: The mechanism: understand how Why Prometheus TSDB cannot survive unbounded label cardinality works from first principles.

Think of Why Prometheus TSDB cannot survive unbounded label cardinality as the intuition layer for Controlling Prometheus High-Cardinality Metrics: The mechanism: understand how Why Prometheus TSDB cannot survive unbounded label cardinality works from first principles.

Prometheus stores every unique (metric_name, label_set) as an independent time series in its in-memory TSDB head block (~5-8 KB RAM per active series). Worse, a 15-bucket histogram emits 18 series (15 le buckets + +Inf + _sum + _count) per label tuple—so a single unbounded label like user_id or raw path multiplies memory linearly into an instant OOM.

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

18.4M active series, 92 GB RAM demand, and WAL-replay OOM loops.

Why this matters: The problem: see what breaks without 18.4M active series, 92 GB RAM demand, and WAL-replay OOM loops under real concurrency or scale.

With 1,022,000 unique (user_id, trace_id, path) tuples × 18 histogram series = 18.4M active series:

  1. 92 GB Head Block: Exceeds the 64 GB pod limit, triggering Exit Code 137 every 9 minutes during head-block compaction.
  2. WAL Replay OOM Loop: Even after fixing app code, Prometheus crashes on restart replaying the poisoned 18M-series WAL from disk.
  3. Alert Blindness: Grafana and Alertmanager go completely dark during an active outage.
Cardinality Explosion & Recovery

Unbounded Labels (18.4M Series) vs. Relabeling + Route Templates

App Pods (50)Scrape RelabelerTSDB Head (64GB)1. 1M tuples x 18 buckets = 18.4M(92GB OOM!)2. action: drop bad metric + wipepoisoned WAL3. Fix: route=/orders/{id}/items +UNMATCHED4. sample_limit: 50000 -> 720Kseries (3.6GB)

Dropping dynamic labels and normalizing routes shrinks active series by 96% (from 18.4M to 720K / 3.6 GB).

Engineering Decision Framework
Problem
How to implement Controlling Prometheus High-Cardinality Metrics without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
To keep the architecture evolvable without sacrificing reliability, drop offending series at scrape time via metric_relabel_configs, enforce sample_limit, normalize routes to templates (/orders/{id}/items), and move per-user/trace dimensions to logs and traces.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Controlling Prometheus High-Cardinality Metrics experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Why `labeldrop` alone causes duplicate-sample collisions.

Why this matters: The problem: see what breaks without Why labeldrop alone causes duplicate-sample collisions under real concurrency or scale.

Two common mistakes prolong the outage:

  • Using action: labeldrop on user_id: Stripping user_id merges 1,000 distinct series with identical timestamps into one series, causing duplicate sample for timestamp ingestion errors; you must action: drop the broken metric until the app is fixed.
  • Ignoring 404 Scanner Paths: Vulnerability scanners hitting /wp-login.php or /admin/123 create millions of path values unless unmatched routes map to route="UNMATCHED".
Request & State Flow

Why `labeldrop` alone causes duplicate-sample collisions

Control PlaneScheduler / IngressWorker NodePod / Container1. Apply desired replica & PDBspec2. Bind pod respecting CPU/memlimits3. Run readiness probe beforetraffic4. Drain in-flight connections onrollout
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise solution: Scrape-Time Guardrails + Route Normalization.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Stabilize Prometheus immediately and prevent future regressions:

  • Immediate Stabilization: Add metric_relabel_configs (action: drop on http_server_requests_seconds.* temporarily), archive/wipe the poisoned wal/ directory, and set sample_limit: 50000 per scrape target so one bad deploy never OOMs the server.
  • Bounded Instrumentation: Use framework route templates (40 routes × 5 status_classes × 4 methods × 50 pods × 18 = 720,000 series ≈ 3.6 GB), map 404s to UNMATCHED, and move user_id / trace_id to structured logs and OpenTelemetry traces (or exemplars).
Observability Signal Split

Low-Cardinality Metrics vs. High-Cardinality Logs & Traces

HTTP Middlewareroute=/orders/{id},…Scrape Guardsample_limit: 50000 +…Prometheus TSDB720K series (~3.6 GB…Logs / OTel Tracesuser_id, trace_id,…trace_id via exemplar

Keeping Prometheus labels bounded to route/method/status_class cuts RAM 96% while exemplars link to traces.

scrape_configs:
  - job_name: 'order-service'
    sample_limit: 50000  # Reject runaway targets before TSDB OOMs
    metric_relabel_configs:
      # Emergency drop while broken app version rolls out
      - source_labels: [__name__]
        regex: 'http_server_requests_seconds.*'
        action: drop
05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where metric cardinality governance is applied.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Cardinality limits protect every production observability platform:

  • Multi-Tenant Prometheus / Mimir / Thanos: Enforce per-tenant max_series_per_metric and sample_limit at the distributor.
  • OpenTelemetry Exemplars: Attach trace_id as an exemplar metadata sample on histogram buckets instead of as a metric label.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why does `action: labeldrop` on `user_id` often fail as an emergency Prometheus fix, and what should you do instead?

Recommended Learning Flow