Micrometer Metrics: Counters, Timers, and Gauges in Spring Boot
Fast recall
Quick revision
Core answer
Practical knowledge of Micrometer metric types and choosing the right instrument for each use case.
Using a Gauge for something like processed orders (which should be a Counter), or using a Counter for queue depth (which should be a Gauge).
Explain DistributionSummary for non-time values and percentile histograms for SLO-based alerting.
Interview-ready guide
Interview answer
Micrometer provides three primary instrument types for Spring Boot metrics — Counter, Timer, and Gauge — and choosing the right one matters because the wrong type produces dashboards that can't answer the questions you need to ask.
Counter — for things that only go up
- A Counter tracks a cumulative count that only ever increases — total orders placed, total payment failures, total cache misses; you never reset a Counter
- In dashboards, you query the rate of change (
rate(counter[5m])in Prometheus) not the raw value — which means a Counter of 10,000 orders tells you nothing until you ask "how many orders per minute in the last 5 minutes?" Counter.builder("orders.placed").tag("status", status).register(meterRegistry).increment()
Timer — for duration and count together
- A Timer records both the duration of each operation and the count of operations — which means one Timer instrument gives you request rate, average latency, and latency percentiles (P50, P95, P99) all in one
Timer.builder("payment.processing").register(meterRegistry).record(() -> paymentService.process(payment))wraps the call and records its duration automatically- Spring Boot's Actuator auto-creates a Timer for every HTTP endpoint —
http.server.requeststagged withuri,method, andstatus; you get latency distributions without writing any code
Gauge — for values that go up and down
- A Gauge reports the current value of something that fluctuates — queue depth, active connections, cache size, JVM heap percentage
Gauge.builder("queue.depth", queue, Queue::size).register(meterRegistry)reports the current queue size every time Micrometer scrapes the metric- Use Gauge for current state; use Counter for cumulative counts; never use Gauge for things that only go up (use Counter for those)
Tags create dimensions — tag("service", "payments") and tag("env", "prod") let dashboards filter and group metrics without separate namespaces.
Micrometer Metrics: Counters, Timers, and Gauges in Spring Boot
Direct Answer
Counters track cumulative occurrences (events that only go up), timers measure duration and throughput of operations, and gauges measure the current value of something that can go up or down.
Common Mistakes & Pitfalls
Using a Gauge for something like processed orders (which should be a Counter), or using a Counter for queue depth (which should be a Gauge).
Check your understanding
Answer these without looking back at the article.
- What is the difference between `publishPercentiles` and `publishPercentileHistogram` in Micrometer?
- How does Micrometer differ from Prometheus client_java?
- How would you track the size of a cache using Micrometer?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.