当前位置:网站首页>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.
边栏推荐
- 什么是 RESTful API?
- Tensorflow2.0 混淆矩阵与打印准确率不符
- Mapped Statements collection does not contain value for的解决方法
- 部分分类网络性能对比
- VBA runtime error '-2147217900 (80040e14): Automation error
- Object和Map的区别
- Alibaba Cloud Martial Arts Headline Event Sharing
- Install MySQL tutorial under Linux
- 又一家公司面试的内容
- 阿里面试这些微服务还不会?那还是别去了,基本等通知
猜你喜欢
MindSpore:【Resolve node failed】解析节点失败的问题
【MindSpore】多卡训练保存权重问题
The JDBC programming of the MySQL database
ELK日志分析系统
Cesium加载离线地图和离线地形
Install Mysql5.7 under Linux, super detailed and complete tutorial, and cloud mysql connection
【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。
MindSpore:【语音识别】DFCNN网络训练loss不收敛
LeetCode 0952. Calculate Maximum Component Size by Common Factor: Mapping / Union Search
SimpleOSS third-party library libcurl and engine libcurl error solution
随机推荐
What is the difference between a cloud database and an on-premises database?
LeetCode 0952.按公因数计算最大组件大小:建图 / 并查集
Spark学习:编译Spark项目时遇到的报错
MongoDB打破了原则引入SQL?
Trial writing C language sanbang
Another company interview
【PyTorchVideo教程01】快速实现视频动作识别
VBA connects Access database and Excel
Range.CopyFromRecordset 方法 (Excel)
MindSpore:【模型训练】【mindinsight】timeline的时间和实际用时相差很远
第十七届“振兴杯”全国青年 职业技能大赛——计算机程序设计员(云计算平台与运维)参赛回顾与总结
MindSpore:【JupyterLab】按照新手教程训练时报错
[PyTorchVideo Tutorial 01] Quickly implement video action recognition
How to build FTP server under win2003
How to install and use PostgreSQL 14.4
架构师如何成长
The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)
监听开机广播
启动前台Activity
centos7安装mysql8