当前位置:网站首页>Cache penetration, cache breakdown, cache avalanche differences and solutions [easy to understand]
Cache penetration, cache breakdown, cache avalanche differences and solutions [easy to understand]
2022-07-24 16:16:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
One 、 Cache processing flow
Front desk request , The background first fetches data from the cache , Take the direct return result , Can't get it from the database , Database access to update cache , And return the result , The database was not accessed , That returns a null result directly .
Two 、 Cache penetration
describe :
Cache penetration refers to data that does not exist in cache or database , And users are constantly requesting , If initiated as id by “-1” Data or id For very large nonexistent data . The user at this time is likely to be the attacker , The attack will cause too much pressure on the database .
Solution :
- Verification is added to the interface layer , Such as user authentication verification ,id Do basic verification ,id<=0 Direct interception of ;
- Data not available from cache , In the database, there is no access to , At this time, you can also key-value Write as key-null, Cache effective time can be set as a short point , Such as 30 second ( Setting too long will cause normal conditions to be unavailable ). This prevents the attacker from repeatedly using the same id Violent attack
3、 ... and 、 Cache breakdown
describe :
Cache breakdown refers to data not in the cache but in the database ( Generally, the cache time expires ), At this time, there are many concurrent users , At the same time, the read cache does not read the data , At the same time go to the database to get data , Causes the database pressure to increase instantaneously , Cause too much pressure
Solution :
- Set hotspot data never to expire .
- Add mutex lock , The mutex reference code is as follows :
explain :
1) Data in cache , Go straight to the above code 13 The result will be returned after line
2) There is no data in the cache , The first 1 Incoming threads , Get the lock and get the data from the database , Before you release the lock , Other threads in parallel will wait 100ms, Then cache the data again . In this way, we can prevent repeated data retrieval from the database , Data is repeatedly updated into the cache .
3) Of course, this is a simplification , In theory, if we can base it on key It's better to lock the value , The thread A Take... From the database key1 Data does not prevent threads from B take key2 The data of , The above code obviously can't do this .
Four 、 Cache avalanche
describe :
Cache avalanche refers to the time from bulk to expiration of data in cache , And the amount of query data is huge , Causes database pressure to be too big even down machine . Unlike cache breakdown , Cache breakdown refers to concurrent query of the same data , The cache avalanche is that different data has expired , A lot of data can't be found to look up the database .
Solution :
- The expiration time of cache data is set randomly , Prevent a large number of data expiration at the same time .
- If the cache database is a distributed deployment , Distribute the hot data evenly in different cache databases .
- Set hotspot data never to expire .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/124664.html Link to the original text :https://javaforall.cn
边栏推荐
- Software recommendation - website construction
- Best practices for preventing XSS in PHP web applications
- deepin任务栏消失解决方法
- Code shoe set - mt2093 · palindrome digit
- Dynamics 365: how to get the threshold value of executemullerequest in batch requests
- Power of leetcode 231.2
- Servlet framework (servlet+jsp) + addition, deletion, modification and query + paging implemented by MySQL (function package student information entry, addition, deletion, modification and query of st
- Kubernetes GPU's Dilemma and failure
- 2.19 haas506 2.0开发教程 - bluetooth - 蓝牙通信(仅支持2.2以上版本)
- Will the capital market be optimistic about TCL's folding screen story?
猜你喜欢
![[loj3247] [USACO 2020.1 platinum](/img/69/f5f7996cd1bb97c84ba381c3df55be.png)
[loj3247] [USACO 2020.1 platinum "non declining subsequences (DP, divide and conquer)

LaneATT

Withdrawal of IPO application, Yunzhou intelligent "tour" of unmanned boat enterprise fails to enter the science and Technology Innovation Board

【SWT】自定义数据表格
![Dynamics crm: [problem solved]cannot open SQL encryption symmetric key because symmetric key password](/img/ae/125fcb16c9d85714c7bbd1255d1d18.png)
Dynamics crm: [problem solved]cannot open SQL encryption symmetric key because symmetric key password

After taking aiyouteng's medicine, Naifei's condition improved

Knowledge points of MySQL (12)

Dynamics crm: mailbox configuration (III) - configure email server profiles and mailboxes

Telephone system rules

Dynamics crm: sharing records for users and teams
随机推荐
Kubernetes version docking object storage
'resultmap'must match' (constructor?, id*, result*, association*, collect problem solving
如何在 PHP 中防止 XSS
Quickly view the version of redis in the server
Adaptive design and responsive design
Rest style
狗牙根植物介绍
Best practices for preventing XSS in PHP web applications
How to choose the appropriate data type for fields in MySQL?
Urban safety series popular science - enter the high incidence period of drowning, if you know the common sense of life-saving
Custom view - Custom button
yolov4 训练自己的数据集
Memcache cache application (lnmp+memcache)
How to generate complex flow chart of XMIND
2.19 haas506 2.0开发教程 - bluetooth - 蓝牙通信(仅支持2.2以上版本)
安信证券开户在手机开户安全吗?
MySQL之知识点(十二)
Yolov6 trains its own data set
R language ggplot2 visualization: ggplot2 visualization basic scatter plot, through in theme_ Specify the parameter base in BW_ Size to change the size of axis labels and control the size of gridlines
[adaptiveavgpool3d] pytorch tutorial