cloudpatterns.appClassroomKatasWhiteboardBlog
Z3 · Caching & Delivery

Read-through & write-through

Read-through & write-through

Bronze

In read-through, the cache itself loads a missing item from the database. In write-through, each write goes to the database and the cache together and is confirmed only once both have it — so a read straight after a write gets the new value, at the price of slower writes and memory spent on data nobody may read. Write-around writes the database alone and leaves the cache to load the item on its next miss.

Use when
Data read soon after it is written — a user's own profile or caption straight after an edit · When a miss or an old value right after a save would be visible to the user
Avoid when
Data written often and rarely read — it fills the cache with churn · As the only way to fill the cache — new nodes start empty
Trade-offs
Fresh reads straight after a write, but every write waits for two stores · Memory spent on items that may never be read — a TTL limits the waste · More coordination logic in the write path
Requires
Cache-aside
3 objectives · 9 misconceptions
  • predict Predict a write's latency and what a read straight after it returns under write-through versus write-around (cache-aside's invalidate on write).
  • choose Choose write-through for data that is read soon after it changes and write-around for data written often or rarely re-read — keeping lazy loading underneath for everything else.
  • explain Explain why write-through still leaves gaps — a new or replaced cache node starts without the data, changes made straight in the database never reach the cache, and a failure between the two writes leaves them disagreeing.
  • Not true: With write-through, the cache can never hold stale data.
  • Not true: Write-through makes writes faster, because they land in the fast cache first.
  • Not true: Once writes fill the cache, loading on read misses is no longer needed.
  • Not true: Caching everything that is written costs nothing extra.
  • Not true: A read-through cache never misses, because it loads data by itself.
  • Not true: Write-through and write-behind are two names for the same thing.
  • Not true: Users mind a slow save as much as a slow page load.
  • Not true: A write-around write leaves the new value in the cache.
  • Not true: Write-through writes the database, then the cache re-reads the item from it.

Neighborhood

builds onCache-aside
Read-through & write-through
On linesCache Line

Progress is saved on this device