当前位置:网站首页>Redis cache update strategy, cache penetration, avalanche, breakdown problems
Redis cache update strategy, cache penetration, avalanche, breakdown problems
2022-07-06 12:12:00 【A pole】
Catalog
Preface
This article is learned by me from dark horse programmer B Stop video tutorial (https://www.bilibili.com/video/BV1cr4y1671t) Notes made in the process of , Mainly records Redis Some ideas about caching , No code involved .
Redis cache
effect
- Reduce back-end load
- Improve reading and writing efficiency , Reduce response time
cost
- Data consistency costs
- Code maintenance costs
- O & M costs
Cache update strategy
Memory obsolescence
Automatically eliminate when the content is insufficient
Default on
You don't have to maintain it yourself
Poor consistency
Low maintenance cost
Time out culling
add to TTL Automatically delete when due
General consistency
Low maintenance cost
Active update
Write business logic , When modifying the database , Update cache
Good consistency
Maintenance costs are high
Select according to business scenario :
Low consistency requirements : Use Redis Built in memory elimination mechanism .
High consistency requirements : Active update , And enable timeout culling ,
Read operations
Cache hits return directly
If not, query the database , And write to the cache
Write operations
Write the database first , Then delete the cache
To ensure the atomicity of database and cache operations ( Monomer applications can use @Transactional Open transaction )
Cache penetration
Cache penetration means that the data requested by the client does not exist in the cache or in the database , This cache will never take effect , These requests will all go to the database .
There are two common solutions :
- Caching empty objects
- advantage : Implement a simple , Convenient maintenance
- shortcoming : Extra memory consumption 、 May cause short-term inconsistencies
- Bloon filtration ( be based on bitmap Realization )
- advantage : Less memory , There is no excess key
- shortcoming : The implementation is complex 、 There is a possibility of misjudgment
( On the left is the schematic diagram of caching empty objects , On the right is the schematic diagram of bulon filtration )
Cache avalanche
Cache avalanche refers to a large number of caches at the same time key Simultaneous failure or Redis The service outage , Cause a large number of requests to reach the database , It brings a lot of pressure .
( On the left is a large number of cache invalidation diagram , The right side is redis Downtime diagram )
Solution :
- To different Key Of TTL add to Random value
- utilize Redis colony Improve service availability
- Add Degraded current limiting Strategy
- Add to business Multi level cache ( Browser cache 、Nginx cache 、Redis cache 、JVM Local cache 、 Databases, etc )
Cache breakdown
Cache breakdown is also called hot spot Key problem , It is a highly concurrent access and complex cache reconstruction business key All of a sudden it failed , Countless requests for access will have a huge impact on the database in an instant .
There are two common solutions :
Logical expiration
Not set up TTL, Instead, add a expire Field , When the field time reaches , Update data .
- advantage
- Threads do not need to wait , Good performance
- shortcoming
- There is no guarantee of consistency
- There is additional memory consumption
- The implementation is complex
- advantage
The mutex
- advantage
- No additional memory consumption
- Guarantee consistency
- Implement a simple
- shortcoming
- Thread needs to wait , Performance is affected
- There may be a deadlock risk
( On the left is the schematic diagram of mutual exclusion , Schematic diagram of logical expiration of the right side wall )
- advantage
Solve the cache breakdown problem based on mutual exclusion
Solve the cache breakdown problem based on logical expiration
Postscript
u1s1, This tutorial is really good , And it's 2022 New version of video , It is highly recommended that you have a look .
边栏推荐
- Arduino JSON data information parsing
- Inline detailed explanation [C language]
- 共用体(union)详解【C语言】
- Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
- Kconfig Kbuild
- JS變量類型以及常用類型轉換
- Vscode basic configuration
- ToggleButton实现一个开关灯的效果
- Several declarations about pointers [C language]
- 小天才电话手表 Z3工作原理
猜你喜欢
open-mmlab labelImg mmdetection
JS變量類型以及常用類型轉換
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
Time slice polling scheduling of RT thread threads
Implementation scheme of distributed transaction
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
Pat 1097 duplication on a linked list (25 points)
Kconfig Kbuild
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
随机推荐
C语言函数之可变参数原理:va_start、va_arg及va_end
Symbolic representation of functions in deep learning papers
【ESP32学习-1】Arduino ESP32开发环境搭建
. elf . map . list . Hex file
There are three iPhone se 2022 models in the Eurasian Economic Commission database
电商数据分析--用户行为分析
JS variable types and common type conversions
STM32 how to locate the code segment that causes hard fault
基于Redis的分布式ID生成器
Common properties of location
arduino获取随机数
Kaggle competition two Sigma connect: rental listing inquiries
Basic knowledge of lithium battery
GCC compilation options
Keyword inline (inline function) usage analysis [C language]
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
ESP8266使用arduino连接阿里云物联网
MySQL占用内存过大解决方案
共用体(union)详解【C语言】
基于Redis的分布式锁 以及 超详细的改进思路