Redis: cache avalanche, breakdown, penetration

Avalanche (keys have expired)

  1. 现象: If redis uses regular refresh (redis), all the keys in redis may be invalid at a certain moment, and a large number of requests will be made to request the database, and the database will hang immediately, and will be killed by new traffic after restarting.
  2. 解决:
    2.1. Setting a random value for the expiration time of each key can ensure that the data will not fail in a large area at the same time.
    2.2. Or set the hotspot data to never expire, just update the cache if there is an update operation (for example, if you update the homepage product, you can refresh the cache (but the periodic active deletion is random), you can write a script to add new products Click a new one, so that the memory will eliminate those very old ones)

Breakdown (a large number of accessed hotspot keys suddenly expire)

  1. 现象: Cache breakdown refers to a key that is very hot, constantly carrying large concurrency, and the large concurrency concentrates access to this point. When the key is invalid, the continuous large concurrency will break through the cache and directly request the database , It's like digging a hole in an intact barrel.
  2. 解决:
    2.1 Set hotspot data to never expire.
    2.2 Locking the entire process of accessing redis->accessing mysql->writing redis will ensure that after the key fails once, a large number of users will not access the database during the redis writing process. You can make the thread wait for the lock over time, and then visit redis if you can't wait, maybe the data of this key will already exist again.

Penetration (key does not exist in redis/db)

  1. 现象: For data that is not in the cache or the database, the user keeps making requests, such as inquiring data with id=-1. At this time, the user is likely to be a hacker. In this way, a large number of databases will be searched, and then it will hang again. Normally, if the database cannot be found, it will not be placed in redis.
  2. 解决:
    2.1. Perform legal verification on parameters and verify users. If an ip initiates a large number of requests per unit time, it will be blocked.
    2.2 If you don’t get it in the database, write the value of the corresponding key as null and write it to redis, but you should set the expiration time of such a key to be shorter. After all, hackers are only a very small number. If redis is all illegal The key, this will also cause an avalanche. .
    2.3 Redis also has an advanced usage Bloom Filter (Bloom Filter), which can also prevent the occurrence of cache penetration. Its principle is also very simple, using efficient data structures and algorithms to quickly determine whether your Key is in It exists in Redis. If it does not exist, you can return. If it exists, you can check the DB and refresh the KV and then return.

to sum up

  • Beforehand: Redis high availability, master-slave + sentinel, Redis cluster, to avoid total crash
  • In the event: local ehcache cache + Hystrix current limit + downgrade to prevent Mysql from hanging up.
  • After the event: Redis persists RDB+AOF, once restarted, it quickly restores Redis data.

Explain the current limiting and degradation: the current limiting component ensures that only how many requests per second can pass. As long as the database is not dead, that is to say, for users, 3/5 of the requests can be processed. As long as 3/5 of the requests can be processed, it means that your system is not dead. For users, the page may not be displayed after a few clicks, but the page can be displayed once after a few more clicks. This is the most common among the current mainstream Internet companies. Are you curious about what happened to a certain celebrity? You find that the interface is blank when you go to Weibo, but some people enter directly again. It came out several times, and now you know it, that was a downgrade, sacrificing some users' experience in exchange for server security.

Guess you like

Origin blog.csdn.net/qq_42576687/article/details/109357306
Recommended