How to Use Recording Rules in Prometheus to Reduce Load and Speed Up Queries

June 3, 2025 · prometheus, monitoring, observability, recording-rules, performance-optimization

I remember the first time my Prometheus server groaned under the weight of a complex Grafana dashboard—it felt like asking a toddler to solve calculus. Dashboards loaded slower than my morning coffee brewed, and my CPU metrics looked like a stress test. That’s when I discovered recording rules, Prometheus’s secret weapon for taming expensive queries. Here’s how to use them effectively.

Why Recording Rules Matter

Recording rules let you precompute frequently used or resource-intensive queries and save the results as new time series. This means:

Think of it like meal-prepping for your monitoring system—do the heavy lifting once, then enjoy quick “servings” of metrics later.


Prerequisites

Before diving in, ensure you have:

  1. A running Prometheus server (v2.0+).
  2. Basic familiarity with PromQL (Prometheus Query Language).
  3. Access to your Prometheus configuration file (prometheus.yml).

Step 1: Plan Your Recording Rules

Start by identifying slow or repetitive queries. For example:

Pro Tip: Check Prometheus’s /targets and /metrics endpoints for high-cardinality or expensive metrics.


Step 2: Define Rules in a Separate File

Prometheus loads rules from files specified in prometheus.yml. Create a new file (e.g., recording_rules.yml) and add your rules:

groups:
  - name: http_requests_rules
    interval: 1m  # How often to evaluate rules (optional)
    rules:
      - record: instance_path:http_requests:rate5m
        expr: rate(http_requests_total{job="myapp"}[5m])

Key Fields:


Edit prometheus.yml to include your rules file:

rule_files:
  - 'recording_rules.yml'  # Path relative to prometheus.yml

Reload Prometheus gracefully to apply changes:

curl -X POST http://localhost:9090/-/reload

Warning: Avoid hot-reloading in production during peak traffic!


Step 4: Verify and Use Your New Metrics

  1. Check if the rule is active:
    curl http://localhost:9090/api/v1/rules | jq '.data.groups[]'
    
  2. Query the new metric in Grafana or Prometheus’s UI (e.g., instance_path:http_requests:rate5m).

Troubleshooting:


Advanced Tips

  1. Optimize Intervals: Balance precision and load:
    interval: 5m  # For slower-changing metrics like daily totals.
    
  2. Avoid Cardinality Explosions: Never record high-cardinality labels (e.g., user_id).
  3. Chain Rules: Precompute base metrics first, then build atop them:
    - record: job:http_errors:percent
      expr: instance_path:http_errors:rate5m / instance_path:http_requests:rate5m * 100
    

FAQ

Q: How do recording rules differ from alerting rules?

A: Alerting rules evaluate conditions to trigger alerts; recording rules create new time series. Both use the same YAML structure.

Q: Can recording rules backfill historical data?

A: No—they only compute data from the point of creation onward. For backfilling, use tools like Thanos or Cortex.

Q: What’s the performance impact of too many rules?

A: Each rule adds CPU/memory overhead. Monitor prometheus_rule_evaluations_total to avoid overloading your server.


Further Reading

Now go forth and precompute! Your Prometheus server will thank you with snappier dashboards and cooler temps (both literally and metaphorically).

Building something like this in production?

I help teams turn setups like this into reliable, monitored infrastructure.

Get a free consulting call

Get my monitoring stack checklist

The exact checklist I use when setting up observability for a new team. No spam, unsubscribe anytime.