MySQL and Redis cache coherency achieve

First, real-time synchronization

For strong consistent high demand, real-time synchronization program should be used, that is, the query cache can not find another query from the DB, save it to cache; updating the cache to update the database, then set the cache expiration (not recommended to update the cache contents, directly set the cache expires)

@Cacheable:查询时使用,注意Long类型需转换为String类型,否则会抛异常。
@CachePut:更新时使用,使用此注解,一定会从DB上查询数据
@CacheEvict:删除时使用
@Caching:组合用法

Second, asynchronous column

For a high degree of concurrency, the column can be asynchronous to synchronous mode, and the like may be employed kafka messaging middleware processing messages of production and consumption.

Cache penetration

Cache penetration refers to a certain query data does not exist, because the cache is needed from the database query is not hit, can not find the data cache is not written, it will lead to a time that does not exist in the data request should go to the database query, resulting in cache penetration.

Solution:

Persistence query cache less than a null result, first determine whether the cache exists (key), if there is a direct return empty, then no query after query returns.
Attention to the need to clear the query key when insert, or even if there is value in the DB can not find out (of course you are provided with an empty cache expiration time)

Cache avalanche

If the cache is concentrated over a period of time has expired, a lot of cache penetration occurs, all queries fall on the database, resulting in a cache avalanche.

Solution:

This is not a 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.

Hot Key

Hot key: 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 crashes.

Solution:

1, a lock, a single synchronized, lock etc., distributed in a distributed lock.
2, cache expiration time is not set, but in the key in the corresponding value. If the detected stored for longer than the expiration time asynchronous update the cache.
3, a set value of t1 in value than the expiration time t0 small expiration time t1 when expired, extend and make the update cache operation t1.
4, set the label cache, the cache tag set an expiration time, after the label cache expires, the need to update the actual asynchronous cache.

Published 179 original articles · won praise 185 · views 80000 +

Guess you like

Origin blog.csdn.net/Sophia_0331/article/details/104796763