Integrated powerful Redis, high penetration of concurrent queries, and caching and cache avalanche prevention

 

 

 

On the use Redis, we should have been no stranger, I also introduced the use Redis, data structure, scene analysis, since Redis this powerful, then after architecture introduced Redis, it is not "invincible" of it?

In fact, all the architectures, frameworks, components, while solving part of the problem, will also bring new problems, let's see what use Redis may encounter problems.

 

01. Cache penetration

Redis most usage scenarios, it is based on the key, the first in Redis query, if the query is not the case, then query the database.

When a large number of requests, key value is simply not Redis, then will fall on the database query, these requests as if "through" After a Redis fell on the database, the database will eventually lead to collapse until overwhelmed.

 

 

Let's see penetrate the cache coping strategies:

1. The invalid key to save the Redis

If you can not find in Redis, and query the database and no result, then this will be written to the Redis key, set value = empty, so if the key value is repeatedly accessed, it will not query the database.

 

 

But after a few minutes if the database, into a real data, inconsistent database and cache data happens;

This case, either actively update in this key- empty Redis data, either set the cache time, and set the amount of cache expiration time, so that when the time is over, you can cache data in the brush into Redis.

If the key value is not the same for each query, such as receipt of a malicious attack, every access is invalid without the same key value, then this approach will fail.

2. Bloom filter

Principle Bloom filter (Bloom Filter) is very complicated to explain, in the vernacular summarize its characteristics: it says a key is not present, then it certainly does not exist, it says there is a key, it is very likely exist (there is a certain rate of false positives).

Use Bloom filter, turned back an invalid request, the process something like this:

 

 

 

For students interested Bloom filter, you can try Google tools produced Guava libraries, among them the Bloom filter out of the box ready to use: BloomFilter;

 

另外,Redis 在 4.0 之后有了插件功能(Module),可以使用外部的扩展功能,可以使用 RedisBloom 作为 Redis 布隆过滤器插件。

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class BloomFilterTest {
   public static void main(String[] args){
     int size = 1000000 ;
     //布隆过滤器
     BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), size, 0.001 );
     
     for ( int i = 0 ; i < size; i++) {
             bloomFilter.put(i);
         }
     
     List<Integer> list = new ArrayList<Integer>( 1000 );
     for ( int i = size + 1 ; i < size + 10000 ; i++) {
          if (bloomFilter.mightContain(i)) {
              list.add(i);
          }
      }
      System.out.println( "误判数量:" + list.size());
   }
}

  

 

 

 

 

02.缓存雪崩

通常我们在使用 Redis 的时候,都会为缓存设置过期时间,但是如果在某个时间点,有大量缓存失效,那么下一个时间点就会有大量请求访问到数据库,这种情况下,数据库可能因为访问量多大导致“崩溃”,这就是缓存雪崩。

让我们看看缓存雪崩的解决方案:

1. 不设置缓存过期时间

最暴力的解决办法,缓存不设置自动过期时间,只要缓存不崩,数据库就不会崩。

2. 设置随机过期时间

另外一个办法,就是让缓存过期时间不那么一致,比如一批缓存数据24小时后过期,那么就在这个基础上,让每条缓存的过期时间前后随机 1-6000 秒(1-10分钟)。

3. 使用互斥锁

在缓存失效后,通过互斥锁或者队列,控制读数据库和写缓存的线程数量;不过这样会导致系统的吞吐量下降。

4. 双缓存

设置一级缓存和二级缓存,一级缓存过期时间短,二级缓存过期时间长或者不过期,一级缓存失效后访问二级缓存,同时刷新一级缓存。

 

最后在强调一遍,任何架构、组件、框架的引入,都会带来新的问题,我们在使用的时候一定要有相应的评估和解决方案。

Guess you like

Origin www.cnblogs.com/lykbk/p/ssdsdsdw33423434343434.html