In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to solve redis avalanche and penetration". In daily operation, I believe many people have doubts about how to solve the problem of redis avalanche and penetration. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to solve redis avalanche and penetration"! Next, please follow the editor to study!
The solutions are: 1. Cache traversal, which can cache empty data and use Bloom filter; 2. Cache avalanche, you can set the corresponding hotspot key never expires, combine multiple caches, purchase third-party Redis, and stagger the expiration time, and the expiration time can be randomly generated.
The operating environment of this tutorial: windows7 system, Redis5.0.10 version, DELL G3 computer.
Redis cache traversal and its solution
First, cache penetration
1. 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.
two。 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);}
3. Solution 2: Bloom filter
Bloom filter: judge whether an element is in an array. As shown in the figure below, it takes up a small amount of memory, 0 means it does not exist, 1 means it does not 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 the Bloom filter through an algorithm, and there may be multiple key at a certain location. When a nonexistent key value is passed in, it matches the collection. If the match does not match, a null will be returned.
Disadvantages: 1, 1% error rate, when there is a key that 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, the lower the error rate, the shorter the array, the higher the error rate.
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 also increases because we have to maintain an additional collection, and when we use redis clusters, the Bloom filter is used in conjunction with redis.
II. Redis cache avalanche
1. 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.
two。 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 hot spot data is set to be longer, while that of non-hot spot data can be set to shorter.
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,
Purchase third-party Redis (Alibaba Cloud or redis on Tencent Cloud)
At this point, the study on "how to solve redis avalanche and penetration" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.