Redis cache penetration and cache avalanche and their solutions

Redis cache penetration and cache avalanche

What is cache penetration?

When a user wants to look up a piece of data, he finds that it is not queried in Redis (the cache is not hit), so he will query the data in the persistence layer database. As a result, the data was not queried in the persistence layer. When many users query data, because the data does not exist in the Redis cache, they are all directed to the persistence layer database to query, which puts a lot of pressure on the persistence layer database. This situation is called cache wear through. (It means that it cannot be queried in the cache or the database)

Insert picture description here
Note:
Different from cache breakdown. Cache breakdown refers to a hot key. Large concurrency accesses this key. When this key becomes invalid, there are still a large number of accesses to this key, but because this key has expired, all requests will be directly persisted. Layer database for access. This situation is cache breakdown, which is different from cache penetration.

Insert picture description here

Cache penetration solution:

  • 1. Use Bloom filter to filter
    all existing data into bitmap. If access does not exist, it will be intercepted by bitmap, so as to ensure that the persistence layer will not be queried.
  • 2. If the queried data does not exist, this data is also cached (the cache is empty).

What is a cache avalanche?

Cache avalanche refers to problems in the cache layer itself, such as: the cache layer is down, or the cached data expires at the same time. Then all query requests will point to the database. This situation is called a cache avalanche.

Insert picture description here

Cache avalanche solution:

  • 1. Build a Redis cluster to ensure that Redis provides normal services.
  • 2. Set different expiration times for the data.

The difference between cache penetration and cache avalanche and cache breakdown:

  • Cache penetration: The accessed data does not exist in the cache or the database.
  • Cache breakdown: The sudden invalidation of the hot key causes all requests to point to the server database.
  • Cache avalanche: The cache is unavailable or all data fails at the same time, causing all requests to point to the database.

Guess you like

Origin blog.csdn.net/nxw_tsp/article/details/108013441
Recommended