What they get is a text Redis cache and cache penetrate Avalanche (containing solution)

Foreword

As an in-memory database, redis also always going to have a variety of problems, this article is mainly to explain for which two issues: Cache Cache penetration and avalanches. And gives some solutions. These two issues are fundamental question of the interview FAQ.

A cache penetration

1, the concept of

Cache penetration concept is simple, a user wants to query the data and found no redis-memory database, which is a cache miss, then the query to the database persistence layer. Found that there is no, then this query failed. When many users when the cache did not hit, then we went to request a database persistence layer. This database persistence layer will cause a lot of pressure, this time is equivalent to the cache appeared to penetrate.

It should be noted that the difference between breakdown and cache, the cache breakdown, a key means very hot, be accessed concurrently kept carrying big, big complicated by a focus on this point, the moment when the key failure, continued concurrent wore out large caches, direct database requests, like drilling a hole in a barrier.

In order to avoid penetrating the cache fact, there are many solutions. Here are a few.

2. Solution

(1) Bloom filter

Bloom filter is a data structure, spam sites and normal sites combined worldwide, according to statistics there are billions. Internet police to filter these junk sites, you can not go to a comparative database inside a bar, which can use the Bloom filter. Suppose we store one hundred million spam addresses.

Can first have a one hundred million binary bits, and then generate Internet police with eight different random number is (F1, F2, ..., F8) to produce eight fingerprint information (f1, f2, ..., f8). Next, use a random number generator G this fingerprint information is mapped to the eight natural number eight g1 1 to 100 million, g2, ..., g8. Finally, this binary eight positions all set to one. Process is as follows:

Here Insert Picture Description
There is a Skynet police found a suspicious site, trying to gauge whether the site is XX, first suspicious sites mapped to 100 million points on 8-bit array by hash. If there is an eight-point is not a point, it can be judged that the element does not necessarily exist in the collection.

The Bloom filter that is how to solve the cache redis penetrate it? First of all is very simple query parameters may be stored in a hash, when the user wants to query, using the Bloom filter found not in the set, it discards the query is no longer the persistence layer.
Here Insert Picture Description
This form is very simple.

2, empty object cache

When the storage layer is not hit, even if the return null object may also be cached, also sets an expiration time, and then later access the data will be retrieved from the cache, protection of the backend data source;
Here Insert Picture Description
however this method will exist two questions:

If the null value can be cached, which means that the cache requires more space to store more keys, which may be key because it's a lot of empty values;
even for null set an expiration time, or will exist cache inconsistent data layer and the storage layer, there will be a window of time, which will affect the need for consistency of service.

Second, the cache avalanche

1, the concept of

Avalanche cache means, the cache layer error has occurred, it is not working properly. So all requests will reach the storage layer, the call of the storage layer will surge, causing the storage layer will hang.

Here Insert Picture Description

2. Solution

(1) redis availability

The meaning of this idea is that since redis likely to hang up, then I more than a few additional redis, the other can continue to work after such a hang, in fact, is to build clusters.

(2) limiting downgrade

Thought of this solution is that after a cache miss, to control the number of threads read write cache database by locking or queue. For example, a key for allowing only one thread to query the data and write cache, other threads wait.

(3) Data preheating

Heating the meaning of the data is, before the formal deployment, may I first of advance access to data again, this data may be part of a large number of access will be loaded into the cache. Before large concurrent access impending triggered manually load a different cache key, set different expiry time for the point in time of a cache miss as even as possible.

Published 146 original articles · won praise 66 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_38923792/article/details/105286296