Cache breakdown, cache penetration, and cache avalanche

Source: Internet
Author: User
Cache breakdown

Definition: the cache key generally has an expiration time. If a key expires, when a large number of concurrent requests access this key, all these requests will reach the DB, this results in a high load on the database in an instant.

Solution: 1. Set mutex and mutex. When the cache is invalid, access the database immediately from time to time, but use the operation of the cache tool to successfully perform operations with return values, such as setnx (set if not exit) of redis, add of memcache, use setnx to implement the lock effect.

Disadvantage: it may cause deadlocks or thread pool blocking.

2. Use mutex lock in advance

The redist timeout value is timeout1, the value timeout value is timeout2, and timeout2 <timeout1. When timeout2 times out, timeout2 is extended. And reset it to redis.

3. never expire

Do not set the expiration time.

Set the expiration time to the value. If it is about to expire, a backend asynchronous thread is used to construct the cache, that is, the logic expires.

Cache penetration

Definition: a key that does not exist in the database is used for access. The key value does not exist in the Database and does not exist in the natural cache. The request is sent directly to the database. If the concurrent access to this key is too large, the database will be overwhelmed.

Solution:

1. Use a filter to hash all data that cannot exist in the database to a large bitmap. If the key does not exist in the database, it will be intercepted by bitmap.

2. Check the key that is null and the resume key value pair in the cache, which is a little shorter than the expiration time, such as 5 minetes.

Cache avalanche

Definition: when a large amount of data in the cache expires at the same time, a large number of requests will be directly transferred to the database, resulting in excessive pressure on the database.

Solution: 1. Lock and add a queue, such as MQ, to ensure that the cache is written in a single thread, so as to avoid a large number of concurrent accesses to the database when the key fails.

2. Stagger the cache expiration time. For example, adding a random value to the original expiration time, such as 1-5 minutes, reduces the repetition rate of the expiration time and reduces the probability of collective failure.

Summary

A cache avalanche is an event in which a large number of keys are invalid at the same time, while cache breakdown and cache penetration are all invalid for a single key. A large number of requests access this key. The difference is that cache breakdown, the value corresponding to the key exists in the database, while the cached key and value do not exist in the Database and may be used for malicious attacks.

Cache breakdown, cache penetration, and cache avalanche

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.