Video in progress

Is the cache actually paying off or just adding complexity? How do you measure that, and how do you show it to others?

Hit ratio isn't enough: I compare p99 latency with and without the cache, the cost of the load taken off the DB, the number of bugs caused by staleness, and the cost of Redis. For every cache, the answer to "what happens if it's gone" should be in numbers.

📖 Answer

Many teams see "hit ratio 90%" and relax. But a cache with a 90% hit ratio may be catching the DB's cheapest queries and letting the expensive ones through.

What I measure:

  1. Per-cache metrics: for each cache name separately: hit, miss, load time (the DB time on a miss), key count, average value size. Spring Boot Actuator provides cache.gets{result=hit|miss}; for a Redis cache you need RedisCacheManager.builder(...).enableStatistics() to turn it on.

  2. Work saved: miss_load_time × hit_count is the DB time the cache saved. With this number you see right away which cache matters. Sometimes a cache with a 99% hit ratio saves a 1 ms query, which is even cheaper than going to Redis, meaning the cache is useless.

  3. An experiment without the cache: with a canary or a feature flag, send 1-5% of traffic through without the cache and compare latency and DB load. This is the most reliable evidence.

  4. Cost: the cost of the Redis cluster and the engineering time spent on it (incidents, staleness bugs). Tagging and counting the support tickets related to staleness is useful.

  5. Memory efficiency: what percentage of keys are never read a second time? You can find out with sampled observation (for example, OBJECT FREQ in LFU mode, or on the application side). In many cases half of the cache is full of "one-time" keys.

I show the result to leadership in a plain table: cache name, DB time saved, latency difference, memory share, incidents in the last 3 months. After that, decisions like "we remove this cache", "we move this one to L1", "we make this one bigger" can be made without arguments.

On one project, after this kind of analysis we removed 5 of 14 caches. The DB load didn't change noticeably, but the bugs related to invalidation almost disappeared.