In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to solve cache avalanche and penetration in redis". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The solution of cache penetration is: 1, cache the empty data; 2, use Bloom filter. 2. Solutions to cache avalanches: 1. Set the corresponding hotspot key to never expire; 2. Stagger the expiration time, use randomly generated expiration time, and set the expiration time of hot spot data a little longer; 3. Combine multiple caches; 4. Purchase third-party Redis.
The operating environment of this tutorial: windows7 system, Redis6 version, DELL G3 computer.
Solutions to Redis cache penetration and avalanches
First, cache penetration
When the key queried by the user does not exist in the redis and the corresponding id does not exist in the database, it is attacked by illegal users, and a large number of requests will be directly hit on the db, resulting in downtime, thus affecting the whole system. This phenomenon is called cache penetration.
Solution 1: cache empty data, such as empty strings, empty objects, empty arrays or list, as follows
If (list! = null & & list.size () > 0) {redisOperator.set ("subCat:" + rootCatId, JsonUtils.objectToJson (list));} else {redisOperator.set ("subCat:" + rootCatId, JsonUtils.objectToJson (list), 5x 60);}
Solution 2: Bloom filter
Bloom filter:
To judge whether an element is in an array, as shown in the following figure, a storage using binary system takes up a small amount of memory. 0 means it does not exist, 1 means it does exist, and it is very efficient to add a query. When a value is saved, the corresponding value will be saved to a location on the set of Bloom filters through an algorithm. There may be multiple key at a certain location. When a nonexistent key value is passed in, there may be more than one value. Match with the collection. If it does not match, a null will be returned.
Disadvantages:
1. 1% error rate, when a key does not exist in the Bloom array, but because of this error rate, it is judged that the key exists in a certain case. The longer the array is, the lower the error rate is, the shorter the array is, the higher the error rate is.
2. When we delete a key value, we will delete our database and redis, but we cannot delete it in the Bloom array, because there will be a pair of key at some point in the array. If we want to delete it, we will change 1 to 0, but we will delete all the key values in it.
3. The code complexity will also increase because we have to maintain an additional collection. When we use redis clusters, Bloom filters are used in conjunction with redis.
II. Redis cache avalanche
Cache avalanche: the data in the cache becomes invalid in large quantities, and then this usage requires a large number of requests, but because all the key in redis is invalidated, all requests will be sent to db, resulting in downtime.
Solution
1. Set the corresponding hotspot key to never expire
two。 The expiration time is staggered, the expiration time is randomly generated, and the expiration time of hot spot data is set to be longer, while that of non-hot spot data can be set to shorter.
3. Multi-cache combination, for example: request to enter, you can request redis now, and then request memcache when the redis does not exist. If you do not request db again,
4. Purchase third-party Redis (Alibaba Cloud or redis on Tencent Cloud)
This is the end of "how redis solves cache avalanches and penetration". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.