Redis cache issue

Redis is used to support caching. What caching problems does it have?

1. Consistency issues

Data in distributed systems is generally weakly consistent, or eventually consistent. Because of the CAP principle, in terms of consistency, availability, and partition tolerance, a distributed system can still provide services that meet consistency or availability when it encounters a node or network partition failure ) Only two of the three can be selected. Since the distributed system must have partition fault tolerance, only consistency and availability are optional. The second benefit comes first, so you can only sacrifice consistency.

The way to deal with the consistency problem of Redis cache is to adopt appropriate strategies to reduce the probability of data inconsistency between the cache and the database, including the appropriate cache update strategy, update the cache in time after updating the database, and increase the retry mechanism when the cache fails. But there is no guarantee of strong consistency between the two. If the project's requirements for caching are strongly consistent, then don't use caching.

2. Redis avalanche

If all KEYs fail together, then all requests will fall on the database instantaneously, and the database will inevitably be unable to bear it. The only way is to restart the database, but after the restart, all requests will still fall on the database together, killing the database again.

The strategy is to set different expiration times for different KEYs; or to distribute different keys to different servers.

3. Redis cache penetration

Request data that does not exist in the database at all. If it does not exist in the database, then it certainly does not exist in redis. However, the outside world or hackers continue to request one after another, and continuous attacks cause the database to be under great pressure and will seriously destroy the database.

Coping strategies:
1) Add verification at the interface layer, such as user authentication, parameter verification, and illegal verification directly return, such as id for basic verification, id <= 0 to intercept directly.
2) Apply Bloom Filter, if the data does not exist in the database, return directly

4. Redis cache breakdown

Refers to a Key is very hot, carrying a large number of requests non-stop, large concurrent access to this point, when the Key fails, the continuous large concurrency directly falls on the database, just at the point of this Key Punctured the cache. Cache penetration is similar to avalanche, but for different reasons. Avalanche is a large area of ​​cache failure, and cache penetration is a continuous request for data.

Strategy: Set hotspot data to never expire, or add a mutual exclusion lock.

Reference article: The
most complete collection of Redis technology in human history, do not look at the lifelong series of regret

1134 original articles published · 354 praised · 3.49 million views

Guess you like

Origin blog.csdn.net/leftfist/article/details/105185305