Redis6 application problems - cache penetration, cache breakdown, cache avalanche problems and solutions

cache penetration

Multiple requests for data that does not exist in the database
insert image description here
insert image description here
Solution:
(1) Cache empty values: If the data returned by a query is empty (regardless of whether the data exists), cache the empty result (null), and set the expiration time of the empty result It will be very short, no longer than 5 minutes
(2) Set up an accessible list (white list): use the bitmaps type to define an accessible list, the list id is used as the offset of the bitmaps, each visit and the id in the bitmaps For comparison, if the access id is not in the bitmaps, it will be intercepted and access will not be allowed.
(3) Use Bloom filter
(4) for real-time monitoring: set blacklist to restrict services

cache breakdown

A large number of concurrent requests for a certain hot data that does not exist in the cache, the request hits the database, causing the database to crash. Solution: insert image description here
(
1) Pre-set hot data: before redis peak access, store some hot data in redis in advance Inside, increase the duration of these popular data keys
(2) Real-time adjustment: monitor which popular data on-site, and adjust the expiration time of the key in real time
(3) Use locks:
1. When the cache fails (judging that the value taken out is empty ), not to load db immediately
2. First use some operations of the cache tool with a successful operation return value (such as Redis's SETNX)

cache avalanche

A large number of concurrent requests for batch keys that do not exist in the cache, the request hits the database, resulting in an instant pressure on the database and a crash. The
difference between cache avalanche and cache penetration is that the former is for many key caches, and the latter is a insert image description here
solution for a certain key:
(1) Build a multi-level cache architecture: nginx cache + redis cache + other caches (ehcache, etc.)
(2) Use locks or queues: use locks or queues to ensure that there will not be a large number of threads reading the database at one time Write, so as to avoid a large number of concurrent requests falling on the underlying storage system when it fails, and it is not suitable for high concurrency

Summary:
Cache penetration: A large number of concurrent requests for data that does not exist in the database, because the cache and the database do not exist, resulting in the database being queried all the time and the database crashing.
Cache breakdown: A redis hotspot key has expired, and a large number of accesses use this data, resulting in excessive database access.
Cache avalanche: Query the centralized expiration of a large number of keys in a very small period of time

Guess you like

Origin blog.csdn.net/weixin_45334970/article/details/122882120