How to deal with cache invalidation, cache penetration, cache concurrency, and so on

Source: Internet
Author: User

Cache invalidation :

The main factor that causes this is high concurrency, we generally set a cache expiration time, there may be some will set 5 minutes ah, 10 minutes of these; High concurrency may occur at a time when a lot of cache is generated, and the expiry time at the same time, this time can be raised-when the expiration time , these caches are invalidated at the same time, and all requests forwarded to the DB,DB may be overloaded.

Processing method:

A simple solution is to spread the cache expiration time, not so that the cache length is set to 5 minutes or 10 minutes; For example, we can add a random value based on the original failure time, such as 1-5 minutes random, so that the repetition rate of each cache expiration time will be reduced, It is difficult to induce collective failure events.

The avalanche effect that occurs when the cache fails, all requests are placed on the database, so it is easy to reach the bottleneck of the database, resulting in the service not being provided properly. Try to avoid the occurrence of this scenario.

Cache penetration :

Scene: Refers to the query of a certain non-existent data, because the cache is not hit when the passive write, and for fault-tolerant considerations, if the data from the storage layer is not written to the cache, which will cause this non-existent data each request to the storage layer to query, lost the meaning of the cache.

When the traffic is large, this situation, always request the DB, it is easy to cause the service to hang up.

Processing method:

Method 1. Add a step in the encapsulated cache set and get section, if the query a key does not exist, the key is a prefix to set an identity key, the next time to query the key, the first query identity key, if the identity key exists, return a contract good non-false or null value, and then the app to do the corresponding Processing so that the cache layer is not penetrated. Of course, this verification key does not expire for too long.

Method 2. If the data returned by a query is empty (whether the data does not exist or is a system failure), we still cache the empty result, but its expiration time will be very short, typically only a few minutes.

Method 3. Using the bitmap filter to hash all possible data into a large enough one, a certain non-existent data is intercepted by this bitmap, thus avoiding the query pressure on the underlying storage system.

Cache concurrency :

Scene: When the site concurrent access high, a cache if it fails, there may be multiple processes simultaneously querying the DB, while setting the cache, if the concurrency is really large, this can also cause the db pressure is too high, there is frequent cache update problem.

Processing method: Lock the cache query, if key does not exist, lock, then check the db into the cache, and then unlock, and other processes if the lock is found to wait, and then after the unlock to return data or into the DB query.

How to deal with cache invalidation, cache penetration, cache concurrency, and so on

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.