There is a class of bugs that are worse than crashes. Crashes are loud, they page someone and they get fixed.
This one is quiet. Your cache is running, your hit rate looks fine. But your database is being hit on every single request for data that has not changed at all. I hit this while implementing cache-aside in a side project.
A Quick Recap of How Cache-Aside Works
Before getting into the bug, let me explain the pattern I was using. Cache-aside is the most common caching strategy:
A request comes in. Check the cache first.
If the value is there, return it. Done.
If it is not there, fetch from the database, store the result in the cache, then return it.
The next request for the same data skips the database entirely.
The way you know if a key exists in the cache is: cache.get(key) returns None when the key is not there, and returns the actual value when it is. So your logic ends up looking like this:
raw = cache.get(key)
if raw is not None: # None means miss
Discussion
Start the conversation
Your voice can be the first to spark an engaging conversation.