当前位置:网站首页>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.
边栏推荐
- SimpleOSS third-party library libcurl and engine libcurl error solution
- VBA connects Access database and Excel
- 云数据库和本地数据库有什么区别?
- Spark学习:用spark实现ETL
- [flink] Error finishing Could not instantiate the executor. Make sure a planner module is on the classpath
- M3SDA:用于多源域自适应的矩匹配
- MindSpore:npu 多卡训练自定义数据集如何给不同npu传递不同数据
- 055 c# print
- ImportError:attempted relative import with no known parent package
- Tensorflow2.0 confusion matrix does not match printing accuracy
猜你喜欢

The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary

MySQL six-pulse sword, SQL customs clearance summary

LeetCode 0952. Calculate Maximum Component Size by Common Factor: Mapping / Union Search

DCM 中间件家族迎来新成员

How to build FTP server under win2003

HCIP --- 企业网的三层架构

Frog jumping steps (recursive and non-recursive) ------- Xiaolele walks the steps

【Node实现数据加密】

Alibaba Cloud Martial Arts Headline Event Sharing

Golang logging library zerolog use record
随机推荐
架构师如何成长
第十七届“振兴杯”全国青年 职业技能大赛——计算机程序设计员(云计算平台与运维)参赛回顾与总结
Download Win11 how to change the default path?Download Win11 change the default path method
阿里面试这些微服务还不会?那还是别去了,基本等通知
MindSpore:npu 多卡训练自定义数据集如何给不同npu传递不同数据
PostgreSQL 14.4如何安装使用
The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)
MySQL六脉神剑,SQL通关大总结
【MindSpore】多卡训练保存权重问题
How architects grow
055 c# print
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
MindSpore:【resnet_thor模型】尝试运行resnet_thor时报Could not convert to
MySQL数据库————视图和索引
How to install and use PostgreSQL 14.4
MySQL性能优化(硬件,系统配置,表结构,SQL语句)
【刷题篇】计算质数
电脑死机的时候,发生了什么?
ImportError: attempted relative import with no known parent package
跨进程启动后台服务