当前位置:网站首页>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 !
边栏推荐
- Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
- Fairygui character status Popup
- 阿里云一面:并发场景下的底层细节 - 伪共享问题
- What are the functions and features of helm or terrain
- Error: symbol not found
- FGUI工程打包发布&导入Unity&将UI显示出来的方式
- [algorithm] sword finger offer2 golang interview question 2: binary addition
- [dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
- 地球围绕太阳转
- RTKLIB: demo5 b34f. 1 vs b33
猜你喜欢
[算法] 剑指offer2 golang 面试题1:整数除法
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
FairyGUI人物状态弹窗
Fundamentals of UD decomposition of KF UD decomposition [1]
FairyGUI复选框与进度条的组合使用
Fairygui character status Popup
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
架构师怎样绘制系统架构蓝图?
Chromatic judgement bipartite graph
编辑距离(多源BFS)
随机推荐
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
Excel导入,导出功能实现
阿里云一面:并发场景下的底层细节 - 伪共享问题
IText 7 generate PDF summary
Dark chain lock (lca+ difference on tree)
Meanings and differences of PV, UV, IP, VV, CV
Mysql database index
记录:初次cmd启动MySQL拒接访问之解决
[GNSS data processing] Helmert variance component estimation analysis and code implementation
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
NovAtel 板卡OEM617D配置步骤记录
isEmpty 和 isBlank 的用法区别
【GNSS】抗差估计(稳健估计)原理及程序实现
KF UD分解之伪代码实现进阶篇【2】
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
What are the functions and features of helm or terrain
最短Hamilton路径 (状压DP)
FGUI工程打包发布&导入Unity&将UI显示出来的方式
Fairygui loop list