当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
[hbuilder] cannot run some projects, open the terminal and cannot enter commands
How to install and use PostgreSQL 14.4
The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary
Scala学习:breakable
MySQL数据库 ---MySQL表的增删改查(进阶)
MySQL sub-database sub-table
【MindSpore】多卡训练保存权重问题
.eslintrc.js for musicApp
又一家公司面试的内容
VBA connects Access database and Excel
Google's AlphaFold claims to have predicted almost every protein structure on Earth
VBA批量将Excel数据导入Access数据库
MindSpore:【Resolve node failed】解析节点失败的问题
第十七届“振兴杯”全国青年 职业技能大赛——计算机程序设计员(云计算平台与运维)参赛回顾与总结
Spark学习:用spark实现ETL
Download Win11 how to change the default path?Download Win11 change the default path method
什么是 RESTful API?
电脑死机的时候,发生了什么?
【flink】报错整理 Could not instantiate the executor. Make sure a planner module is on the classpath
Witness the magical awakening of the mini world in HUAWEI CLOUD








