Home  >  Article  >  Database  >  What is the solution to redis avalanche and penetration

What is the solution to redis avalanche and penetration

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-06-18 10:31:283239browse

The solution is: 1. Cache penetration, you can also cache empty data and use Bloom filters; 2. Cache avalanche, you can set the corresponding hotspot key to never expire, combine multiple caches, and purchase Third-party Redis and staggered expiration time, the expiration time can be randomly generated.

What is the solution to redis avalanche and penetration

The operating environment of this tutorial: Windows 7 system, Redis version 5.0.10, DELL G3 computer.

Redis cache penetration and solutions

1. Cache penetration

1. When the key queried by the user does not exist in redis, the corresponding id does not exist in the database. If it is attacked by illegal users, a large number of requests will be hit directly on the db, causing downtime and affecting the entire system. This phenomenon is called cache penetration.

2. Solution 1: Cache empty data, such as empty strings, empty objects, empty arrays or lists, the code is as follows

if (list != null && list.size() > 0) {
     redisOperator.set("subCat:" + rootCatId, JsonUtils.objectToJson(list));
} else {
     redisOperator.set("subCat:" + rootCatId, JsonUtils.objectToJson(list), 5*60);
}

3. Solution 2: Bloom Filter

Bloom filter: Determine whether an element is in an array, as shown in the figure below. It uses binary to store and occupies a relatively small memory. 0 represents non-existence and 1 represents existence, which increases query efficiency. Soon, when a value is saved, an algorithm will be used to save the corresponding value to a certain position on the Bloom filter set. There may be multiple keys at a certain position. When a non-existent key value is passed in, , matches the set, and if it does not match, it will return a null

Disadvantages: 1. 1% misjudgment rate, when a key does not exist in the Bloom array, but due to this misjudgment rate, Under certain circumstances, it is judged that the key exists. The longer the array, the lower the false positive rate, and the shorter the array, the higher the false positive rate.

2. When we want to delete a certain key value, it is The content in our database and redis will be deleted, but it cannot be deleted in the Bloom array, because there will be a key at a certain position in the array. If we want to delete it, we will change 1 to 0, but all the keys in it will be changed. All values ​​are deleted

3. The code complexity will also increase because we have to maintain an additional collection. When we use redis cluster, Bloom filter must be used in combination with redis

What is the solution to redis avalanche and penetration

2. Redis cache avalanche

1. Cache avalanche: The data in the cache fails in large batches, and then this use requires a large number of requests to come in, but because all the keys in redis If it fails, all requests will be sent to the db, causing downtime

2. Solution

  • Set the corresponding hotspot key to never expire

  • The expiration time is staggered, the expiration time is randomly generated, and the expiration time of hotspot data is set longer, and the expiration time of non-hotspot data can be set shorter.

  • Multiple cache combinations, for example: To request entry, you can request redis now. When redis does not exist, request memcache. If it does not exist, request db

  • Purchase third-party Redis (Alibaba Cloud or Tencent Cloud) redis)

Related tutorial recommendations: Redis tutorial

The above is the detailed content of What is the solution to redis avalanche and penetration. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn