Sub topic 15
Monitoring caches with Actuator and Micrometer
This chapter assumes you can run a Spring Boot application, use @Cacheable on a service method, and edit application.properties. You also need spring-boot-starter-actuator on the classpath. Here you will learn how to see which caches exist and how well they perform.
Why monitor a cache
A cache that is never hit costs memory and adds complexity without helping. A cache that is hit often saves work. You only know which one you have if you measure it. The key number is the hit ratio, which is hits divided by total lookups.
Spring Boot reports cache activity through Micrometer, the metrics library that Actuator uses. When a CacheManager is auto-configured and a MeterRegistry is available, Boot binds each cache to the registry. No extra code is needed.
Exposing the endpoints
Actuator exposes only health over HTTP by default. Add the endpoints you want:
management.endpoints.web.exposure.include=health,caches,metrics
# Then inspect:
# GET /actuator/caches
# GET /actuator/metrics/cache.gets?tag=result:hit
# GET /actuator/metrics/cache.gets?tag=result:missTreat these endpoints as internal. Do not expose them publicly without authentication.
Listing caches
GET /actuator/caches returns every cache the application knows about, grouped by cache manager. For the shop example, the response looks like this:
{
"cacheManagers": {
"cacheManager": {
"caches": {
"products": {
"target": "com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalManualCache"
},
"productSearch": {
"target": "com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalManualCache"
}
}
}
}
}This is a quick check that your configuration took effect. It confirms the cache names and the provider in use. The endpoint also supports DELETE /actuator/caches/products to clear one cache, which is useful during incident response.
Reading hit and miss counters
Micrometer publishes cache metrics under the cache.* prefix. The most useful ones are:
| Metric | Meaning |
|---|---|
| `cache.gets` | Lookups, tagged `result:hit` or `result:miss` |
| `cache.puts` | Entries added |
| `cache.evictions` | Entries removed by the provider's policy |
| `cache.size` | Approximate number of entries |
Every metric carries a cache tag with the cache name. To get the hits for products, call:
GET /actuator/metrics/cache.gets?tag=result:hit&tag=cache:productsCompute the hit ratio as hit / (hit + miss). A ratio close to 1 means the cache is doing its job. A low ratio suggests that keys are too specific, that entries expire too soon, or that the data is not worth caching.
Enabling statistics for Caffeine
Caffeine records statistics only when asked. Without recordStats, cache.gets and cache.evictions stay at zero while cache.size still works. Use the spec from the Caffeine chapter:
spring.cache.type=caffeine
spring.cache.cache-names=products,productSearch
spring.cache.caffeine.spec=maximumSize=500,expireAfterWrite=10m,recordStatsRedis behaves differently. Spring Data Redis keeps its own statistics, and Boot publishes them as cache.gets, cache.puts, and similar metrics. Counters describe only the current application instance, so with several instances you must aggregate them in your metrics backend.
Common errors
- The
cachesendpoint returns 404. The endpoint is not in the exposure list, or Actuator is missing. - Hits and misses are always zero. The Caffeine spec lacks
recordStats, or no request has reached a cached method yet. - Cache names are missing from the list. Caches that are created lazily appear only after first use. Declare them in
spring.cache.cache-namesto register them at startup. - Misses never turn into hits. Check for self-invocation or a key that changes on every call, as covered in earlier chapters.
Exercise
You enabled the caches and metrics endpoints and use Caffeine with spring.cache.caffeine.spec=maximumSize=500,expireAfterWrite=10m. /actuator/caches lists your caches, but cache.gets reports 0 for both hits and misses even after many requests. What is the likely cause and the fix?
Show answer
The spec does not include recordStats, so Caffeine collects no statistics. Add recordStats to the spec (maximumSize=500,expireAfterWrite=10m,recordStats) and restart. The endpoint list still works because it only reads cache names.