[Redis] Cache penetration, cache breakdown, cache avalanche

Cache penetration-not found

What is it

We know that our Redis is used to cache some data to prevent every SQL statement from querying our database

If we go to query the primary key with id = -1, it will definitely not be queried in our Redis cache at this time, and we will go to our database to query

At this time, hackers take advantage of this problem to constantly query our server for id = -1 or non-existent data, and will continue to access our MySQL, which may eventually cause our MySQL to crash.

Solution

Bloom filter

  • Explanation: Add a bloom filter to our Redis layer. The bloom filter stores the value in the database. If it is not in our bloom filter, return it directly and prevent him from entering
  • Bloom filter principle

Create a cache empty value

  • Explanation: That is, every time we query our database, we will make a key-"" empty key-value pair in Redis and set an expiration time so that we will not be queried during the second query Database.
  • Disadvantages: consumes a lot of cache space, and there is a particularly stupid problem~~~
  • If I were a hacker, I would directly use UUID to query~~~ I cached it every time, and it crashed directly~

Cache breakdown-found, too many, broken

What is it

When the subject first saw the name, Nima had a heartbreak. These two are so the same.

Suppose, double 11 is about to go right now, and everyone is about to snap up the Super lights of Xiaohuang’s house, which is about 300 million people.

Suddenly, the Redis cache of this Super lamp on my server suddenly expired. At this time, 300 million people suddenly came in for queries, and my database was accessed by 300 million people at the same time, so I was directly confused.

It’s similar to smashing a hole from my cache, puncturing my cache, and reaching my database

Solution

Set hotspots to never expire

  • I directly set the cache data of my Super lamp to not expire. You can check it, and I will lose if I pass through.

Add distributed lock

  • As the old saying goes, as long as multi-threading and high concurrency are involved, locking can basically solve all problems. When users want to access our database, I add a distributed lock to allow one thread to enter, and the others wait, and then put the data Put it in our cache so that it won’t cause breakdown.
    Insert picture description here

Cache avalanche

What is it

When I first saw Avalanche’s understanding, I was a little confused

  • Understanding: Cache avalanche means that the cache set expires and becomes invalid in a certain period of time. Redis is down!

This seems to be similar to breakdown

Breakdown refers to a cache expired, 300 million users query our expired data.
Avalanche refers to multiple caches expired. 300 million users query multiple data of ours, which also penetrated our database.

Insert picture description here
Therefore, in order to prevent the occurrence of the above incidents, we usually stop some services

For example: Double 11, in order to ensure normal shopping, we will stop refunds and other services

solution

Redis high availability

Simple and easy to understand, I add a few more Redis to build a cluster. Using the sentinel, a master or slave hung up, it didn’t affect my future affairs at all

Current limit downgrade

As mentioned earlier, there is nothing that cannot be solved by locking

When our cache fails, we lock to ensure that the thread that reads the database, for example, for a certain Key, we only allow one thread to read

Data warm-up

We know that today is Double 11, which are the more important data, such as the lights of Xiaohuang’s shop

We write all the lamp data of this shop into our cache, so that 300 million people come to buy the lamp and it will not cause an avalanche event.

Guess you like

Origin blog.csdn.net/qq_40915439/article/details/108567982