当前位置:网站首页>Interviewer Ali: Describe to me the phenomenon of cache breakdown, and talk about your solution?
Interviewer Ali: Describe to me the phenomenon of cache breakdown, and talk about your solution?
2022-07-30 19:41:00 【nuzzzzz】
Foreword
Cache (memory or Memcached or Redis.....) is widely used in Internet projects. This blog will discuss the topic of cache breakdown, covering the phenomenon of cache breakdown, solutions, and codeAbstract way to handle cache breakdown.
What is cache breakdown?

The above code is a typical way of writing: when querying, first get it from the Redis cluster, if not, then query it from the DB and set it to the Redis cluster.Note that in actual development, we generally store in the cache, and the data structure stored is JSON.(The serialization method provided by JDK is slightly less efficient than JSON serialization; and JDK serialization is very strict, and the increase or decrease of fields may cause deserialization failure, and JSON has better compatibility in this regard) Suppose query from DBIt takes 2S, so obviously the requests that come during this period of time will all go through the DB query under the above code, which is equivalent to the direct penetration of the cache. This phenomenon is called "cache breakdown"!
Analysis of ideas to avoid cache breakdown
Add synchronized?

If synchronized is added to the method, all query requests have to be queued. Originally, our intention was to let concurrent queries go to the cache.That is, the granularity of synchronized is too large now.
Reduce the granularity of synchronized?

In the above code, when there is data in the cache, the request for querying the cache does not have to be queued, which reduces the granularity of synchronization.However, the problem of cache breakdown is still not resolved.Although multiple DB query requests are queued, even if one DB query request is completed and set in the cache, other DB query requests will continue to query DB!
synchronized+double check mechanism

Through the synchronized + double check mechanism: In the synchronized block, continue to judge and check to ensure that it does not exist, and then check the DB.
Code abstraction
No, in fact, the code we deal with the cache is templated except for the specific query DB logic.Now let's abstract it!
An interface for querying DB:

Since querying the specific DB is determined by the business, then expose this interface for the business to implement it.
A template:

Don't Spring have a lot of Template classes?We can also abstract the code through this idea, let the outside world decide the specific business implementation, and write the template steps.(somewhat similar to the concept of AOP)
Improved code:

It can be seen from this that we don't care where the cached data is loaded from, but to the specific user, and the user no longer has to pay attention to the problem of cache breakdown when using it, because we give abstraction.
边栏推荐
猜你喜欢
随机推荐
青蛙跳台阶(递归和非递归)-------小乐乐走台阶
ImportError: attempted relative import with no known parent package
VBA批量将Excel数据导入Access数据库
谷歌AlphaFold近日宣称预测出地球上几乎所有蛋白质结构
Scala学习:类和对象
What is the difference between a cloud database and an on-premises database?
Google's AlphaFold claims to have predicted almost every protein structure on Earth
MindSpore:【Resolve node failed】解析节点失败的问题
Witness the magical awakening of the mini world in HUAWEI CLOUD
Perfectly Clear QuickDesk & QuickServer图像校正优化工具
Mapped Statements collection does not contain value for的解决方法
Scala学习:breakable
牛客网——华为题库(100~108)
mysql慢查询优化
musicApp 的.eslintrc.js
The problem of writing go run in crontab does not execute
MySQL函数(经典收藏)
How do radio waves transmit information?
VBA runtime error '-2147217900 (80040e14): Automation error
The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
![After MySQL grouping, take the largest piece of data [optimal solution]](/img/ff/b8218cb010b8be7d0564e45d4efa70.png)







