Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the redis cache avalanche?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you the cache avalanche of redis. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. What is an avalanche?

Cache avalanche refers to a large number of data in the cache to the expiration time, and the huge amount of query data, resulting in excessive pressure on the database and even downmachine. Cache avalanche means that the cache set expires in a certain period of time.

One of the reasons for the avalanche, for example, at the time of writing, it will soon be double 12: 00, and there will soon be a wave of rush buying. This wave of product time is concentrated in the cache, assuming that it is cached for an hour. Then by 01:00 in the morning, the cache of these goods will have expired. And the access query for this batch of goods, all fall on the database, for the database, it will produce periodic pressure peaks.

In fact, centralized expiration is not very fatal, but the more deadly cache avalanche is the downtime or network outage of a node in the cache server. Because of the natural cache avalanche, the cache must be created in a certain period of time, so the database can withstand the pressure at that time. It's just a cyclical pressure on the database. The downtime of the cache service node, the pressure on the database server is unpredictable, and it is likely to crush the database instantly.

two。 Solution method

(1) set the high availability of redis cluster and DB cluster. If the redis goes down, it can be replaced by another machine immediately. This prevents some of the risks.

(2) use mutex

After the cache expires, the number of threads reading and writing to the database is controlled by locking or queuing. For example, only one thread is allowed to query data and write cache for a key, while other threads wait. In the case of a stand-alone machine, you can use synchronized or lock to solve the problem. In a distributed environment, you can use the setnx command of redis to solve the problem.

(3) different key can be set for different expiration time, so that the cache expiration time is different and the average distribution can be achieved as far as possible.

(4) never expire

The setting in redis does not expire forever, which ensures that there will be no hot issues, that is, it will not expire physically.

(5) Resource protection

Using netflix's hystrix, you can do thread pool isolation for various resources, thereby protecting the main thread pool.

The above is the redis cache avalanche shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report