What is the difference between a counter and a gauge in Prometheus?
Learn the difference between a Prometheus counter and gauge, when to use each, why counters need rate(), and clear examples for both metric types.
Expected Interview Answer
A counter is a cumulative metric that only ever increases (or resets to zero on restart), while a gauge is a metric that can move both up and down to reflect a current value.
Counters model monotonic totals such as requests handled, errors, or bytes sent, and you almost always query them through rate() or increase() rather than reading the raw number. Gauges model instantaneous levels such as memory usage, temperature, active connections, or queue depth, and you read them directly. The give-away is direction: if the value can legitimately drop, it must be a gauge, not a counter.
- Counters make per-second rates and growth trends trivial
- Counters survive restarts because rate() handles resets
- Gauges naturally express current, fluctuating state
- Gauges can be read directly without a rate function
- Picking correctly prevents misleading dashboards and alerts
AI Mentor Explanation
A counter is like the team's cumulative run total on the scoreboard, which only ever climbs as runs are added and never falls back during the innings. A gauge is like the current required run rate, which rises and falls ball by ball as the chase tightens or eases. Ask about direction: runs scored can only go up, so it's a counter; run rate moves both ways, so it's a gauge.
Step-by-Step Explanation
Step 1
Ask about direction
Determine whether the value can ever legitimately decrease; if yes, it must be a gauge.
Step 2
Model totals as counters
For monotonic totals like requests or errors, declare a counter and only inc() it.
Step 3
Model levels as gauges
For fluctuating levels like memory or connections, declare a gauge and set/inc/dec it.
Step 4
Query counters with rate()
Never alert on a raw counter value; wrap it in rate() or increase() over a window.
Step 5
Read gauges directly
Gauges are meaningful as-is, so dashboards and alerts can use their current value.
What Interviewer Expects
- Monotonic vs bidirectional distinction stated clearly
- Knowing rate()/increase() apply to counters
- Correct real examples for each type
- Awareness that counters reset to zero on restart
- Not reading a raw counter value in an alert
Common Mistakes
- Using a counter for a value that can decrease
- Alerting on a counter's raw value instead of its rate()
- Using a gauge for a monotonic total and losing rate semantics
- Assuming a counter reset means data loss rather than a handled event
Best Answer (HR Friendly)
“A counter is a number that only goes up, like a total of how many requests a service has handled. A gauge is a number that can go up and down, like how much memory is being used right now. If the value can ever drop, it's a gauge.”
Code Example
from prometheus_client import Counter, Gauge
# Counter: only ever increases
requests = Counter('http_requests_total', 'Total HTTP requests')
requests.inc()
# Gauge: can go up and down
connections = Gauge('active_connections', 'Currently open connections')
connections.inc() # a client connected
connections.dec() # a client disconnected
connections.set(42) # or set an absolute valueFollow-up Questions
- Why do you query counters with rate() instead of reading them directly?
- What happens to a counter when the process restarts?
- Give an example where using a counter instead of a gauge would be wrong.
- Can a gauge be used to track a total? What breaks if you do?
- How does increase() differ from rate() for counters?
MCQ Practice
1. Which statement best describes a Prometheus counter?
A counter is monotonic: it only ever goes up, resetting to zero on process restart.
2. Which is the correct metric type for 'currently active database connections'?
Active connections rise and fall over time, so a gauge, which can move both ways, is correct.
3. How should you typically alert on http_requests_total?
Counters are cumulative, so rate() over a window gives the meaningful per-second signal for alerts.
Flash Cards
Counter — Monotonic metric that only increases, resetting to 0 on restart; queried via rate().
Gauge — Metric that can go up and down, representing a current level like memory or connections.
Counter example — http_requests_total, errors_total, bytes_sent_total.
Gauge example — active_connections, memory_bytes, queue_depth, temperature.