当前位置:网站首页>Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
2022-07-06 13:02:00 【Java misty rain】
Write an article to slow down , Share the old stereotype : Cache breakdown
、 Cache penetration
、 Cache avalanche
.
Before we understand these three problems , We need to understand , Commonly used distributed cache Redis The amount of single machine concurrency can reach 10000 , Common relational databases MySQL The general concurrency is 1000 , The amount of concurrency they support may be ten times worse , Therefore, the traffic should be intercepted in the cache layer as much as possible .
Why? ? It's like draining more water in a big lake , It may wash out the small river , I don't know if you've heard of —— The water of the Yangtze River comes up from the sky , Baiyangdian can't hold .
Contents of this article :
Cache breakdown
What is cache breakdown
How to solve
Cache penetration
What is cache penetration
How to solve cache penetration
Cache avalanche
What is a cache avalanche
How to solve the cache avalanche
Text :
What is cache breakdown
Start with cache breakdown .
Cache breakdown : A large number of concurrent visits key Expire at a certain time , Cause all requests to be typed directly in DB On .
Cache breakdown
Cache breakdown will increase the load on the database , Let's see how to alleviate .
How to solve
Lock update
The query cache , It is found that there is no... In the cache , Lock , Let other threads wait , Let only one thread update the cache .
Lock update
Asynchronous update
Another possible solution is to set the cache to never expire . How to update the cache ? Update the cache asynchronously .
For example, a daemon thread is set in the background to update the cache regularly , But this timing is difficult to grasp .
Asynchronous update mechanism is actually more suitable for cache preheating .
Cache penetration
What is cache penetration
Cache penetration : Cache penetration refers to data that does not exist in both the query cache and the database , In this way, each request is directly called to the database , It's like the cache doesn't exist .
Cache penetration
Cache penetration will cause non-existent data to be queried in the storage layer every time it is requested , Lost the significance of cache protection back-end storage .
Cache penetration may increase the back-end storage load , If a large number of storage layer null hits are found , Maybe there is a cache penetration problem .
Cache penetration can occur for two reasons :
Own business code problem
A malicious attack , Reptiles cause empty hits
Let's see how to solve .
How to solve cache penetration
Cache null / The default value is
One way is after a database miss , Save an empty object or default value to the cache , Then access the data , It gets it from the cache , This protects the database .
Cache null
There are two major problems with caching null values :
Null values are cached , It means that there are more keys in the cache layer , Need more memory space ( If it's an attack , The problem is more serious ), More effective
The method is to set a shorter expiration time for this kind of data , Let it automatically remove .
The data of cache layer and storage layer will be inconsistent for a period of time , It may have some impact on the business .
For example, the expiration time is set to 5 minute , If the storage layer adds this data at this time , Then there will be inconsistencies between the cache layer and the storage layer data in this period of time .
At this time, you can use message queue or other asynchronous methods to clean up the empty objects in the cache .
The bloon filter
In addition to caching empty objects , We can also store and cache before , Add a bloom filter , Do a layer of filtering .
The bloom filter will store whether the data exists , If you judge that the data is not, you can no longer , No access to storage .
The bloom filter filters
What's the bloom filter ? Find out if it will be slow ?
What is a bloon filter ?
I don't know how much you know about hash tables , The bloom filter is a similar thing .
It is a continuous data structure , Each storage bit is a bit
, namely 0
perhaps 1
, To identify whether the data exists .
When storing data , Use K A different hash function maps this variable to bit Of the list K A little bit , Set them as 1.
Bulon filter structure
We judge the cache key Whether there is , Again ,K Hash functions , Mapping to bit On the list K A little bit , Judgment is not 1:
If it's not 1, that key non-existent ;
If it's all 1, It just means key Possible .
As for why ? Because hash functions may collide .
There are two main solutions for cache penetration , Let's make a simple comparison :
Cache empty object core bloom filter scheme comparison
Cache avalanche
Next, let's look at the most serious situation , Cache avalanche .
What is a cache avalanche
Cache avalanche : When ⼀ All the time ⽣⼤ Large scale cache invalidation , For example, the cache service is down 、 A lot of key Expire at the same time , The result is ⼤ The quantity request comes in and calls directly to DB On , May cause the whole system to crash , It's called an avalanche .
.... Bloggers are too lazy, too many words , I don't want to write. .... The article has been finished PDF, Friends in need can get it for free by private letter !
边栏推荐
- [rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
- FairyGUI简单背包的制作
- Special palindromes of daily practice of Blue Bridge Cup
- [算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
- [algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
- Agile development helps me
- Matlab读取GNSS 观测值o文件代码示例
- Record: Navicat premium can't connect to MySQL for the first time
- Mixed use of fairygui button dynamics
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
猜你喜欢
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
Fairygui joystick
The port is occupied because the service is not shut down normally
Unity3d, Alibaba cloud server, platform configuration
The earth revolves around the sun
地球围绕太阳转
Combination of fairygui check box and progress bar
121道分布式面试题和答案
MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
记录:初次cmd启动MySQL拒接访问之解决
随机推荐
121道分布式面试题和答案
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
【无标题】
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
Excel导入,导出功能实现
FairyGUI复选框与进度条的组合使用
《软件测试》习题答案:第一章
Shortest Hamilton path (pressure DP)
[GNSS data processing] Helmert variance component estimation analysis and code implementation
异常:IOException:Stream Closed
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
记录:下一不小心写了个递归
Problems and solutions of robust estimation in rtklib single point location spp
GNSS positioning accuracy index calculation
Music playback (toggle & playerprefs)
Itext 7 生成PDF总结
染色法判定二分图
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
Fairygui character status Popup
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers