<> preface :

Application background processing query data request process ( Normally ):

* Application background receives request
* Query cache , If so , The corresponding data is extracted from the cache , Back to front end
* If the cache does not find the corresponding data , Then the database query is taken
* The database found the corresponding data , Store this data in the cache , And return the data to the front end
* without , Then go back to the front end and don't find it
What is cache penetration , Cache avalanche and cache breakdown , How to prevent ?

Cache penetration

describe : Access to data that must not exist , This causes the request to go directly through the cache layer , To access the database ( Because normally speaking , This nonexistent data cache layer must not exist ). If a large number of such requests come at the same time , It will lead to excessive pressure on the database layer , It can even lead to downtime 【 This is also a means of attack by hackers 】

Solution :

1. When the database layer query is empty , This result is also cached , At the same time, set an appropriate expiration time according to the actual situation . In this way, the same request will be returned directly through the cache layer , At the same time, avoid taking up too much memory
2. Use the bloon filter , Filter out illegal requests in advance 【 Not all illegal requests can be filtered completely 】( principle : Hash all possible data to a large enough one bitmap in , A data that must not exist will be
this bitmap Intercept it )

Cache avalanche

describe : Refers to large area cache failure , The request cannot be returned in the cache layer , Results in a large number of requests directly accessing the database layer . There are two ways to cause cache avalanches :
1, The cache layer has a large number of key Expired at the same time
2, Cache server down

Solution :
1. Set cache layer key At the expiration time , Add a random number . This can avoid a lot of key Expired at the same time
2. The cache layer uses cluster deployment , as redis Cluster in a certain redis When the server is down , other redis The server can still provide caching service . Ensure that the cache layer is always available .

Buffer breakdown

describe : Refers to a large number of concurrent requests when accessing one or some data , This or these data just expired in the cache layer , Results in a large number of requests going directly to the database layer . And cache penetration ( Cache penetration refers to accessing data that must not exist in the database , Cache breakdown refers to the existence of the accessed data in the database layer , It just doesn't exist in the cache layer )

Solution :
1. Set hotspots key Never expire
2. Use mutex , as redis Of setNx( Command in the specified key When it doesn't exist , by key Sets the specified value )
public function getData(string $key) { //todo Cache the corresponding data redis get($key) if (
' No data ') { //todo Set mutex , as redis setNx, At the same time, set the expiration time of the lock , Prevent the lock from being released if (' Setup succeeded ') { //todo
Go to the database to query the data //todo Store the data returned from the database into the cache //todo Release the lock redis del } else { //todo
Setup failed , Indicates that another thread has acquired the lock , Wait for a certain time to try to get the data again sleep(60); $this->getData($key); } } //todo
Return data to front end }

©2019-2020 Toolsou All rights reserved,
c Language implementation 《 Student management system 》 No hole is the future of mobile phone ? There are still many problems to be solved Junior , A little sense , Just for mutual encouragement How to use Vue Used in Echarts Visual Library The 11th Blue Bridge Cup Java The second provincial competition B Group part Python- be based on OpenCV Contour fill for flooding algorithm hole filling 【C#】 The realization of student achievement information management system List Common interview questions in the collection and simple ideas China Mobile Science Popularization : Why do mobile networks call “ Cellular mobile network ”【Golang Basic series 10 】Go language On conditional sentences if