当前位置:网站首页>Redis cache avalanche, cache penetration, cache breakdown
Redis cache avalanche, cache penetration, cache breakdown
2022-07-28 21:12:00 【m0_ sixty-seven million four hundred and one thousand six hundr】
Redis Cache avalanche 、 Cache penetration 、 Cache breakdown
Redis Caching process
Redis The database is a nosql database , The stored data format is key-value.Redis The database runs on Memory in , So it's faster than his query MySql Much faster . So we will put some data that users often query on Redis in , When Redis Sometimes it goes straight back , When Redis If not, go to the database to find . So as to increase the operation efficiency of the service .
Cache avalanche
Redis The cached data in has expiration time , When in When a large number of caches fail at the same time, it will cause cache avalanche .
for instance , stay 11 Everyone went hungry at o'clock and ordered some takeout , Are you hungry at this time Redis There are a large number of business information stored in , And the expiration time set by hungry programmers for this cache is 6 Hours . So in the afternoon 5 It's dinner time, and a large group of people are hungry. What's the takeout , This is the time Redis Your cache just expired , In a short time, a large number of query requests fall into the fragile MySql On , Lead to MySql Direct explosion !
Solution
To solve Redis Cache avalanche requires avoid Redis A large number of caches expire in a short time
Never expire
Set up Redis Medium key Never expire , But this will take up a lot of server memory .
Reasonably set the expiration time
Set the expiration time reasonably according to business needs . But there are some unexpected situations .
Use Redis Distributed locks for
Since a large number of requests fall to MySql Can lead to MySql The explosion ! Then add a limit , Let only Same request Fall on MySql On , Anyway, they all query the same information , After that, you can go to Redis I found .
Cache penetration
Redis Cache penetration refers to , stay Redis No relevant data can be found in the cache or database . In other words, this is an illegal query , The client sent out a large number of illegal queries such as id It's negative , So every time this query needs to go to Redis And query in the database . Lead to MySql Direct explosion !
Solution
Filter illegal queries
Filter illegal queries in the background service , Just don't let him fall into Redis Service . such as id<=0 perhaps Paging content is too large etc.
Caching empty objects
If his query data is legal , But it is Redis and MySql None of them , So here we are Redis Store an empty object in , In this way, the next time the client continues to query, it can be in Redis In the back . however , If the client keeps sending this malicious query , It will lead to Redis There are many such empty objects in , Waste a lot of space
The bloon filter
The bloom filter consists of a binary array and k A hash array .
New addition of Bloom filter
When we want to add an element ( For example, new python), The bloom filter will use hash Function calculates several index values , Then change the corresponding position in the binary array to 1.
Query of Bloom filter
When we want to query an element ( For example, query python), The bloom filter will use hash Function calculates several index values , Then query whether the corresponding position in the binary array All for 1. If it's all for 1 It means that the modified element exists . But bloom filter has the possibility of misjudgment , Because different elements hash The value after may be the same , So let's say we query java,java after hash The calculated index value and python As like as two peas. , Then you think java Also in the bloom filter .
Deletion of Bloom filter
ditto , To delete the bloom filter is to delete hash The corresponding position of the rear array is changed to 0. But there is the possibility of accidental deletion . Delete... According to the above example python Will put java Delete it .
Bloom filter solves cache penetration
Let's first put MySql The data in is stored in the bloom filter ( Due to the use of binary arrays , That is, bitmap, so little space is used ), Then if Redis No hits in cache , You need to check MySql Before the database, query whether it is in the bloom filter MySql There's data .
Features of bloon filter
- There is a possibility of miscalculation
- If the data exists , Then you must return to true
- The time complexity of the query is O(k),k by hash Number of functions
- k The bigger it is , The larger the array length , The lower the likelihood of miscalculation
- Use bitmaps ( Binary array ) So the memory pressure is small
Cache breakdown
Cache breakdown is similar to cache avalanche , It is also because Redis in key Due to expiration . It's just that cache breakdown is a hot spot key Due to expiration . When a hotspot data suddenly expires , It will lead to a sudden large number of situations falling directly to MySql On , Lead to MySql Direct explosion !
Solution
There are two main ideas ,
- Let that hot key Don't expire suddenly
- Don't let a lot of requests fall on MySql On .
Set hot spots Key Never expire
Simple and crude , I don't even expire , You can't bypass me to visit MySql. But it may be right Redis Memory causes great pressure , Therefore, it is usually set for a long time .
Use Redis Distributed locks for
Since a large number of requests fall to MySql Can lead to MySql The explosion ! Then add a limit , Let only one same request fall at a time MySql On , Anyway, they all query the same information , After that, you can go to Redis I found .
边栏推荐
- EfficientFormer:轻量化ViT Backbone
- mfc wpf winform(工业用mfc还是qt)
- mysql梳理复习内容--附思维导图
- Lazada店铺如何产号高效补单?(测评自养号技术详解篇)
- Unity foundation 4 common plug-ins
- Interesting pictures and words
- PostgreSQL数据库删库前是不是需要把所有连接断开才能删除?
- source insight 使用快捷键
- MFC WPF WinForm (Industrial MFC or QT)
- Reading and writing basic data types in protobuf
猜你喜欢

protobuf 中基础数据类型的读写

Cobal Strike的学习与使用

SharkTeam完成Flow生态NFT市场MatrixMarket的安全审计

C language function program example (super complete)

BUUCTF做题Upload-Labs记录pass-01~pass-10

作业 ce

A 58 year old native of Anhui Province, he has become the largest IPO investor in Switzerland this year

【云原生】什么是 CI/CD ? | 摆平交付障碍的 CI/CD

《软件设计师考试》易混淆知识点

Unity foundation 1 - event execution sequence, custom events
随机推荐
Zcmu--5066: dark corridor
Explain various coordinate systems in unity in detail
怎样搭建企业内部维基百科
C # basic 7-iterator and coroutine
C # basic 4-written examination question 1
MFC WPF WinForm (Industrial MFC or QT)
Understanding of C # delegate
广和通&高通物联网技术开放日成功举办
Nacos 原理
【TiDB】txt文档导入数据库,这样做真的很高效
C#连接MySql数据库详细步骤
3D laser slam: Interpretation of logo-loam paper - Introduction
Unity foundation 2 editor expansion
A 58 year old native of Anhui Province, he has become the largest IPO investor in Switzerland this year
《软件设计师考试》易混淆知识点
Cause analysis of restart of EMC cx4-120 SPB controller
融合数据库生态:利用 EventBridge 构建 CDC 应用
Color finder actual combat (QT including source code)
数据库--explain的使用
作业 ce