Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between JAVA cache breakdown, cache penetration and cache avalanche

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要介绍"JAVA缓存击穿、缓存穿透、缓存雪崩的区别有哪些",在日常操作中,相信很多人在JAVA缓存击穿、缓存穿透、缓存雪崩的区别有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"JAVA缓存击穿、缓存穿透、缓存雪崩的区别有哪些"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

调用链路

一个请求Request过来,服务器首先和缓存中间件建立连接,传输对应key到缓存中间件中获取相对应的数据,服务器拿到返回的结果后,判断返回的结果是否有数据,如果有数据,则返回从缓存中拿到的结果。如果缓存中间件中没有数据,则建立数据库连接,访问数据库服务器,按照相应逻辑拿到返回结果,判断结果中是否有数据,如果有则返回对应数据,如果没有则按照业务场景要求,返回对应结果(一般为null或者new一个空对象)。

缓存击穿

含义:

什么是缓存击穿?通俗的讲指的是缓存中没有数据,但数据库中有数据的场景。那为什么缓存中会没有数据呢?一般是由于设置了缓存时间导致缓存过期,所以没有数据。那缓存找不到数据去数据库查询就好了呀,为啥又叫击穿?是因为要查询这个key对应的数据是一个热点数据,并发访问的量大,同时去查询数据库,导致数据库压力骤增,严重会打崩数据库。

解决方案:

1、如果是不改变的数据,如一些常量值,则可以设置对应热点key永不过期。

2、加上互斥锁,防止同一台服务器同一时间有多个连接访问数据库。

// 伪代码public class Main { // 双重检测锁 public static String getHotData(String key) { // 先从缓存中间件获取对应热点key数据 String response = redis.get(key); // 缓存没有数据 if(Objects.isNull(response)) { // 保证一台服务器同一时间只有一个线程访问 synchronized (Main.class) { // 假设A线程访问进synchronized里,线程B, C阻塞在synchronsized外面 // 线程A退出synchronized后,线程B和C应该从redis中拿而不是再访问数据库 response = redis.get(key); // 访问数据库 拿到数据后 写进redis中 if(Objects.isNull(response)) { response = loadDataFromMySQL(key); redis.set(key, response); } } } return response; }}

3、加上分布式锁,全局保证只有一个线程访问数据库。

// 伪代码public class Main { // 分布式唯一key public static String getHotData(String key, int tryTime) throws InterruptedException { if(tryTime >= 4) { return ""; } // 先从缓存中间件获取对应热点key数据 String response = redis.get(key); // 缓存没有数据 if(Objects.isNull(response)) { // 保证整个服务集群同一时间只有一个线程访问 if (redis.tryLock()) { try { // 访问数据库 拿到数据后 写进redis中 if(Objects.isNull(response)) { response = loadDataFromMySQL(key); redis.set(key, response); } } finally { redis.unlock(); } } else { TimeUnit.MILLISECONDS.sleep(100); getHotData(key, tryTime + 1); } } return response; }}缓存穿透

含义:

缓存穿透指的是缓存中间件和数据库都没有对应的数据,但是不断接收到请求获取该key的数据,导致数据库压力过大,甚至崩溃。

Solution:

1. After accessing the database and getting no data, you can add a value of the key to the cache layer according to specific business requirements, and set an expiration time, such as 10s or 1min, etc. So why not set it to expire? The first is that because the key may have a corresponding business meaning, it may just be that there is no data at this point in time, so it cannot be set not to expire; the second is that if it is really malicious access, then there may be no similar request after a while, so we do not need to keep the data in the cache.

2. Add verification. If it is an unexpected request, it can be filtered directly. For example, if the cache stores user information and the corresponding cache key is related to id, then if your id is greater than or equal to 0, the id less than 0 can be filtered directly.

@Controllerpublic class Controller { @RequestMapping(value="/test") public String printHello(Integer id) { if(Objects.isNull(id) || id < 0) { return null; } //process corresponding logic }} Cache avalanche meaning:

Cache avalanche refers to that at the same point in time, a large number of data in the cache expires, and they are all hot data, resulting in concurrent pressure hitting the database at the same time, resulting in a sudden increase in database pressure and even downtime. Some people will ask, isn't this the same as cache breakdown? Cache breakdown refers to the concurrent query of a hot key data, cache avalanche refers to a large batch. One of the scenarios that occurs is on certain core pages, where the content of the page is cached and the cache time is set to the same value.

Solution:

1. The simplest thing is to set the hot spot data not to expire, but it should be viewed in combination with the corresponding business scenario.

2. When setting the expiration time for each hotspot key, add a random value to make the hotspot data discrete and not expire in large quantities at the same time.

3. Mutex lock and distributed lock mentioned in cache breakdown scenario.

At this point, the study of "JAVA cache breakdown, cache penetration, cache avalanche" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report