redis cache penetrate 00

Cache penetration

Cache penetration, refers to certain data query a database does not exist. Normal use caching process generally, data query to cache queries, if there is no key or key has expired, then query the database, and the query to the object into the cache. If the database query object is empty, the cache is not putting them in.

 

First, set a default value, simple and crude:

If you query the database also empty directly set a default value stored in the cache, so the second time to get there is value in the buffer, but will not continue to access the database, this approach is the most simple and crude.

Code flow

1. The primary key parameters of the incoming object ID

2. Get an object from the cache based on key

3. If the object is not empty, the direct return

4. If the object is empty, database query

5. If the query objects from the database is not empty, then into the cache (set expiration time)

Imagine this situation, if the incoming parameter is -1, be like? This -1, is sure there is no object. It will always go to query the database, and each query are empty, every time it will not be cached. If malicious attacks, you can take advantage of this loophole, putting pressure on the database, even crushed database. Even using UUID, is also very easy to find a KEY does not exist, attack. I'm at work, we will adopt the cache null value, that is, [the code] process in Step 5, if the object from the database query is empty, but also into the cache, the cache expiration is set just a short time, such as setting 60 seconds.

 

 

 Second, according to the data cache Key rules. Such as set-top box, the cached data to the Mac Key, Mac there is a rule, if it does not comply with the rules to filter out, so you can filter part of the query. In doing cache when planning, Key has certain rules, you can take this approach. This approach can only ease the pressure part of the filtration system and independent inquiry, but no cure.

 

 

Third, the use Bloom filter, there may be a hash of all data to a BitSet large enough, there is no data will be blocked off, thus avoiding pressure on the underlying query of the storage system. About Bloom filter, Detail View: Bloom filter based on the BitSet (Bloom Filter)

 

Note: large cache penetration, causes the cache avalanche.

 

Guess you like

Origin www.cnblogs.com/chengege/p/11073317.html