Video in progress

What metrics and alerts do you set up for a cache layer (Caffeine or Redis)? What does it mean for a cache to be "working"?

The main signal is the hit ratio, but it is not enough on its own: you also need evictions and size and load time, and for Redis latency, memory, evicted keys and connections. A cache is working when the hit ratio is at the expected level and cache misses are not flooding the backend.

📖 Answer

For caches created through Spring Boot's CacheManager, Boot exports cache.gets{result="hit|miss"}, cache.puts, cache.evictions and cache.size metrics itself. With Caffeine, stats must be enabled, otherwise everything is zero:

spring:
  cache:
    caffeine:
      spec: maximumSize=50000,expireAfterWrite=10m,recordStats

Hit ratio:

sum by (cache) (rate(cache_gets_total{result="hit"}[5m]))
/ sum by (cache) (rate(cache_gets_total[5m]))

What I look at:

  • The trend of the hit ratio, not the absolute number: 70% is great for one cache and a disaster for another. A sharp drop after a deploy means the cache key changed or the TTL got shorter.
  • Eviction rate and size: if cache.size is always at maximumSize and there are many evictions, the cache is too small and data keeps churning in and out.
  • The cost of a miss: how many calls go to the backend on a cache miss, and their latency. A hit ratio drop from 95% to 85% triples the DB load; you need that arithmetic when picking an alert threshold.
  • On the Redis side, from redis_exporter: evicted_keys (hit maxmemory), used_memory, number of connections, command latency. On the client side, the Lettuce command latency metrics.

Alerts: I don't put a page directly on the hit ratio, because it is a cause. A page goes on the symptom (the service latency SLO). But at ticket level, "the hit ratio dropped 20 points compared to last week" and Redis memory above 90% are useful.

A risk people often forget is the thundering herd: when a popular key expires, hundreds of requests hit the backend at the same time. In the metrics it shows up as a short but tall spike of misses. In Caffeine, refreshAfterWrite or a single loader (get(key, loader)) softens this problem.