In our practical development and application, the widespread existence of caching mechanism greatly improves the system’s request tolerance threshold to the database. However, in some specific scenarios, we need to understand its possible problems and corresponding solutions to better increase the robustness of our system
1. Cache penetration
In general query scenarios, when a query cannot find the corresponding information from the cache, it will continue to access the database for query, which is called cache penetration. Seemingly normal operation, in fact, when someone maliciously uses data that does not exist to access the server frequently, it may cause system paralysis.
Bloom filter: it is a kind of random data structure with high spatial efficiency. It uses digit group to express a set concisely and can judge whether an element belongs to the set. It is a fast probability algorithm to judge whether there is a set of elements. In some queries, all possible query conditions can be put into this collection, and the collection can be used to filter query conditions before query, so as to avoid the problem of cache penetration.
Empty value cache: after the first query is empty, the query criteria key is stored in the cache, and the cache time is set to a short time, which can deal with a large number of repeated queries in a short time.
2. Cache avalanche
When we use redis for caching operations, we usually set an expiration time for the cache. However, for a large number of systems with the same cache expiration time, it may cause all requests that should be accepted by the cache to be directly requested to the database because the cache fails at the same time in a certain period of time, causing the database to crash. When there is an avalanche, no snowflake feels its responsibility.
Cross expiration time: when setting the cache time, we can randomly set the expiration time of these caches within a reasonable range of time to avoid the same expiration time
3. Cache breakdown
Cache breakdown is a special case of cache avalanche. If a single hotspot’s cache fails due to its expiration time, there will also be a large number of requests to access the database, leading to a crash. Unlike cache avalanche, cache breakdown is more like a cache avalanche at a certain hot point.
Second level cache: set the second level cache for the data with high heat, and stagger the expiration time of the first level cache and the second level cache, so that the request will not penetrate the two-level cache to access the database at the same time