当前位置:网站首页>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 !
边栏推荐
- Fairygui joystick
- [算法] 剑指offer2 golang 面试题1:整数除法
- FairyGUI增益BUFF数值改变的显示
- 【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
- [algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
- Prove the time complexity of heap sorting
- 音乐播放(Toggle && PlayerPrefs)
- 雇佣收银员【差分约束】
- FairyGUI增益BUFF數值改變的顯示
- [algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
猜你喜欢
Fairygui character status Popup
第一人称视角的角色移动
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
The earth revolves around the sun
Mixed use of fairygui button dynamics
[算法] 剑指offer2 golang 面试题10:和为k的子数组
Fairygui loop list
RTKLIB: demo5 b34f. 1 vs b33
[GNSS data processing] Helmert variance component estimation analysis and code implementation
Liste des boucles de l'interface graphique de défaillance
随机推荐
Liste des boucles de l'interface graphique de défaillance
2022 National Games RE1 baby_ tree
Detailed explanation of balanced binary tree is easy to understand
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
[GNSS data processing] Helmert variance component estimation analysis and code implementation
Fairygui character status Popup
染色法判定二分图
一文搞定 UDP 和 TCP 高频面试题!
Matlab读取GNSS 观测值o文件代码示例
错误:排序与角标越界
Rt-ppp test using rtknavi
【GNSS】抗差估计(稳健估计)原理及程序实现
Problems and solutions of robust estimation in rtklib single point location spp
Pride-pppar source code analysis
面试必备:聊聊分布式锁的多种实现!
IText 7 generate PDF summary
(core focus of software engineering review) Chapter V detailed design exercises
记录:下一不小心写了个递归
[算法] 剑指offer2 golang 面试题10:和为k的子数组
121 distributed interview questions and answers