# rate, irate and increase — Prometheus + Grafana

Source: https://www.skillbyai.com/en/prometheus-grafana/q-rate

> Turning counters into speeds.

## Per-second rates that handle resets

`rate(counter[5m])` gives the average per-second increase over the window and automatically handles counter resets after restarts. `increase(counter[1h])` gives the total increase over the window (rate times the window length). `irate` uses only the last two samples, which is spiky and better suited to fast-moving graphs than alerts. Use a window of at least four times the scrape interval so each window contains enough samples. Always apply rate **before** aggregating with sum.

## Rates in practice

Common queries.

```promql
# requests per second per instance, averaged over 5 minutes
rate(http_requests_total{job="orders-api"}[5m])

# total requests in the last hour
sum(increase(http_requests_total{job="orders-api"}[1h]))

# wrong order: summing counters first breaks reset handling
# rate(sum(http_requests_total)[5m])   <- avoid
```

## rate then sum, never sum then rate

Summing raw counters hides individual resets, so the resulting rate shows false drops and spikes.

**Quiz:** Why use rate() on a counter instead of graphing it directly?

- [ ] Graphing counters is not allowed
- [ ] Counters are always zero
- [ ] rate() converts gauges to counters
- [x] Counters only grow; rate shows the per-second change and handles resets

*Answer:* Counters only grow; rate shows the per-second change and handles resets. The speed is what matters.
