<> Cache request process
Redis Cached avalanche , pierce through , Breakdown is a classic problem often encountered in daily work , Let's find out , Their solutions are mainly ： Bloom filter , Distributed lock （ Write next time ）
This is a simple client , Server request Redis Flow chart of , Simply put, when the user accesses the server , If the server needs to request data from the database , Go to the cache first to see if there is any , If yes, return directly , If it doesn't exist, you will go to the database to query and return , This is a normal caching process .
<> Cache avalanche
For example, the scene of a treasure , When the double 11 comes , The number of user visits is very large , So a lot of data is put into Redis Cached in , Corresponding Redis of key, And the cache expiration time is set to three hours , During the double 11 , After shopping for more than three hours , At the same time, all caches are invalidated in an instant , As a result, all requests are made to the database , The database fails to respond in time , At this time, there is no way to provide this service externally .
Simply put ： User visits a treasure ,Redis Medium key Large area failure , Lead to direct communication with the database , This phenomenon is called cache avalanche .
* Set expiration time , Let him not fail at the same time , When setting cache , Random initialization failure time , In this way, all caches will not be invalidated at the same time , Distributed as widely as possible .
* Redis Generally, it is a cluster deployment , We will focus on key Put it on different nodes , Let these hotspot caches be evenly distributed on different nodes .
* Do not set cache expiration time
* Run timed tasks , Constantly refresh the cache , When the cache is about to expire, let it be extended for three hours
<> Cache penetration
Here is a scene , For example, a hacker wants to attack our website ：
The primary keys of the database are from 0 Start increment , There is no negative number , This hacker has been using id<0 To our server ,redis Of course not id, Will be to MySQL Make a request , This causes requests to continue to hit the database , because Redis No interception id<0 Data of , Directly penetrated by such illegal requests .
Simply put ： Cache penetration is Redis There is no such data in the database , Generally, this is not a normal user , Are malicious users .
* When data penetration Redis When calling the database , The null value returned by the database should also be synchronized to Redis Middle to , At least make sure it's illegal id Next request directly from Redis Returned in
* （ Hackers will change different illegal parameters ）, Directly pull black ip
* （ Hackers will change ip）, Verify the validity of the input parameters
* Bloom filter
<> Buffer breakdown
For example, this scene ： On the double 11 , Take father Ma's shoes out for auction , The programmer put the shoe data in Redis In cache , Corresponding to one key, At auction , as one can imagine , Everyone's enthusiasm is very high , It's been going on 4 hour , This shoe cache is invalid , As a result, a large number of requests cannot query the data of this shoe , And then all of them to the database , The database fails to respond in time .
Simply put ： Is to puncture a very hot spot key
* Let cache never expire （ just so so ）
The best way ： Distributed lock （ Lock the database at the request step , At this time, only one thread can grab the lock , That is, only one thread can operate the database , At this time, the pressure on the database is very small , When this hotspot is queried key Later, I will write again Redis in , Other threads that did not grab the lock , Let him sleep for a few milliseconds first , Go again redis Query data in ）
A project can be divided into three stages , Preparation before launching , Preparation during project operation , Treatment measures after project downtime .
For example, before the project goes online , It can be built into a cluster ：Redis colony ,MySQL colony , Own distributed cluster , In this way, a high availability cluster can be formed , The robustness of the system is enhanced .
Some problems occurred during the operation of the project , The treatment measures of current limiting and degradation can be adopted , Prevent a large number of requests from hitting the database , Make the system unavailable .
If the system really hangs up , Some alarm systems can be integrated , Even if we inform our developers , At the same time, restart the project quickly , utilize Redis of aof,rdb Persistence mechanism , To quickly recover cached data , Minimize the time when the system is unavailable