Recording Rules
What you'll learn: How to use recording rules in Prometheus to pre-compute expensive queries and boost performance.
The Problem with Complex Queries
When you're monitoring thousands of services with Prometheus, some queries become computationally expensive—especially aggregations across many time series with labels. Running these queries repeatedly (say, in a dashboard refreshing every 30 seconds) wastes CPU and slows down your monitoring.
What Are Recording Rules?
Recording rules are pre-computed queries that Prometheus evaluates at regular intervals and stores the results as new time series. Think of them as calculated spreadsheet columns: instead of recalculating a complex formula every time you look at a cell, you compute it once and store the answer.
For example, instead of calculating the average request rate across all instances every time someone loads a dashboard, you can define a recording rule that computes this aggregation every minute and saves it as a new metric.
How They Work
You define recording rules in your Prometheus configuration. Each rule specifies:
- A new metric name for the computed result
- A PromQL expression to evaluate
- Labels to attach to the result
Prometheus evaluates these rules on schedule (typically every evaluation interval), stores the results, and you query the pre-computed metric name instead of the expensive expression.
Benefits
- Faster dashboards: Queries execute in milliseconds instead of seconds
- Reduced load: Less strain on Prometheus during peak usage
- Simplified queries: Complex aggregations become simple metric lookups
- Consistent results: Everyone queries the same pre-computed data
Key Takeaway: Recording rules trade a small amount of storage and continuous computation for dramatically faster query performance—essential for frequently-accessed metrics and real-time dashboards at scale.