Prometheus Query Rate vs Irate Confusion Fix
In this tutorial, you'll learn about Prometheus Query Rate vs Irate Confusion Fix. We cover key concepts, practical examples, and best practices.
Your Prometheus CPU usage graph looks like a staircase instead of a smooth line — rate() produces flattened peaks while irate() shows noisy spikes. The wrong function choice distorts the visual representation and alerting behavior.
The Problem
// A graph of CPU usage shows a staircase pattern
rate(cpu_seconds_total[5m])
The rate() function averages over a 5-minute window — it smooths out spikes and introduces latency. Short CPU bursts are hidden in the average.
Step-by-Step Fix
1. Understand rate vs irate
// rate() — average over the entire window (smoother)
rate(cpu_seconds_total[5m]) // Average per-second rate over 5m
// irate() — based on the last two samples (more responsive)
irate(cpu_seconds_total[5m]) // Instantaneous rate from last two data points
2. Choose based on use case
// Dashboards — use rate() for smooth, readable graphs
rate(http_requests_total[5m])
// Auto-scaling — use rate() with short window for responsive scaling
rate(http_requests_total[1m])
// Spot-checking — use irate() for real-time spikes
irate(cpu_seconds_total[2m])
// Alerting — use rate() to prevent flapping alerts
rate(errors_total[5m]) > 0.01
3. Match window to scrape interval
// Scrape interval: 15s
// Minimum window: 1m (4 data points) — OK for rate()
// Recommended window: 2m-5m for stable results
rate(http_requests_total[1m]) // Uses ~4 data points
rate(http_requests_total[5m]) // Uses ~20 data points — more stable
4. Use increase() for human-readable totals
// increase() is rate() * window — total over the window
increase(http_requests_total[1h]) // Total requests in the last hour
// Equivalent to:
rate(http_requests_total[1h]) * 3600
5. Handle counter reset in all functions
// All three functions handle counter resets automatically
rate(cpu_seconds_total[5m]) // Handles restarts
irate(cpu_seconds_total[5m]) // Handles restarts
increase(errors_total[1h]) // Handles restarts
Expected output:
rate(http_requests_total[5m]) → 150.2 (smooth, average)
irate(http_requests_total[5m]) → 162.0 (responsive, shows current spike)
increase(http_requests_total[1h]) → 540000 (total requests this hour)
Prevention Tips
- Use
rate()for dashboards and alerting — it is more stable - Use
irate()for real-time monitoring of spiky metrics - Use
increase()for human-readable "requests per hour" - Use minimum 2x scrape interval as the rate window
- Never use
rate()orirate()on gauge metrics
Common Mistakes with query rate irate
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world PROMETHEUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro