当前位置:网站首页>16 application problem solving
16 application problem solving
2022-06-25 05:37:00 【Which floor do you rate moto】
Catalog
16.1 Cache penetration
16.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 .

16.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 ), We still put this empty result (null) Cache , Setting the expiration time for empty results can be very short , Up to five minutes
- 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 .
- Use of Blum filter :( 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 .) Hash all possible data to a large enough bitmaps in , A certain nonexistent data will be This bitmaps Intercept , Thus, the query pressure on the underlying storage system is avoided .
- 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
16.2 Cache breakdown
16.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 , At this time, a large number of concurrent requests may instantly put the back end DB Overwhelmed .

16.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 .
solve the problem :
(1) Preset hot data : stay redis Before the summit visit , Put some hot data into redis Inside , Increase the hot data key Duration
(2) Real time adjustments : What data is hot on the spot , Real time adjustments key The expiration time of
(3) 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 .

16.3 Cache avalanche
16.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 that there are many key cache , The latter is a key
Normal visit

The moment the cache fails

16.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. )
- 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 .
边栏推荐
- Penetration test - right raising topic
- Array introduction plus example 01
- Understand JS high-order function and write a high-order function
- [day40 literature extensive reading] space and time in the child's mind: metallic or atomic
- Depth of binary tree
- CVPR2021-Semi-supervised Domain Adaptation based on Dual-level Domain Mixing for Semantic Segmentati
- Jenkins installation and configuration
- In depth understanding of line height and vertical align
- JS handwriting depth clone array and object
- Uva1103 ancient pictograph recognition
猜你喜欢

Various pits encountered in the configuration of yolov3 on win10

How to add an external header file in vs?

Use of MySQL variables

Instant messaging project (I)

JSON Library Tutorial from scratch (II): parsing digital learning and sorting notes

Create an environment for new projects

Array introduction plus example 01

Dynamic programming example 2 leetcode62 unique paths

Word quickly makes multiple single-sided table labels, number plates, etc

In depth understanding of line height and vertical align
随机推荐
Enhanced paste quill editor
Database query optimization method
Penetration test - right raising topic
SQL lab range explanation
电子协会 C语言 1级 28 、字符菱形
Detailed summary of position positioning
Electronic Society C language level 1 28, character diamond
By inserting a section break, the word header, footer, and page number can start from any page
Use of MySQL variables
Could not find “store“ in the context of “Connect(homePage)
[pan Wai 1] Huawei computer test
Double recursion in deep analysis merge sort
CTFHub-rce
How to install the blue lake plug-in to support Photoshop CC 2017
Create an environment for new projects
Monkey test of APP automation
Array introduction plus example 01
Bind simulation, key points of interpreting bind handwritten code [details]
滲透測試-提權專題
How to add an external header file in vs?