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

Introduction

A system of record is the authoritative data source when information is scattered among various data providers. When we introduce a caching solution, 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 Aspect-Oriented caching interceptor 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 XAResource we can then enlist the cache and the database in the same global transaction. The database and the cache are therefore updated in a single atomic unit-of-work
  • 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 Java Persistence EntityManager (first-level cache), all entity state transitions 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 database updates can be batched and the number of DML transactions is also reduced.

转载于:https://www.cnblogs.com/davidwang456/p/4444336.html

A beginner’s guide to Cache synchronization strategies--转载相关推荐

  1. Unity增强现实初学者指南视频教程 A Beginner’s Guide to Augmented Reality with Unity

    Unity增强现实初学者指南视频教程 A Beginner's Guide to Augmented Reality with Unity MP4 |视频:h264,1280×720 (部分1920X ...

  2. NumPy Beginner's Guide 2e 带注释源码 四、NumPy 便利的函数

    # 来源:NumPy Beginner's Guide 2e ch4 交易相关偶对 import numpy as np from matplotlib.pyplot import plot from ...

  3. A Beginner's Guide To btrfs

    写得很好,原文被墙,转载过来收藏. 原文:http://www.howtoforge.com/a-beginners-guide-to-btrfs Version 1.0  Author: Falko ...

  4. Magento: Beginner’s Guide(书籍推荐)

    为什么80%的码农都做不了架构师?>>>    BUY Book Step-by-step guide to building your own online store Focus ...

  5. NumPy Beginner's Guide 2e 带注释源码 九、使用 Matplotlib 绘图

    # 来源:NumPy Biginner's Guide 2e ch9 绘制多项式函数 import numpy as np import matplotlib.pyplot as plt# 创建函数 ...

  6. NumPy Beginner's Guide 2e 带注释源码 三、熟悉 NumPy 常用函数

    # 来源:NumPy Biginner's Guide 2e ch3 读写文件 import numpy as np# eye 用于创建单位矩阵 i2 = np.eye(2) print i2 ''' ...

  7. NumPy Beginner's Guide 2e 带注释源码 七、NumPy 特殊例程

    # 来源:NumPy Biginner's Guide 2e ch7 字典排序 import numpy as np import datetime# 日期转成字符串 def datestr2num( ...

  8. NumPy Beginner's Guide 2e 带注释源码 六、深入 NumPy 模块

    # 来源:NumPy Biginner's Guide 2e ch6 矩阵的逆 import numpy as npA = np.mat("0 1 2;1 0 3;4 -3 8") ...

  9. NumPy Beginner's Guide 2e 带注释源码 五、处理 NumPy 矩阵和 ufunc

    # 来源:NumPy Biginner's Guide 2e ch5 创建矩阵 import numpy as np# mat 函数创建矩阵 # 空格分割行,分号分隔列 A = np.mat('1 2 ...

最新文章

  1. Android Monkey使用
  2. sevlet表单处理无法相应问题及web.xml配置.
  3. SpringBoot升级到2.3.x后返回message为空
  4. 安装 | cmd(命令提示符)窗口下使用conda安装TensorFlow
  5. php 代码符号,网站开发编程中的特殊符号处理_php
  6. php怎么创建进程,在php中为长时间运行的进程创建后台进程
  7. os.popen read()报编码错误_数据科学家易犯的十大编码错误,你中招了吗?
  8. virtualbox中windows虚拟机安装增强功能
  9. 用汇编的眼光看C++(之缺省模板、特化模板)
  10. fedora9的安装
  11. python网络爬虫网易云音乐_怎么使用Python网络爬虫爬取网易云音乐歌词
  12. Python: 傅里叶级数
  13. Java毕设项目大学生校园兼职系统计算机(附源码+系统+数据库+LW)
  14. pdfbox或icepdf转换PDF为图片时,中文乱码(出现方框)处理
  15. Java开源模板引擎
  16. linux下的office 速度慢,针对Libreoffice卡顿奇慢的怪病,这里有一味特效药
  17. 企业数字化转型思考系列文章(一)何为数字化转型?
  18. [MySQL] 统计图数据查询 (MySQL查询时间范围内所有日期)
  19. 荣耀x10max能否升级鸿蒙,坐拥豪华配置:荣耀X10 Max成今年唯一5G大屏手机
  20. 读《VR虚拟现实与AR增强现实的技术原理与商业应用》

热门文章

  1. java游戏怎么导入jme3,Java Camera.getProjectionMatrix方法代码示例
  2. 任务与中断共享资源冲突示例
  3. C++中的const成员函数
  4. js 数组 实现 完全树_算法和数据结构 | 树状数组(Binary Indexed Tree)
  5. word回车后间距太大_Word这些神奇的功能,你知道多少?学会这4招,工作不再发愁...
  6. yolov3yolov4yolov5比较
  7. jetson nano 人脸识别
  8. 彩色图如何转换成灰度
  9. oracle中app文件夹下,Oracle Form开发之folder(文件夹)功能开发(一)
  10. ceph编译_Ceph编译:L版本及其之后的版本