博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A beginner’s guide to Cache synchronization strategies--转载
阅读量:7062 次
发布时间:2019-06-28

本文共 2847 字,大约阅读时间需要 9 分钟。

原文地址:http://vladmihalcea.com/2015/04/20/a-beginners-guide-to-cache-synchronization-strategies/

Introduction

A  is the authoritative data source when information is scattered among various data providers. When we introduce a , we automatically duplicate our data. To avoid inconsistent reads and data integrity issues, it’s very important to synchronize the database and the cache (whenever a change occurs into the system).

There are various ways to keep the cache and the underlying database in sync and this article will present some of the most common cache synchronization strategies.

Cache-aside

The application code can manually manage both the database and the cache information. The application logic inspects the cache before hitting the database and it updates the cache after any database modification.

Mixing caching management and application is not very appealing, especially if we have to repeat these steps in every data retrieval method. Leveraging an  can mitigate the cache leaking into the application code, but it doesn’t exonerate us from making sure that both the database and the cache are properly synchronized.

Read-through

Instead of managing both the database and the cache, we can simply delegate the database synchronization to the cache provider. All data interactions is therefore done through the cache abstraction layer.

Upon fetching a cache entry, the Cache verifies the cached element availability and loads the underlying resource on our behalf. The application uses the cache as thesystem of record and the cache is able to auto-populate on demand.

Write-through

Analogous to the read-through data fetching strategy, the cache can update the underlying database every time a cache entry is changed.

Although the database and the cache are updated synchronously, we have the liberty of choosing the transaction boundaries according to our current business requirements.

  • If strong consistency is mandatory and the cache provider offers an  we can then enlist the cache and the database in the same global transaction. The database and the cache are therefore updated in a 
  • If consistency can be weaken, we can update the cache and the database sequentially, without using a global transaction. Usually the cache is changed first and if the database update fails, the cache can use a compensating action to roll-back the current transaction changes

Write-behind

If strong consistency is not mandated, we can simply enqueue the cache changes and periodically flush them to the database.

This strategy is employed by the  (first-level cache), all  being flushed towards the end of the current running transaction (or when a query is issued).

Although it breaks transaction guarantees, the write-behind caching strategy can outperform the write-through policy, because  and the number of DML transactions is also reduced.

转载地址:http://zbnll.baihongyu.com/

你可能感兴趣的文章
React Native安卓模拟器调出Dev Setting菜单
查看>>
Swift枚举相关值
查看>>
少走弯路,给Java 1~5 年程序员的建议
查看>>
08.Android之View事件问题
查看>>
[ JavaScript ] 数据结构与算法 —— 链表
查看>>
Java程序员幽默爆笑锦集
查看>>
小程序button引导用户授权
查看>>
机器人定位导航技术 激光SLAM与视觉SLAM谁更胜一筹?
查看>>
我是如何设计 Upload 上传组件的
查看>>
彻底搞懂浏览器Event-loop
查看>>
java2019面试题北京
查看>>
Promise面试题2实现异步串行执行
查看>>
ECS应用管理最佳实践
查看>>
JavaSE小实践1:Java爬取斗图网站的所有表情包
查看>>
微信小程序上拉加载:onReachBottom详解+设置触发距离
查看>>
python docx文档转html页面
查看>>
【跃迁之路】【463天】刻意练习系列222(2018.05.14)
查看>>
windows-nginx-https-本地配置
查看>>
JDK9: 集成 Jshell 和 Maven 项目.
查看>>
【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
查看>>