Redis study notes ---- penetrate cache, the cache avalanche and hot key

1. Cache penetration

1.1 concept:
caching refers to penetrate a certain query data does not exist, because the cache is needed from the database query is not hit, the cache is not written when finding out the data, which will result in a time that does not exist in the data requests to go to the database query result caching penetration.
1.2 Solution:
After the persistence layer can not find an empty result cache (ie, the first time we can not find, on the buffer is empty), the query cache to determine whether exissts (key), if there is a direct return empty, the query does not return . But note that the need to clear the key is inserted into the query, or even if the DB even if there is value also can not find (of course, you can set an empty cache expiration time)

2. Cache avalanche

2.1 The concept
If the cache is concentrated over a period of time has expired, the occurrence of a large number of cache penetration, all queries fall on the database, resulting in an avalanche cache.
2.2 solution
this is no perfect solution, but can analyze user behavior, try to make time to failure points evenly distributed. Most system designers to consider a way of locking or cache queues to ensure single-threaded (process) to write, in order to avoid failure of a large number of concurrent requests fall on the underlying storage system. I.e. a method:
[1] locks / or distributed lock queue serial access
lock limiting queue - a flow restrictor algorithm:
1] Count
2] Sliding Window
3] Token Bucket bucket the Token
. 4] leaky bucket bucket [1]
queue serial access:
after a cache miss, by locking to control the number of threads or queue read write cache database. For example, a key for allowing only one thread to query the data and write cache, other threads wait.
[2] homogeneously distributed cache expiration

3. Hot key

3.1 The concept:
a key visit very often, when the key failure of a large number of threads to build the cache, resulting in increased load, the system ran collapse.
3.2 Solution:
[1] using the lock, single use synchronized, lock, etc., distributed by Distributed Lock
[2] cache expiration time is not set on the key, but in the key in the corresponding value. If the detected cache expiration time longer than the asynchronous update the cache.
[3] in the value sets a value t1 than the expiration time t0 small expiration time, when t1 expires, extend t1 and do the update cache operation
after [4] provided tab cache tag cache set the expiration time, the tag cache expires, need actual cache asynchronously update

Published 54 original articles · won praise 2 · Views 3985

Guess you like

Origin blog.csdn.net/TheWindOfSon/article/details/104525397