Cache problem - cache penetration

sonht1109,cachebackend

Cache is one of the most important part when building highly available application. It provides a high-performance way to get frequently accessed data and reduce traffic to our database.

Cache penetration is one of very first problem we should consider when utilizing cache in our application.

What is cache penetration?

This is a scenario where data to be searched doesn't exist in our database and those empty data is not cached as well, so database always gets hit on every search on same resource and even cause it to crash.

Example

This is one simple way we often cache in our application.

new way to use css variable

Whenever request comes, we will search in cache first. If existed, we return data. Otherwise, we will find in our database, and then cache it and return data. So every following request on the same resource will hit cache instead of our database.

But let's imagine that when data is not found at cache, and data is not found in database as well.

new way to use css variable

The searched data is empty, and we may think "Oh it's empty, nothing to cache so just return to our client", so every following request on the same resource will hit our database and yep, that's the problem we have mentioned above.

Solution

Easiest solution here is we have to cache those empty data as well. Remember to set ttl properly.

new way to use css variable

Another solution can be considered is to use Bloom Filter (opens in a new tab). But I don't know much about this algorithm so will let you guys discover it by yourself haha.

Conclusion

Be aware of cache problems will help us build a better application. Hope this will help.

Reference


Thank you for reading. Welcome all your comments/feedbacks.


© 2023 - 2024 by sonht1109