> For the complete documentation index, see [llms.txt](https://teamsmiley.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://teamsmiley.gitbook.io/devops/en/monitoring/prometheus/rate-vs-irate.md).

# Rate vs Irate

## counter function

Before looking at the rate function, let's understand the counter function.

* Can be used with counter metrics.
* Using sum, count, bucket (summary, histogram) etc. results in a counter metric.
* Always increases.
* The raw value itself is not meaningful.
* Therefore, functions are used to derive gauge values for actual use.
* If there is only one sample in the time series within the given range, the counter function produces no output.

## rate function

Returns a value representing how fast a counter is increasing per second within each time series in the given range vector.

The \[5m] range vector means a 5-minute window.

Say there are five data points over 5 minutes: (1, 2, 3, 4, 5) => these are all summed, averaged, and converted to a per-second rate of change.

For data like \[5, 10, 4, 6] => it is processed as \[5, 10, 14, 16] (because the counter must always increase... though why 14 is not entirely clear — does it keep adding to 10?).

If the counter increases by 10 per second but runs for only 30 minutes, using 1 hour as the range will give a value of 5, because it is averaged.

The vector range is recommended to be at least 4 times (usually 5 times) the scrape interval. If scraping every 1 minute, a 5-minute vector is recommended.

It is recommended to use the same range for all rate functions within Prometheus, for consistent analysis of data.

The rate function does not provide exact data. If you need exact data, use a log-based analysis system.

## irate function

Works the same way as the rate function. The difference is how the range is calculated.

It only uses the last 2 values of the passed vector. It does not average over the entire range. It clearly shows the instantaneous rate of change. With rate, peak and trough values get smoothed out due to averaging, but irate uses only the last 2 values, so peaks and troughs are more prominent than with rate.

There is no need to worry about the vector range and scrape interval. - This is an advantage.

When using irate, if the query range step is larger than the scrape interval, data points will be skipped.

I remember setting irate to 30 seconds or 1 minute when scraping node exporter every minute, and seeing no graph on the node exporter dashboard as a result. Tracking this down took about 6 months. If graphs are not appearing in the node exporter dashboard, check the scrape interval and the irate range.

irate is highly volatile because it calculates using only the last 2 values.

Since it is sensitive to large changes, it is probably best not to use it for alerts.
