Cache寫機制:Write-through與Write-back

Cache寫機制參考http://en.wikipedia.org/wiki/Cache#Writing_Policies上的說明,Cache寫機制分爲write through和write back兩種。

  • Write-through- Write is done synchronously both to the cache and to the backing store.

  • Write-back (or Write-behind) – Writing is done only to the cache. A modified cache block is written back to the store, just before it is replaced.

Write-through(直寫模式)在數據更新時,同時寫入緩存Cache和後端存儲。此模式的優點是操作簡單;缺點是因爲數據修改需要同時寫入存儲,數據寫入速度較慢。

Write-back(回寫模式)在數據更新時只寫入緩存Cache。只在數據被替換出緩存時,被修改的緩存數據纔會被寫到後端存儲。此模式的優點是數據寫入速度快,因爲不需要寫存儲;缺點是一旦更新後的數據未被寫入存儲時出現系統掉電的情況,數據將無法找回。

Write-misses寫缺失的處理方式

對於寫操作,存在寫入緩存缺失數據的情況,這時有兩種處理方式:

  • Write allocate (aka Fetch on write) – Datum at the missed-write location is loaded to cache, followed by a write-hit operation. In this approach, write misses are similar to read-misses.

  • No-write allocate (aka Write-no-allocate, Write around) – Datum at the missed-write location is not loaded to cache, and is written directly to the backing store. In this approach, actually only system reads are being cached.

Write allocate方式將寫入位置讀入緩存,然後採用write-hit(緩存命中寫入)操作。寫缺失操作與讀缺失操作類似。

No-write allocate方式並不將寫入位置讀入緩存,而是直接將數據寫入存儲。這種方式下,只有讀操作會被緩存。

無論是Write-through還是Write-back都可以使用寫缺失的兩種方式之一。只是通常Write-back採用Write allocate方式,而Write-through採用No-write allocate方式;因爲多次寫入同一緩存時,Write allocate配合Write-back可以提升性能;而對於Write-through則沒有幫助。

處理流程圖

Write-through模式處理流程:


A Write-Through cache with No-Write Allocation

A Write-Through cache with No-Write Allocation

Write-back模式處理流程:



A Write-Back cache with Write Allocation

A Write-Back cache with Write Allocation


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章