Redis The use of caching , Greatly improve the performance and efficiency of the application , Especially in data query . But at the same time , It also brings some problems . among , The most important question , It's data consistency , Strictly speaking , There is no solution to this problem . If there is a high requirement for data consistency , Then you can't use caching .
Other typical problems are , Cache penetration 、 Cache avalanche and cache breakdown . at present , There are also popular solutions in the industry . This article , It's not to solve these three problems more perfectly , It's not about subverting popular solutions in the industry . It is , From the actual code operation , To demonstrate these three phenomena . The reason for this is , Because , Just look at the academic explanation of these problems , It's hard to have an image in your head , With the actual code demo , We can deepen our understanding of these problems .
Cache penetration , It means to query data that does not exist in a database . The normal process of using cache is roughly , Data query starts with cache query , If key Nonexistence or key It's overdue , Then query the database , And query the object , Put in cache . If the database query object is empty , It's not put into the cache .
Parameter is passed into the primary key of the object ID according to key Get objects from cache If the object is not empty , Go straight back to If the object is empty , Do database query If the object queried from the database is not empty , Then put it into the cache （ Set expiration time ） Imagine this situation , If the parameter passed in is -1, What will happen ？ This -1, Is the object that must not exist . Will go to the database every time , And every query is empty , It doesn't cache every time . If there is a malicious attack , You can take advantage of this loophole , Put pressure on the database , Even crush the database . Even with UUID, It's also easy to find a nonexistent KEY, The attack .
Xiao Bian is at work , Will cache null values , That is to say 【 Code flow 】 pass the civil examinations 5 Step , If the object queried from the database is empty , Also put it in the cache , Only the cache expiration time is set to be shorter , For example, set to 60 second .
Cache avalanche , At a certain time , Expiration in cache set .
One of the reasons for the avalanche , For example, when writing this article , It's about double twelve o'clock , There will soon be a rush , This wave of commodity time is put into the cache , Suppose you cache for an hour . Then at one o'clock in the morning , The cache of this batch of goods has expired . And the access to this batch of products , It's all in the database , For databases , There will be periodic pressure peaks .
When Xiaobian is doing e-commerce projects , Generally, different kinds of goods are adopted , Cache different cycles . Goods in the same category , Add a random factor . This can spread the cache expiration time as much as possible , and , Popular categories have a longer cache time , The storage time of the products in the popular category is shorter , It can also save resources of cache service .
Actually, the concentration is overdue , It's not very deadly , More deadly cache avalanche , It means that a node of the cache server is down or disconnected . Because of the cache avalanche formed naturally , Cache must be created in a certain time period , Then the database can withstand the pressure , This is the time , The database can also withstand the pressure . It's just periodic pressure on the database . The cache service node is down , The pressure on the database server is unpredictable , It's likely to crush the database in an instant .
Cache breakdown , It means a key Very hot , Constantly carrying big concurrency , Large concurrent centralized access to this point , When this key At the moment of failure , Continuous large concurrency breaks through the cache , Direct request database , It's like cutting a hole in a barrier .
When Xiaobian is doing e-commerce projects , Take this and make it “ Hot style ”.
Actually , Most of the time, it's hard to bring crushing pressure to the database server . Few companies have reached this level . therefore , A pragmatist editor , The main products are prepared early , Let the cache never expire . Even if some of the products have fermented themselves into pop money , Also directly set to never expire .
The greatest truths are the simplest ,mutex key Mutex really doesn't work .