Video in progress

What is the cache hit ratio, and what do you check when it is low?

The hit ratio shows what fraction of requests were answered from the cache: hit / (hit + miss). At 95%, only 5% of requests go to the database. When it's low, usually the TTL is too short, the key is built wrong, or the cache is too small.

📖 Answer

An example: 10,000 requests per second with a 90% hit ratio means 1000 requests reach the database. Raise the hit ratio to 99% and it's 100 requests. A difference of a few percent changes the database load by a factor of 10.

Common reasons for a low hit ratio:

  • something unnecessary is in the key: for example a timestamp or a request ID — every key becomes unique and is never reused;
  • the TTL is a few seconds while the data doesn't change for hours;
  • the cache is too small, so eviction runs constantly and throws out entries you still need;
  • the data is rarely re-read by nature (each user sees only their own data once) — in that case a cache doesn't help anyway.

It's easy to check: in Redis the INFO stats command shows keyspace_hits and keyspace_misses; in Caffeine you turn on recordStats() and chart it through Micrometer.