What is the rate() function in PromQL and when do you use it?
Understand the PromQL rate() function: how it turns counters into per-second rates, handles resets, needs a range vector, and differs from irate().
Expected Interview Answer
rate() calculates the per-second average rate of increase of a counter over a given time range, automatically handling counter resets, and it must be applied to a range vector such as http_requests_total[5m].
Counters only ever go up (until a process restart resets them to zero), so their raw value is meaningless on its own — rate() converts that ever-growing number into a smoothed per-second rate over the window. It extrapolates slightly to the range edges and detects resets, treating a drop as a restart rather than negative growth. Use rate() over a wider window for smoother, alert-friendly trends and irate() for the last two samples when you need fast-reacting, high-resolution graphs. rate() only makes sense on counters, never on gauges.
- Turns monotonically increasing counters into a meaningful per-second rate
- Automatically compensates for counter resets on restarts
- Smooths noisy data over the chosen window
- Foundation for latency, throughput and error-rate queries
- Enables reliable alerting thresholds
AI Mentor Explanation
A counter is like a batter's cumulative career runs — always climbing, so the raw total tells you little about current form. rate() is like computing runs-per-over across the last five overs: it converts the ever-rising tally into a live scoring rate. And if the scoreboard is reset for a new innings, rate() is smart enough to treat that drop as a fresh start, not as the batter losing runs.
Step-by-Step Explanation
Step 1
Start with a counter
rate() only applies to counters — monotonically increasing metrics like http_requests_total.
Step 2
Provide a range vector
Append a duration such as [5m] so rate() has a window of samples to work over.
Step 3
Compute per-second increase
rate() averages the increase across the window and divides by seconds, yielding a per-second rate.
Step 4
Handle resets automatically
rate() detects counter resets (drops to a lower value) and treats them as restarts, not negative growth.
Step 5
Aggregate as needed
Wrap with sum by (...) to combine rates across instances for dashboards and alerts.
What Interviewer Expects
- Knows rate() needs a range vector, not an instant vector
- Explains that rate() applies to counters, not gauges
- Understands automatic counter-reset handling
- Distinguishes rate() from irate()
- Can pick an appropriate window for smoothing vs responsiveness
Common Mistakes
- Applying rate() to a gauge
- Passing an instant vector instead of a range vector
- Choosing a window shorter than 2x the scrape interval
- Expecting rate() to show a negative value on restart
- Confusing rate() with irate() for alerting
Best Answer (HR Friendly)
“rate() takes a number that only ever counts upward — like total requests served — and tells you how fast it's growing per second over a chosen time window. It's handy because that raw ever-increasing number isn't useful by itself, and rate() even copes gracefully when a service restarts and the counter resets to zero.”
Code Example
rate(http_requests_total[5m])sum by (service) (
rate(http_requests_total[5m])
)irate(http_requests_total[1m])Follow-up Questions
- How does rate() differ from irate()?
- Why must rate() be applied to a range vector?
- How does rate() handle counter resets?
- How do you choose the window size for rate()?
- Why should you sum(rate(...)) and not rate(sum(...))?
MCQ Practice
1. rate() must be applied to which of the following?
rate() needs a range vector (e.g. metric[5m]) to have a window of samples to compute over.
2. How does rate() handle a counter reset on restart?
rate() detects the reset and treats it as a restart rather than negative growth.
3. Which is best for smooth, alert-friendly trends?
rate() over a wider window smooths noise, making it suitable for alert thresholds; irate() is spiky.
Flash Cards
What does rate() compute? — The per-second average rate of increase of a counter over a range.
What input does rate() require? — A range vector, e.g. metric[5m] — never an instant vector.
Which metric type suits rate()? — Counters only, never gauges.
How does rate() treat a reset? — It detects the drop as a restart and compensates, never reporting negative growth.
rate() vs irate()? — rate() averages over the window (smooth); irate() uses the last two samples (responsive).