编程知识 cdmana.com

How to analyze several common key and hot issues in redis from multiple dimensions

explain

Cache penetration 、 Cache breakdown and cache avalanche are Redis During the interview and actual development , A problem that often needs to be considered . Many people are concerned about the problem 、 The reason and solution are still not clear enough . In fact, for the three situations , A good solution can be found by carefully analyzing the principle of a generation . This paper defines 、 Case study 、 Several perspectives on hazards and solutions , To help you quickly understand the three questions .

Three more

  1. Cache penetration 、 Cache breakdown and cache avalanche are caused by the absence of data in the cache , It leads to going to the database to query data .
  2. Because the cached data does not exist , All requests go to the database , Therefore, it will lead to excessive pressure on the database and even service crash , The whole system cannot be used .

Cache penetration

Definition : Cache penetration is because the data requested by the client does not exist in the cache , Then go to the database , However, the database has no data to be queried by the client , As a result, every request will go through the database query operation . give an example : When the client requests product details , Carry a commodity ID, At this time, the product ID It doesn't exist ( Whether in the cache or in the database ). Cause every request for this ID The data information of commodities will go to the database . harm : Because the data corresponding to the requested parameter does not exist at all , This will cause the database to be requested every time , Increase the pressure on the database or service crash , It even affects other business modules . It often happens to users Malicious request In the case of . Solution :

  1. Cache a... According to the requested parameters null value . And set an expiration time for this value , You can set the time a little short .
  2. Using the bloon filter , First, filter through bloom filter , If it exists in the filter, query the database , Then add it to the cache . If it does not exist, the client data is returned directly .
  3. Because cache penetration may be a malicious request initiated by the user , Users can be ip Write it down , For malicious ip Request a ban .

Cache breakdown

Definition : Cache breakdown is due to Some hot spots key non-existent , This leads to database queries . Increased database pressure . This pressure can be instantaneous , It may also be more lasting . give an example : There are one or more popular products , When the user views the product details, the user carries the product details ID To get the details of the product . At this point, the data in the cache has just expired , Therefore, all incoming requests must go to the database to query . harm : Relative to cache penetration , The data exists in the database , Just because the cache has expired , It leads to going to the database once , Then add to the cache , The next request will go to the cache normally . The so-called harm is also aimed at the harm at the database level . Solution :

  1. Add mutex lock . For the first request , Found no data in cache , At this point, the query database is added to the cache . In this way, the subsequent requests do not need to go through the database query .
  2. Increase business logic expiration time . When setting cache , We can add a cache expiration time . Every time I read , Make a judgment , If the expiration time is less than the current time , Trigger a background thread , Go to the database and pull the data , Then update the cached data and the expiration time of the cache . In fact, the principle is to extend the cache duration at the code level .
  3. Data preheating . The implementation adds data to the cache through the background . For example, before the second kill scene , Just add the inventory of goods to the cache , So when the user request comes , Just go to cache .
  4. Never expired . When setting the expiration time for the cache , Let it never expire . Start a thread in the background , To maintain these cache expiration times and data updates .

Cache avalanche

Definition : We talked about cache breakdown earlier , Because some hotspots in the cache key invalid , Cause a large number of requests to go to the database . However, cache avalanche is the same , It's just that this is more serious , Is most of the cache key invalid , Not one or two key invalid . give an example : In an e-commerce system , The commodity data under a certain category is invalid in the cache . However, many requests in the current system are the product data under the category . This causes all requests to go through the database query . harm : Due to the sudden influx of requests , Every request must go to the database for query . Database instant traffic influx , Seriously increase the burden of database , It can easily lead to the direct paralysis of the database . Solution :

  1. The cache time is random . Because at some point , A lot of cache failures , This indicates that the expiration time of the cache is relatively centralized . We directly set the expiration time as not centralized , Random disruption . In this way, the cache expiration time is relatively less concentrated , There will not be a large number of requests to query the database at the same time .
  2. Multi level cache . Not just by Redis To do caching , We can also use memcached To do caching ( Here is just one example , Other caching services can also be used ). Cache data , Yes Redis Make a cache , Yes memcached Make a cache . If Redis It doesn't work , We can go memcached. But this increases the difficulty of system architecture , And all kinds of other problems , For example, cache multi-level updates .
  3. The mutex . In cache breakdown, we mentioned the use of mutexes to achieve , We can also use it in the case of avalanches .
  4. Set expiration flag . In fact, you can also use the permanent non expiration mentioned in cache breakdown . When requested , Determine expiration time , If the expiration time is approaching, set an expiration flag , Trigger a separate thread to update the cache .

summary

  1. Cache penetration is because the database itself does not have the data .
  2. Cache breakdown and cache avalanche are the reasons why the data exists in the database , Just the data in the cache is invalid , As a result, the database needs to be queried again and then added to the cache .
  3. Cache breakdown is for some hotspots key, Cache avalanche is a large area cache failure . The two principles are actually the same , It's just for caching key It's just different .


版权声明
本文为[An independent pig]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/11/20211109095357346D.html

Scroll to Top