Redis缓存穿透、击穿、雪崩,及解决方案(Redis cache penetration, breakdown, avalanche, and Solutions)

Redis缓存穿透、击穿、雪崩,及解决方案

概念

穿透:
查询缓存和数据库中都没有数据,每次缓存都无法命中,导致每个请求都查询数据库。

击穿:
某一个热点数据接收大量请求,这个key突然过期的瞬间,大量的请求会落在数据库上。

雪崩:
大量缓存同一时间集中失效,大量的请求直接查询数据库,数据库的压力瞬间上升,甚至挂掉。

解决方案

穿透

1、接口层增加校验,比如用户鉴权校验,参数做校验,不合法的参数直接代码Return。

2、把Key的Value对写为null、位置错误等其他值,设置一个较短的过期时间,例如60s。

3、布隆过滤器(Bloom Filter)

击穿

为热点数据设置为永不过期,然后加一个互斥锁保证缓存的单线程写。

雪崩

1、热点数据的过期时间加上一个随机数。

2、热点数据设置永不过期:

(1)有更新操作则同步更新缓存。

(2)把过期时间存在value里,通过一个后台的异步线程进行缓存的更新。

3、在缓存失效后,通过互斥锁来控制读数据库写缓存的线程数量。比如对某个key只允许一个线程查询数据和写缓存,其他线程等待。

————————

Redis cache penetration, breakdown, avalanche, and Solutions

concept

pierce through:
There is no data in the query cache and database, and each cache cannot be hit, resulting in each request querying the database.

Breakdown:
When a hot data receives a large number of requests, the instant the key suddenly expires, a large number of requests will fall on the database.

Avalanche:
A large number of caches fail at the same time, and a large number of requests directly query the database. The pressure of the database rises instantaneously or even hangs up.

Solution

pierce through

1. Verification is added to the interface layer, such as user authentication verification, parameter verification, and illegal parameters are directly coded return.

2. Write the value pair of the key to null, location error and other values, and set a short expiration time, such as 60s.

3. Bloom filter

breakdown

Set the hotspot data to never expire, and then add a mutex to ensure the single line write of the cache.

avalanche

1. The expiration time of the hotspot data plus a random number.

2. Hotspot data settings never expire:

(1) If there is an update operation, the cache is updated synchronously.

(2) Store the expiration time in value and update the cache through a background asynchronous thread.

3. After the cache expires, the number of threads reading and writing the database cache is controlled by mutual exclusion. For example, for a key, only one thread is allowed to query data and write cache, while other threads wait.