Video in progress

What are a cache hit and a cache miss, and how do you find out the hit ratio?

A hit means the requested value was found in the cache; a miss means it wasn't and you had to go to the original source. Hit ratio = hit / (hit + miss); in Redis you compute it from keyspace_hits and keyspace_misses in INFO stats.

📖 Answer

redis-cli INFO stats | grep keyspace
keyspace_hits:982341
keyspace_misses:41120

Here the ratio is about 96%. For a cache, that's a good number.

If the ratio is low, the usual causes are:

  • the TTL is too short, so the value expires before anyone asks for it;
  • the key contains something that changes every time (a timestamp, an unbounded page number);
  • maxmemory is small and eviction runs often (check evicted_keys too);
  • nobody ever requests the cached data a second time.

In Spring Boot, if Actuator is on, the cache.gets metric is exposed with a result=hit/miss tag. Unlike the overall Redis figure, it breaks the numbers down per cache name.