当前位置:网站首页>Redis (17) -- redis cache related problem solving
Redis (17) -- redis cache related problem solving
2022-04-23 03:28:00 【Jiang Hao】
13、 ... and )Redis Cache related problems solved
13.1) Cache penetration
13.1.1) Problem description
key The corresponding data does not exist in the data source , Every time for this key The request for could not be fetched from the cache , Requests are pushed to the data source , That could crush the data source ; For example, use a non-existent user id Get user information , No matter cache or database , If hackers exploit this vulnerability, they may crush the database .
Cache penetration :1) The application server is under more pressure 2)redis Hit rate reduced 3) Keep accessing the database to query information, so as to crash the database
Cache penetration reason :1)redis No data to query 2) There are a lot of anomalies url visit 【 A malicious attack 】
13.1.2) Solution
There must be no data that cannot be cached or queried , Because the cache is written passively on Miss , And for the sake of fault tolerance , If no data can be found from the storage tier, it will not be written to the cache , This will cause the non-existent data to be queried by the storage layer every time it is requested , It loses the meaning of caching .
Solution :
-
Cache null values : If the data returned by a query is empty ( Whether the data doesn't exist or not ), Still put this empty result (null) Cache , Setting the expiration time for empty results can be very short , Up to five minutes ;【 A temporary plan 】
-
Set up an accessible list ( White list ): Use bitmaps Type defines a list of accessible , list id As bitmaps The offset , Every visit and bitmap Inside id Compare , If you visit id be not in bitmaps Inside , To intercept , Access not allowed ;【 The efficiency is not high 】
-
Use of Blum filter : Hash all possible data to a large enough bitmaps in , A certain nonexistent data will be bitmaps Intercept , Thus, the query pressure on the underlying storage system is avoided ;
The bloon filter (Bloom Filter) yes 1970 Proposed by bron in . It's actually a very long binary vector ( Bitmap ) And a series of random mapping functions ( hash function ); The bloom filter can be used to retrieve whether an element is in a collection . Its advantage is that the space efficiency and query time are far more than the general algorithm , The disadvantage is that it has certain error recognition rate and deletion difficulty
-
Real-time monitoring : If I found Redis It's starting to drop , Need to check access objects and data , Cooperate with operation and maintenance personnel , You can set up a blacklist to restrict services
13.2) Cache breakdown
13.2.1) Problem description
key The corresponding data exists , But in redis Medium overdue , At this time, if a large number of concurrent requests come , These requests usually find that the cache is expired from the back end DB Load data and reset to cache , This is the time A large number of concurrent requests may instantly put the back end DB Overwhelmed .
Cache breakdown :1) Database access pressure increases instantaneously 2)redis There's not a lot of it key Be overdue 3)redis The normal operation
Cache breakdown reason : redis One of the key Out of date , A large number of concurrent requests key, After the cache is found to be out of date, it is from DB Load cause DB Run away
13.2.2) Solution
key It may be accessed at some point in time with super high concurrency , It's a very “ hotspot ” The data of . This is the time , A question needs to be considered : The cache is “ breakdown ” The problem of .
Solution :
-
Preset hot data : stay redis Before the summit visit , Put some hot data into redis Inside , Increase the hot data key Duration
-
Real time adjustments : What data is hot on the spot , Real time adjustments key The expiration time of
-
Using locks :
-
When the cache fails ( Judge that the value is empty ), Not immediately load db;
-
First, use some operations of the cache tool with the return value of the successful operation ( such as Redis Of SETNX) Go to set One mutex key;
-
When the operation returns success , Proceed again load db The operation of , And reset the cache , Finally delete mutex key;
-
When the operation returns failure , Prove that there are threads in load db, The current thread sleeps for a period of time and then retries the whole get Caching method .
13.3) Cache avalanche
13.3.1) Problem description
key The corresponding data exists , But in redis Medium overdue , At this time, if a large number of concurrent requests come , These requests usually find that the cache is expired from the back end DB Load data and reset to cache , At this time, a large number of concurrent requests may instantly put the back end DB Overwhelmed .
The difference between cache avalanche and cache breakdown is : Cache avalanches are targeted at many key cache , Cache breakdown is some kind of key;
The normal access is shown in the figure below :
The cache avalanche moment is shown in the figure below :
Cache avalanche :1) The pressure on the database is increasing 2) Server crash
Cache avalanche reason :1) In very little time , A large number of key The centralized expiration of , Instant handle DB Overwhelmed
13.3.2) Solution
The avalanche effect of cache failure has a terrible impact on the underlying system !
Solution :
-
Building a multi-level cache architecture :nginx cache + redis cache + Other caches (ehcache etc. );【 The architecture is complex 】
-
Use locks or queues : Lock or queue to ensure that there will not be a large number of threads to read and write the database at one time , So as to avoid a large number of concurrent requests falling on the underlying storage system in case of failure ;【 Not for high concurrency 】
-
Set expiration flag to update cache : Record whether the cache data is out of date ( Set the lead amount ), If it expires, it will trigger another thread to update the actual situation in the background key The cache of ;
-
Spread the cache expiration time : For example, we can add a random value to the original failure time , such as 1-5 Minutes at random , In this way, the repetition rate of each cache expiration time will be reduced , It's hard to trigger a collective failure .
版权声明
本文为[Jiang Hao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230326121156.html
边栏推荐
- Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)
- Problem B: small challenge
- PWA I'm here
- poi根据数据创建导出excel
- Detailed explanation of socket programming send() and recv() functions
- Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
- Punch in: 4.22 C language chapter - (1) first knowledge of C language - (11) pointer
- Charles uses three ways to modify requests and responses
- Optimization of especially slow startup in idea debugging mode
- It can receive multiple data type parameters - variable parameters
猜你喜欢
Peut recevoir plusieurs paramètres de type de données - paramètres variables
【微服务】(十)—— 统一网关Gateway
2022 团体程序设计天梯赛 模拟赛 L1-7 矩阵列平移 (20 分)
关于idea调试模式下启动特别慢的优化
2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
"Visual programming" test paper
Test questions (2)
Applet - WXS
MySQL keyword group_ Concat, combined connection query
Development record of primary sensitive word detection
随机推荐
TCP three handshakes and four waves
C abstract class
String input problem
超好用的Excel异步导出功能
打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
Unity knowledge points (common core classes)
Course design of Database Principle -- material distribution management system
JS takes out the same elements in two arrays
Super easy to use [general excel import function]
. net webapi access authorization mechanism and process design (header token + redis)
Oracle query foreign keys contain comma separated data
poi根据数据创建导出excel
Unity knowledge points (ugui 2)
Experiment 5 components and event handling
Codeforces round 784 (Div. 4) (AK CF (XD) for the first time)
2022 group programming ladder simulation l2-1 blind box packaging line (25 points)
JS - accuracy issues
Iotos IOT middle platform is connected to the access control system of isecure center
Visual programming -- how to customize the mouse cursor