西维蜀黍

【Cache System】缓存更新策略 - Write Behind

Write Back / Write Behind / Lazy Write Pattern

一些了解Linux操作系统内核的同学对write back应该非常熟悉,这不就是Linux文件系统的Page Cache的算法吗?是的,你看基础这玩意全都是相通的。

Initially, writing is done only to the cache. The write to the backing store is postponed until the modified content is about to be replaced by another cache block.

Write Back 的套路,一句说就是,在更新数据的时候,只更新缓存,不更新数据库,而我们的缓存会异步地批量更新数据库。这个设计的好处就是让数据的I/O操作飞快无比(因为直接操作内存嘛),因为异步,write backg还可以合并对同一个数据的多次操作,所以性能的提高是相当可观的。

  ...


【Cache System】缓存更新策略 - Write Through

Write Through Pattern

The write-through strategy adds data or updates data in the cache whenever data is written to the database.

Write Through 套路和Read Through相仿,不过是在更新数据时发生。

这里,可以由 composite 存储更新数据库和缓存(注意是先更新DB,再更新cache),也可以由数据库直接更新缓存。

  ...


【Cache System】缓存更新策略 - Read Through

Read Through Pattern

在上面的Cache Aside套路中,我们的应用代码需要维护两个数据存储,一个是缓存(Cache),一个是数据库(Repository)。

Read Through 套路就是在查询操作中更新缓存,也就是说,当缓存失效的时候(过期或LRU换出),Cache Aside是由调用方负责把数据加载入缓存,而Read Through则用缓存服务(composite 存储)自己来加载,从而对应用方是透明的。

  ...


【Cache System】缓存更新策略 - Cache Aside

Cache Aside Pattern / Lazy-loading

This is the most commonly used cache update strategy in applications.

其具体逻辑如下:

  • 失效:应用程序先从cache取数据,没有得到,则从数据库中取数据,成功后,放到缓存中。

  • 命中:应用程序从cache中取数据,取到后返回。

  • 更新:先把数据存到数据库中,成功后,再让缓存失效。

  ...


【Cache System】缓存一致性(Cache Coherence)

Background

In a shared memory multiprocessor system with a separate cache memory for each processor, it is possible to have many copies of shared data: one copy in the main memory and one in the local cache of each processor that requested it.

When one of the copies of data is changed, the other copies must reflect that change. Cache coherence is the discipline which ensures that the changes in the values of shared operands (data) are propagated throughout the system in a timely fashion.

  ...