当前位置:网站首页>Analysis of cache read and write strategy
Analysis of cache read and write strategy
2022-07-23 07:11:00 【Software development heart】
Preface
Lift cache , We first think of cache as a component that stores data , Its function is to make the request for data return faster .
We usually cache data that doesn't change often and needs fast access , The common practice is to write these data into memory , When you need access, you can quickly return the required results .
actually , Generally, there is a large difference between the two kinds of hardware , A structure used to coordinate the differences in data transmission between the two , Can be called cache .
In the daily development process , We often put the data in the local cache (escache) Or external cache (Redis) in .
Cache read / write strategy
Caching sounds simple , It's nothing more than reading cache first , If the cache does not hit, query from the database , Write it back to the cache after querying . But actually , According to different business scenarios , Cache read and write strategies are also different . Let's take the most common cache + Database scenario .
Cache Aside( Bypass cache ) Strategy
Cache Aside It is the strategy we use most , The cache does not interact directly with the database , It's up to the application to deal with the database at the same time .Cache Aside That's exactly what this model is all about ,Cache On the side of the app (aside).
When reading data
- The program needs to determine whether data already exists in the cache
- When data already exists in the cache ( That's cache hits ,cache hit), Data is returned directly from the cache
- When there is no data in the cache ( That is, cache miss ,cache miss), Read the data from the database first , Cache and store , Then return the data
When writing data
- Update the database first
- Then delete the corresponding data in the cache
Read-Write Through ( Read and write through ) Strategy
Read/Write Through Pattern The server in regards the cache as the main data storage , Read data from it and write it into it . The cache service is responsible for reading and writing this data DB, This reduces the responsibility of the application .
When reading data (Read Through)
- Read data from cache , Read to return directly .
- If you can't read it , Then the cache component loads from the database first , Write to the cache and return a response .

When writing data (Write Through)
- Check the cache first , There is no... In the cache , Update database directly .
- A cache hit , Update the cache first , Then the cache service updates the database itself ( Update cache and database synchronously )

stay Cache Aside Next , When a read request occurs , If cache There is no corresponding data in , The client is responsible for writing data to cache, and Read-Through The policy is written to the cache by the cache service itself , This is transparent to the client .
Read/Write Through The characteristic of the strategy is that the cache node, not the user, deals with the database , In the course of our development , This strategy is less used , The reason is that we usually use cache components (Redis perhaps Memcached) It does not provide the function of writing to the database . Only local cache Guava Cache Medium Loading Cache Yes Read Through The shadow of strategy .
It can be seen that , because Write Through When writing data, you need to update the database at the same time , It will have a great impact on performance . Can we update the database asynchronously ? That's what's next Write Back Strategy .
Write-Back( Asynchronous cache write ) Strategy
Write-Back Strategy and Read-Write Through The common point of the strategy is that the cache service is responsible for the cache and database read and write .
The difference is Write-Back Use caching as a reliable data source , Only write to the cache every time , And writing to the database is done asynchronously , For example, when the data is to be removed from the cache and then stored in the database, or after a period of time to batch update the database .
advantage : Is that the reading and writing speed is very fast , Because they read and write directly from the cache . There is a certain tolerance for the unavailability of the database in the service , When the database is unavailable , It can also process the results normally , Update the data again after the database is restored . At the same time, it also reduces the pressure on the database , Writing to the database can be done when the business flow is low .
shortcoming : There is a risk of data loss , If the cache service hangs and the data is not written to the database in time , Then the data will be lost .
Suitable for the scene : Scenarios in which data often changes and data consistency is not so high , For example, browsing volume 、 Like
summary
These three cache read and write strategies have their own advantages and disadvantages , We need to choose a more suitable one according to the specific business scenarios . If you read more and write less , And high requirements for data consistency , have access to Cache Aside Strategy . If you write more and read less , And the requirements for data consistency are not high , Sure Read-Write Through perhaps Write-Back Strategy .
Ryan.ou
边栏推荐
- 百度钱包帮你还信用卡 跨行还款0手续费 实时到帐 新人奖励5元
- Flink数据源拆解分析(WikipediaEditsSource)
- threejs+shader绘制常用的图形
- 电脑一拖二显示器分辨率怎么调? 两个显示器设置不同分辨率的技巧
- 小黑leetcode之旅:590. N 叉树的后序遍历
- gpu和cpu有什么区别不同 电脑gpu含义介绍
- What if the file copied by SSD is only dozens of KB? Solution to slow copying speed after installing hard disk in computer
- Microsoft, how can't we upload photos? What's the matter? The solution of twinsornot photos not being transmitted
- 局域网SDN技术硬核内幕 - 15 三 从物到人 园区用户漫游的MPLS实现
- [FAQ] common reasons and solutions for the failure of in app payment services to pull up the payment page
猜你喜欢

Demo19-(待更新)

对线程池的了解与应用你掌握多少

小黑leetcode之旅:590. N 叉树的后序遍历

CV target detection model sketch (1)

Pikachu靶场-SQL注入-搜索型注入过关步骤

threejs+shader绘制常用的图形

AIRIOT答疑第5期|如何使用低代码业务流引擎?

LUR缓存算法

The ability to detect movement in vivo and build a safe and reliable payment level "face brushing" experience

Design of boiler drum temperature control system (process control course design matlab/simulink)
随机推荐
DataGrip使用教程(GIF版)
PHP 防止或检测页面被刷新 post重复提交问题
telnet不是内部或外部命令怎么解决 telnet不是内部或外部命令解决办法
Face algorithms
Scientific computing and Simulation - a brief introduction and application of the nonlinear least square problem of Gauss Newton method
session、cookie、token 详解
Combing the docking process between the integration base and the business system
对线程池的了解与应用你掌握多少
苹果开发者账号怎么免费注册申请以便第一时间享受升级体验
电脑显示器分辨率多少合适?各尺寸显示器最佳分辨率及不同壁纸的选择介绍
Kotlin学习快速入门(8)—— 委托
Pikachu靶场-SQL注入-搜索型注入过关步骤
关注公众号免费领取小米移动电源是真的吗?微信朋友圈送小米移动电源
How to delete non system files on disk C. summary of files that can be deleted when disk C is popular
小米金融今日(5月11日)正式上线 白送10000元体验金 附官方地址
dns是什么意思 dns作用是什么介绍
Kali-工具-sqlmap常见用法
Kotlin learning quick start (8) - Delegation
vim文本编辑器
图像处理解决方案 veImageX 技术演进之路