当前位置:网站首页>阿里面试官:给我描述一下缓存击穿的现象,并说说你的解决思路?
阿里面试官:给我描述一下缓存击穿的现象,并说说你的解决思路?
2022-07-30 19:22:00 【nuzzzzz】
前言
缓存(内存 or Memcached or Redis.....)在互联网项目中广泛应用,本篇博客将讨论下缓存击穿这一个话题,涵盖缓存击穿的现象、解决的思路、以及通过代码抽象方式来处理缓存击穿。
什么是缓存击穿?

上面的代码,是一个典型的写法:当查询的时候,先从Redis集群中取,如果没有,那么再从DB中查询并设置到Redis集群中。 注意,在实际开发中,我们一般在缓存中,存储的数据结构是JSON。(JDK提供的序列化方式效率稍微比JSON序列化低一些;而且JDK序列化非常严格,字段的增减,就很可能导致反序列失败,而JSON这方面兼容性较好) 假设从DB中查询需要2S,那么显然这段时间内过来的请求,在上述的代码下,会全部走DB查询,相当于缓存被直接穿透,这样的现象就称之为“缓存击穿”!
避免缓存击穿的思路分析
加synchronized?

如果synchronized加在方法上,使得查询请求都得排队,本来我们的本意是让并发查询走缓存。也就是现在synchronized的粒度太大了。
缩小synchronized的粒度?

上面代码,在缓存有数据时,让查询缓存的请求不必排队,减小了同步的粒度。但是,仍然没有解决缓存击穿的问题。 虽然,多个查询DB的请求进行排队,但是即便一个DB查询请求完成并设置到缓存中,其他查询DB的请求依然会继续查询DB!
synchronized+双重检查机制

通过synchronized+双重检查机制: 在同步块中,继续判断检查,保证不存在,才去查DB。
代码抽象
发现没有,其实我们处理缓存的代码,除了具体的查询DB逻辑外,其他都是模板化的。下面我们就来抽象下!
一个查询DB的接口:

既然查询具体的DB是由业务来决定的,那么暴露这个接口让业务去实现它。
一个模板:

Spring不是有很多Template类么?我们也可以通过这种思想对代码进行一个抽象,让外界来决定具体的业务实现,而把模板步骤写好。(有点类似AOP的概念)
改进后的代码:

从这里可以看出,我们并不关心缓存的数据从哪里加载,而是交给具体的使用方,而且使用方在使用时再也不必关注缓存击穿的问题,因为我们都给抽象了。
边栏推荐
- 深入浅出边缘云 | 3. 资源配置
- SimpleOSS third-party library libcurl and engine libcurl error solution
- Swiper rotates pictures and plays background music
- Listen to the boot broadcast
- What is the value of biomedical papers? How to translate the papers into Chinese and English?
- How architects grow
- - daily a LeetCode 】 【 191. A number of 1
- 【MindSpore】多卡训练保存权重问题
- 【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。
- What is a RESTful API?
猜你喜欢

NXP IMX8QXP更换DDR型号操作流程

- daily a LeetCode 】 【 191. A number of 1

VBA runtime error '-2147217900 (80040e14): Automation error

natural language processing nltk

【MindSpore1.2.0-rc1产品】num_workers问题
![[TypeScript]编译配置](/img/ac/64ebd33de977e35620dbc18d2adfad.png)
[TypeScript]编译配置

After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...

Basic use of scrapy

Zabbix 5.0 监控教程(一)

MindSpore:【语音识别】DFCNN网络训练loss不收敛
随机推荐
[TypeScript]编译配置
ImportError:attempted relative import with no known parent package
ResNet18-实现图像分类
Talking about Contrastive Learning (Contrastive Learning) the first bullet
Chapter 4 Controlling the Execution Flow
The advanced version of the Niu Ke brushing series (team competition, sorting subsequences, inverting strings, deleting common characters, repairing pastures)
【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest
什么是 RESTful API?
The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
Google's AlphaFold claims to have predicted almost every protein structure on Earth
阿里云武林头条活动分享
Difference between Object and Map
Range.CopyFromRecordset 方法 (Excel)
[Private Series] All kinds of strange problems encountered in daily PHP
LeetCode 0952.按公因数计算最大组件大小:建图 / 并查集
[Prometheus] An optimization record of the Prometheus federation [continued]
What is the difference between a cloud database and an on-premises database?
crontab中写go run不执行的问题
Zabbix 5.0 监控教程(一)
MindSpore:【resnet_thor模型】尝试运行resnet_thor时报Could not convert to