当前位置:网站首页>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.
边栏推荐
- 055 c# print
- MySQL函数(经典收藏)
- MySQL sub-database sub-table
- 【hbuilder】运行不了部分项目 , 打开终端 无法输入指令
- After watching "Second Uncle", I was even more internalized
- MySQL六脉神剑,SQL通关大总结
- 阿里面试这些微服务还不会?那还是别去了,基本等通知
- Correct pose of Vulkan open feature
- [flink] Error finishing Could not instantiate the executor. Make sure a planner module is on the classpath
- Linux download and install mysql5.7 version tutorial the most complete and detailed explanation
猜你喜欢
随机推荐
Centos7 install mysql8
MySQL大批量造数据
The problem of writing go run in crontab does not execute
VBA 连接Access数据库和Excle
How to build FTP server under win2003
Start background services across processes
VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
MySQL six-pulse sword, SQL customs clearance summary
LeetCode 0952.按公因数计算最大组件大小:建图 / 并查集
Listen to the boot broadcast
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
监听开机广播
MySQL database - DQL data query language
【flink】报错整理 Could not instantiate the executor. Make sure a planner module is on the classpath
【MindSpore】多卡训练保存权重问题
MindSpore: CV.Rescale(rescale,shift)中参数rescale和shift的含义?
Scala学习:breakable
MySQL数据库主从配置
After watching "Second Uncle", I was even more internalized
【刷题篇】计算质数









