Skip to content

Prometheus recording rule strategy — what to pre-compute, what to leave at query time, and a cardinality budget

A reference for which queries belong in recording rules, which belong in dashboards, and how to size a recording rule cardinality budget so the Prometheus doesn't outgrow the VM it sits on.

Your Grafana dashboards have started loading slowly. The SLO panel takes seven seconds, the latency heatmap takes twelve. Someone suggests “more recording rules” and a week later you have 400 of them, the rule evaluation duration is exceeding the scrape interval, and the active series count has doubled. Recording rules are a real performance tool, but they’re not free — every rule produces new series, and treating them as “the answer to slow queries” without a budget is how Prometheus deployments grow until they fall over.

What’s happening

A recording rule pre-computes a PromQL expression on a schedule (typically every 30s) and stores the result as a new series. Three legitimate reasons to add one:

  1. The dashboard query is genuinely too slow. A histogram_quantile over 5M-sample-bucket-series-per-second is computed by Prometheus during the dashboard load. Pre-computing it once every 30 seconds and writing one series per service costs less than computing it 12 times an hour from raw data.
  2. The query is reused across many alerts/dashboards. A complex error-budget computation that appears in 40 places should be one recording rule named explicitly, not 40 copies of a 6-line PromQL block.
  3. You need to federate the result to another Prometheus or send it to long-term storage. Recording rules with namespaced names (team_payments:slo:error_ratio:5m) are the federation interface.

Three illegitimate reasons people add them:

  • “It might be useful eventually.” Speculative pre-computation is how cardinality budgets get blown.
  • “Dashboards are slow.” First, profile — slow dashboards are usually caused by un-bounded time ranges, missing instant-query usage, or queries scanning every label. Recording rules treat the symptom.
  • “We had time on a Friday and refactored everything into rules.” The cost of a recording rule is a permanent ongoing tax. Refactor the queries first, not the storage.

The output of every recording rule is itself a metric with its own cardinality. A rule that says sum by (service, region, status, customer_tier) (rate(http_requests_total[5m])) produces a series for every combination — services × regions × statuses × tiers. If those are 20 × 4 × 8 × 5 you’ve added 3,200 series per evaluation. Multiply by your rule count.

How to verify

Top contributors to recording rule cardinality:

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

This selects series whose name contains a colon (the convention for recording rules). The output tells you which rules are producing the most series.

Rule evaluation duration:

prometheus_rule_evaluation_duration_seconds{quantile="0.99"}

If any rule group’s p99 exceeds the group’s interval, evaluation is falling behind and you’ll see warning-level logs. The fix is not more CPU; it’s fewer or simpler rules.

Total active series vs. total recording-rule-produced series:

sum(prometheus_tsdb_head_series) / on() group_left
  count({__name__=~".+:.+"})

If more than 30-40% of your active series come from recording rules, you’ve over-rolled-up. Push some back to query time.

Rule group missed evaluations:

increase(prometheus_rule_evaluation_failures_total[1h])
increase(prometheus_rule_group_iterations_missed_total[1h])

Any value above zero means evaluations are timing out or being skipped — usually the rule expression is too expensive for its interval.

The strategy

Pre-compute at recording-rule time:

  • Histograms collapsed to percentiles. histogram_quantile(0.99, ...) over thousands of bucket series is the canonical “compute once, read many” case.
  • SLO/error-budget rollups. The error-rate formula is reused everywhere; record it.
  • Cross-job aggregates that combine multiple metrics with arithmetic. The combined formula is recorded once.
  • Anything used in alerts. Alert rules should evaluate cheap, recorded expressions — alert evaluation latency is the latency to know about an incident.
  • Anything federated to another Prometheus. Federation transports only the published rules.

Leave at query time:

  • Ad-hoc filtering. rate(http_requests_total{handler="/api/v1/users"}[5m]) written in a dashboard panel — the dashboard scopes the matcher; pre-computing every possible matcher combination is what blows cardinality.
  • Short-window views. If you only ever query last-15-minutes, the raw data is right there and Prometheus is fast on a 15-minute window.
  • One-off investigative queries. Don’t bake an incident-investigation query into a rule unless it’s used outside the incident.
  • Anything that involves a label you might add tomorrow. Adding a label to a recording rule is a backfill problem.

Use instant queries (not range queries) for single-value panels. Grafana defaults to range queries even for stat panels; switching to instant on stat/single-stat panels alone can drop dashboard load time by 50% without changing storage.

The cardinality budget

For a single Prometheus server on a typical cloud VM (8 vCPU, 32GB RAM, NVMe):

  • Total active series: 2-5 million is a comfortable working range. 10M is the upper end before you need to shard or move to a denser engine.
  • Recording-rule-produced series: ≤ 30% of total. If you’re above, you’ve pre-computed too aggressively.
  • Single recording rule output: ≤ 50K series. Above that, the rule itself is dangerous — one bad label and you’ve doubled your Prometheus’s working set.
  • Rule evaluation duration p99: ≤ 25% of the group’s evaluation interval. p99 = 8s on a 30s interval is fine. p99 = 25s on a 30s interval means you’ll start missing evaluations under any added load.

When a budget is going to be exceeded, the conversation is: drop a label from the rule, raise the interval (60s instead of 30s for non-alerting rules), or split a rule that’s aggregating too many dimensions into multiple narrower rules.

Naming convention that scales

Prometheus’s recording rule naming pattern is level:metric:operations. We use:

  • level — the aggregation level: instance, job, cluster, service, team_<name>. Tells you what was kept; everything else was summed/averaged.
  • metric — the underlying metric name, possibly shortened: http_requests, request_latency, cpu_usage.
  • operations — what was done: rate5m, p99, sum, error_ratio.

Examples:

  • service:http_requests:rate5m — per-service request rate over 5min
  • service:request_latency:p99_5m — per-service p99 latency over 5min
  • team_payments:slo:error_ratio:5m — team-namespaced SLO error ratio for federation

Without a convention, six months in nobody knows what requests_per_second means, what dimensions are kept, or whether it includes errors.

What to actually do this week

  1. Run the topk(20, count by (__name__) ({__name__=~".+:.+"})) query and look at your existing rules. Half of them are probably either unused or producing more cardinality than they save.
  2. Audit which rules are referenced by which alerts and which dashboards. Anything with zero references is a deletion candidate.
  3. Set the budget numbers above as alerts on prometheus_tsdb_head_series per server. The alert fires at 80% of budget so you have a conversation, not a fire.
  4. Adopt the naming convention. Renaming existing rules is annoying but cheaper than living with naming chaos forever.

If you want a recording rule strategy that’s planned alongside federation, cardinality budgets, and long-term storage — not bolted on after the dashboards got slow — our monitoring + on-call team builds that under environment management. Book a rule audit.