In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Redis cache penetration, breakdown, avalanche is what, the article is very detailed, has a certain reference value, interested friends must read it!
Cache avalanche
At some point, a large number of key fails and a large number of users request the database directly, resulting in database downtime. At this time, Redis is in vain, and a large number of user requests can not be hit, resulting in excessive pressure on the database server. Redis itself is used to reduce the pressure on the database, so how to solve it at this time?
Cache avalanche solution
1. Randomly set the expiration time of the cache to prevent a large amount of cached data from expiring at the same time.
Var time = Math.random () * 10000; > setex name time mango
two。 If you are in a clustered environment, distribute the hot spot data evenly in different cache databases.
3. Can also be hot data never expired, if there is a data update can be directly updated redis.
> set name mango > set name zhangsan # modify the value cache breakdown of key
A key in the cache is very hot. At a certain time, the key just expires, and a large amount of concurrent data breaks through the cache direct request database, resulting in excessive pressure on the database. In serious cases, it will cause the database to crash, which is called cache breakdown. For example: Weibo hot news, a star has an affair, and then a large number of fans brush articles and comments, suddenly request this Weibo cache expires, then a large number of requests directly to the database, establish a large number of connections, the database crashed before it reacted.
Cache breakdown solution
1. Setting this key will never expire
> hset 156486453215 title "mango hot search" describe "mango is so handsome"
two。 The program controls the number of requests to the database, such as adding mutexes and deferring requests (requests after 1 second)
Public static string GetData (string key) {/ / look up data from redis string result = RedisHelper.GetDataByKey (key); if (string.IsNull (result)) / / get empty data {lock (obj) / / Lock resource {/ / get data from database result = DB.GetDataByKey (key) / / verify if (! string.IsNull (result)) {RedisHelper.SetData (key,result); / / cache and return return result;}} Thread.sleep (1000); / / get some sleep}}
The meaning is probably like this, the code, ha, understand the meaning is the most important.
3. The server is cut off, downgraded, limits the number of visits per user, and returns a fixed recommendation page if the data is not available.
Cache penetration
Users keep asking for data that is not cached and the database is not available, and the server frantically establishes a connection with the database to overwhelm the database. For example: according to the product id to get product details of the interface, then I use an id=-1 to request, that certainly can not get any data. In general, in such a situation, it is possible that the server has been maliciously attacked.
Cache traversal solution
1. Add some checks to the interface layer requested by the user, such as user authentication verification, parameter passing value verification and other illegal parameter requests will be returned directly. Developers should maintain a pessimistic attitude and do not trust any client because the computer itself does not know who the operator is, and any parameters should be taken into account.
If (id
< 0){ return("参数不合法");} 2.使用Redis高级数据结构布隆过滤器(Redis如何实现刷抖音不重复-布隆过滤器(Bloom Filter)),他就是检测数据库中是否存在该key,不存在就直接返回即可。 >Bf.mexists user (integer) 0 # if it does not exist, return
Write to the end
In fact, the avalanche, breakdown and penetration of the cache are all client requests directly across the cache to request the database, but they are subtly different in sense. We must understand how these three come into being before we can prescribe the right medicine to the case.
When the user requests, we can configure the circuit breaker + downgrade of the server to ensure that the server will not crash. Frequent user requests can limit the number of requests, or sacrifice the performance of some users.
Generally speaking, when the data reaches a certain amount, in order to ensure the high availability of the server, we will set up a cluster or master-slave + sentinel mode for redis to prevent redis from directly shutting down without any machine replacement. You can also use redis persistence, if Redis crashes, restart and restore data
Of course, we also have to ensure that the database itself has a certain degree of pressure resistance, for example, we add a master and multi-slave database to ensure that the database will not crash so easily.
These are all the contents of the article "what is cache penetration, breakdown and avalanche in Redis". Thank you for reading! Hope to share the content to help you, more related knowledge, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.