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 > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to solve the problem of redis breakdown, avalanche and penetration". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to solve the problem of redis breakdown, avalanche and penetration.
Redis breakdown
Redis cache breakdown means that a very hot key (that is, a large number of keywords searched on the client) suddenly fails, and when a large number of requests sent from the client cannot find the key in the redis, they will look for it in the data, resulting in excessive pressure on the database.
Solution:
1. Setting the expiration date of value to never expire is very simple and crude, but safe and reliable. But it takes up a lot of space and consumes a lot of memory. Individuals do not recommend using this method, but should operate according to specific business logic.
two。 Use Timetask to do a scheduled task using Timetask to do timing, every once in a while to query some hot key database, and update the query results to redis. The former condition is that it will not put too much pressure on the database.
3. Through the synchronized+ double check mechanism, when reids penetration occurs, a large number of requests are sent to the database. Our solution at this point is to let only one thread query the hotspot key, while the other threads remain blocked (you can let them sleep for a few seconds). When the thread entering the database queries the value corresponding to key, we synchronize it to the cache of redis, and other threads wake up and then request data in redis again.
Example:
Private static volaite Object obj = new Object (); public String getValue (String key) {String value=redis.get (key,String.class); if (value==null | | StringUtils.isBlank (value) {synchronized (obj) {/ / check redis again after entering synchronized to prevent the last thread that grabbed the lock has been updated. Value=redis.get (key,String.class); if (value==null | | StringUtils.isBlank (value) {value=db.query (key); redis.set (key,value,1000);} return value;}
Disadvantages: there is a risk of deadlocks and thread blocking.
Redis avalanche
It means that when a large number of requests query multiple key, the redis cache becomes invalid or cannot be found, and then all the massive requests go to db query, resulting in a sudden surge and collapse of db pressure.
Cause of occurrence:
Simultaneous failure of 1.key
2.redis itself collapsed.
Solution:
1. When setting the cache, initialize its expiration time randomly
If the key of redis fails at the same time, this method can be adopted, and the specific failure time is determined according to the business situation.
two。 Put different hotspot key on different nodes
Because redis is generally deployed in clusters, placing different hotspots key evenly on different nodes can also effectively avoid avalanches.
3. Set the time limit of value to never expire
4. Use Timetask to do a scheduled task to refresh the redis cache before it expires
Redis penetration
Because bad users malicious frequent queries will cause great problems to the system: key cache and the database does not exist, so each query will query the database and cause the database to crash.
(for example, the primary keys of the data we store in the database are self-increasing and there is no negative number. Some hackers take advantage of this point and constantly use the parameters of the primary key id-1 to initiate massive query requests, resulting in these requests can not find the corresponding data in the redis, so they can only go to the database to query, resulting in a database crash. )
Solution:
1. When a similar request is sent, whatever result is found will be put into the redis cache.
This solves the problem that the next time he initiates a request with the same parameter, he will go directly into the redis instead of the database.
two。 Block its ip
3. Check the validity of the requested parameters and return them directly on the premise that they are illegal.
4. Use Bloom filter
A Bloom filter can be understood as a whitelist or a blacklist, and its function is to determine whether an element exists in the filter.
Whitelist: the filter contains all the legal parameters key in the database. The request goes through the Bloom filter, and the Bloom filter determines whether the request's key is in the filter, and then let the request enter the redis. If not, return the empty data directly.
Public static void main (String [] args) {Config config = new Config (); config.useSingleServer () .setAddress ("redis://127.0.0.1:6379"); config.useSingleServer () .setPassword ("1234"); / / construct Redsson RedissonClient redisson = Redisson.create (config); RBloomFilter bloomFilter = redisson.getBloomFilter ("phoneList") / / name our self-defined Bloom filter phoneList, and / / initialize the Bloom filter to set the expected element to 100000000L with an error rate of 3% bloomFilter.tryInit (100000000Lmem0.03); / / insert 10086 into the Bloom filter bloomFilter.add ("10086") / / determine whether the following number exists in the Bloom filter / / false System.out.println ("123456"); / / true System.out.println ("10086");}
Disadvantages:
The Bloom filter may cause misjudgment to penetrate the redis into the DB, but the probability of miscalculation is very small.
Thank you for your reading, the above is the content of "how to solve the problem of redis breakdown, avalanche and penetration". After the study of this article, I believe you have a deeper understanding of how to solve the problem of redis breakdown, avalanche and penetration, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.