The English explanation of redis cache avalanche is a running bison, which means that after the cache layer crashes, the concurrent traffic will be like a galloping bison, with a large amount of back-end storage. This is due to some reasons, and the overall failure To provide services, all requests will reach the storage layer, and the number of calls to the storage layer will increase dramatically, causing the storage layer to hang up.
1. What is an avalanche
Because the cache layer carries a large number of requests, it effectively protects the storage layer, but if Due to some reasons, the cache cannot provide services as a whole, so all requests will reach the storage layer, and the number of calls to the storage layer will increase dramatically, causing the storage layer to hang up. The English explanation of cache avalanche is running bison, which means that after the cache layer crashes, concurrent traffic will be like a galloping bison, with a large amount of back-end storage.
One scenario where this problem exists is: when the cache server is restarted or a large number of caches are concentrated in a certain period of time, so that when they fail, a large amount of data will directly access the DB, which will cause a huge problem for the DB. pressure.
2. Solution
(1) Set up the high availability of the redis cluster and DB cluster. If redis goes down, it can be immediately replaced by another The machine takes over. This can prevent some risks.
(2) Use mutex lock
After the cache expires, control the number of threads reading and writing the database through locking or queuing. For example: only one thread is allowed to query data and write cache for a certain key, while other threads wait. If it is a stand-alone machine, you can use synchronized or lock to solve it. If it is a distributed environment, you can use the redis setnx command to solve it.
(3) Different keys can be set to different expiration times to make the cache invalidation time points inconsistent and try to achieve an even distribution.
(4) Never expire
Redis is set to never expire. This ensures that there will be no hot spots, that is, it will not expire physically.
(5) Resource protection
Using Netflix's hystrix, you can isolate the thread pool of various resources to protect the main thread pool.
Recommended tutorial: "Redis Tutorial"
The above is the detailed content of What is redis avalanche. For more information, please follow other related articles on the PHP Chinese website!