Lesson 9 of 9 · 23m
Measuring Hit Rates and Cache Effectiveness
Instrument hit ratio, miss cost, stale serve rate, latency, and origin protection so cache metrics reflect outcomes rather than vanity percentages.
A 98% hit ratio can still be a bad cache. If the remaining 2% are checkout writes, permission checks, or enormous misses, the system can be slower and less correct than before. Hit ratio is a useful signal, but only when paired with cost, freshness, and failure behavior. Measure what the cache is supposed to protect.
Define hit ratio precisely
There are multiple legitimate hit ratios:
key hit ratio hits / (hits + misses)
byte hit ratio bytes served from cache / total bytes
object reuse entries read more than once / entries written
origin offload origin requests avoided / potential origin requestsThey answer different questions. A CDN with high byte hit ratio but low object hit ratio is still useful because large objects dominate egress. An application cache can have high key hit ratio while missing the expensive queries that matter.
Instrument the decision points
At minimum, emit labels for hit, miss, stale hit, negative hit, bypass, error, and recompute:
metrics.increment(
"cache.lookup",
tags={"cache": "product", "result": result},
)Do not include raw cache keys or user identifiers as metric labels. Cardinality will bankrupt the metrics system and possibly leak sensitive data. Use stable cache names and result categories.
Measure miss cost, not just miss count
One miss can cost 2 ms; another can scan a million rows. Record latency and origin work separately:
cache.lookup.result
cache.recompute.duration_ms
cache.recompute.concurrent
origin.query.duration_ms{query_class=product_detail}
origin.bytes_avoided or requests_avoidedThen compare p50 and p95 latency for cache hits, misses, and database fallback. If hit latency is close to miss latency because payloads are huge or deserialization is expensive, the cache is not buying what you think it is buying.
Freshness metrics are correctness metrics
Track how stale served values are when the domain permits bounded staleness. Emit soft-expiry age, stale-while-revalidate use, invalidation lag, and failed invalidations:
cache.stale_serve.count
cache.invalidation.lag_ms
cache.invalidation.failure.count
cache.negative_hit.countA rising stale-serve count during normal traffic may mean TTLs are too short or recomputation is failing. A burst after deployment may be normal. Interpret the shape in context.
Dashboards should answer operational questions
Useful panels include hit ratio by cache, memory usage, evictions, keyspace growth, recomputation concurrency, lock contention, origin fallback QPS, p95 latency split by result, invalidation failures, and top hot keys by count or bytes. Add deploy markers and cache flush markers. Without those, every graph interpretation becomes folklore.
sum(rate(cache_lookup_total{result="hit"}[5m]))
/
sum(rate(cache_lookup_total{result=~"hit|miss"}[5m]))This is a starting point, not the whole truth. Pair it with origin QPS and latency.
Test whether removing the cache would hurt
The uncomfortable question is the important one: what happens if this cache disappears? If nothing changes, remove it. If the database dies, protect that dependency carefully. Chaos exercises and cache-down runbooks reveal whether your theoretical offload exists in practice.
Field notes
- High hit ratio can hide cacheable responses that should never have been shared.
- Eviction count matters. Evicting useful entries can look like random capacity problems.
- Do not alert only on low hit ratio; alert on origin overload, invalidation failure, and abnormal stale serving.
- Per-endpoint ratios beat global averages. Averages hide the exact hot path you care about.
- Cache metrics are security telemetry too: sudden keyspace or cardinality changes can indicate abuse or poisoning.
Was this lesson helpful?