Skip to content

Prometheus Query Rate vs Irate Confusion Fix

DodaTech Updated 2026-06-24 3 min read

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() or irate() on gauge metrics

Common Mistakes with query rate irate

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. 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

### What happens if the rate window is too small?

With rate(metric[30s]) and a 15s scrape interval, you get only 2 data points. A single missed scrape causes no data. Use at least 4x the scrape interval (1m for 15s scrape) for reliable results.

Can I use irate() for alerting?

Yes, but be careful. irate() is sensitive to individual sample variations — it can cause flapping alerts. Use irate() with a for: duration to prevent flapping, or use rate() with a short window for more stable alert conditions.

Why does rate() sometimes show fractional values for integer counters?

Counters increment by integer values, but rate() computes per-second averages. If a counter increases by 1 over 5 minutes, rate() shows 0.0033 — this is correct. Use increase() if you want the total integer increase over the window.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro