Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 37 ยท Strategies and Invalidation

Read-Through, Write-Through and Write-Behind

Compare caching strategies beyond cache-aside: read-through, write-through, write-around and write-behind, with Python simulations and advice on which fit Memcached.

In cache-aside the application manages both the cache and the database. Other strategies move that responsibility into a cache layer that sits in front of the database and behaves as one unit. Names to know: read-through, write-through, write-around and write-behind (also called write-back). Each makes a different trade-off between speed, safety and complexity.

An important fact up front: Memcached itself implements none of these. It is a passive store that never calls your database. You build these strategies in a library or wrapper around your client. Seeing them side by side clarifies why cache-aside is the usual choice for Memcached.

Read-through

With read-through the application only talks to the cache layer. On a miss, the layer (not the caller) loads from the database, stores the value and returns it. The calling code is simpler because it contains no miss logic.

You get this by wrapping Memcached in a small class with a loader function:

Python A read-through wrapper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Functionally this is cache-aside moved behind a function. That is how most Memcached users get "read-through": a helper such as remember(key, ttl, callback), found in many frameworks.

Write-through

With write-through every write goes to the cache and the database together, synchronously, before the caller is told it succeeded. The cache always holds the latest value, so reads right after a write are hits.

  • Pro: reads after writes are fast and fresh; the cache is rarely stale.
  • Con: each write pays for two stores, so writes are slower, and you may cache data that is never read.
  • Failure mode: if the database write succeeds and the cache write fails (or the reverse), the two disagree. You need a plan: usually delete the key on failure.
Python Write-through with a failure

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Write-around

Write-around writes only to the database and lets the cache fill later on a read miss. It avoids filling the cache with data that is written once and rarely read. Cache-aside's delete-on-write is a variant: the key is invalidated rather than updated.

Write-behind (write-back)

With write-behind the application writes to the cache and the cache flushes to the database later, asynchronously, often in batches. Writes are very fast, and many updates to the same key can be merged into one database write. The price is durability: if the cache loses the item before the flush (a crash, an eviction), the data is gone for good.

Python Write-behind coalesces writes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows why write-behind is dangerous with Memcached: Memcached is volatile and can evict any item at any time, so an unflushed write may disappear. Do not use Memcached as a write-behind buffer for data you cannot lose. Use a durable queue or a database for that.

Comparison

Strategy Read latency Write latency Stale risk Data-loss risk Fits Memcached?
Cache-aside Miss pays load Normal (db + delete) Brief, TTL-bounded None Yes, the default
Read-through Same as cache-aside Normal Same None Yes, as a wrapper
Write-through Always fresh Slower (two writes) Very low None if db is first Yes, with care
Write-around Miss after write Normal Low None Yes
Write-behind Fresh Fastest Low High No (cache can lose data)

Choosing

  • Default: cache-aside with delete-on-write and a TTL.
  • Hot data read right after being written (profile pages): consider write-through with a fallback delete.
  • High write rates to counters or metrics you can afford to lose: a counter in Memcached with periodic persistence is acceptable; otherwise use a queue.
  • When you need guaranteed durability: write to the database first and always treat the cache as optional.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Does Memcached itself implement read-through?

    Memcached never contacts your database. Read-through is implemented in a client-side wrapper.
  2. 2. What is the main risk of write-behind?

    Writes live only in volatile memory until flushed, and Memcached may evict or lose them.
  3. 3. In write-through, which store should be written first for safety?

    If the cache write then fails you only have a missing or stale cache entry, which you can delete. The reverse could lose data.
  4. 4. What does write-around do?

    It keeps rarely read data out of the cache until someone asks for it.

Summary

  • Read-through hides miss handling inside a cache layer; with Memcached you build it as a wrapper.
  • Write-through updates the cache and the database together: fresh reads, slower writes.
  • Write-around writes the database only and lets reads fill the cache.
  • Write-behind is fast but can lose data, so it is a poor fit for a volatile cache like Memcached.
  • Cache-aside with delete-on-write and a TTL remains the sensible default.

Next: Invalidation Strategies