Course map

Topic 084 / Core

Write-through and write-back

Chapter 6.1 · Caching Strategies

What and why

Cache-aside (→ 83) only reads through the cache; writes still go straight to the database and the cache entry is merely invalidated. Some workloads need the cache itself to own the write: read-your-own-write freshness without an extra database hit, or write latency too tight for the database to sit on the critical path of every write. Write-through writes to the cache and the database synchronously, in lockstep. Write-back (write-behind) writes to the cache immediately and flushes to the database asynchronously, later. Builds on: → 83 (cache-aside and read-through).

Diagram

Write-through                          Write-back (write-behind)
──────────────                         ──────────────────────────
app ── write(key,val) ──► cache        app ── write(key,val) ──► cache ──► ack (fast)
cache ── write ──► database                                       │
database ack ──► cache ──► ack to app                     (later, async)
(app waits for both writes)                                 cache ── flush ──► database

How it works

  1. Write-through: the app writes to the cache; the cache synchronously writes the database before acknowledging — cache and database agree at every point the app can observe, with no staleness window on the key just written.
  2. Write-through latency = cache write + database write, done serially — never faster than a direct database write, and sometimes slower.
  3. Write-back: the app writes to the cache, which acknowledges immediately; the write to the database is deferred — buffered, batched, and flushed on a timer or when a buffer fills.
  4. Write-back latency ≈ the cache write alone, but the database lags the cache by however long the flush interval is, and the cache's in-memory copy is the only durable copy of the write until it flushes.
  5. If the cache crashes before flushing, whatever is still only in the write-back buffer is lost — write-back trades durability for latency, a trade that must be made explicitly (→ 69, ACID, durability), never by default.
  6. Both patterns govern only the write path; they are normally paired with cache-aside or read-through (→ 83) on the read path.

Practical example

(numbers illustrative). Cache write ≈ 1 ms, database write ≈ 15 ms. Write-through latency = 1 + 15 = 16 ms — 1 ms slower than a direct database write alone (15 ms), the price of keeping the cache synchronized. Write-back latency = 1 ms (the cache ack); the database catches up on a 2 s flush window. At a write rate of 500 writes/second, the writes exposed to loss at any instant if the cache crashed = 500 × 2 s = 1,000 writes — the size of the window write-back leaves open.

When to use it

  • Use write-through when: read-after-write correctness from the cache matters and write volume is low enough that the added per-write latency is acceptable.
  • Use write-back when: writes are frequent and latency-sensitive, and some bounded data loss is tolerable or will be reconciled (counters, view counts, non-critical metrics).
  • Avoid write-through when: writes are very frequent — the serial database write caps throughput. Avoid write-back when: durability is non-negotiable (money, inventory) unless paired with a durable write-ahead buffer.
  • Failure scenarios: write-back data loss on a crash before flush; mistaking write-through for "making the database faster" (it adds a hop, never removes one); an unbounded flush backlog if the database falls behind the write buffer (needs backpressure, → 97).
  • Simpler alternative: cache-aside's invalidate-on-write (→ 83) — accept a brief staleness window instead of making the cache responsible for durability.

Practice

(pattern kind — implement minimal, spot the misuse; 20 min, lab: Postgres + Redis).

  1. Implement write-through: a write updates Redis then Postgres synchronously; kill the network to Postgres mid-write and confirm the app blocks or fails rather than leaving the cache ahead of an unattempted database write.
  2. Implement write-back: writes land in a Redis list immediately (ack), and a background worker flushes every 2 s to Postgres; measure write latency for 100 writes under each pattern and confirm write-through is measurably slower.
  3. Spot the misuse: a write-back buffer with no flush retry — kill the worker mid-run, show the buffered writes are stuck, then add a durable retry and demonstrate recovery.

Done when write-through's measured latency is strictly higher than write-back's, and the write-back loss window is reproduced, then bounded.

Shared lab setup

Key takeaway

Write-through trades write latency for a cache that's never ahead of the database; write-back trades durability for write speed. Done when you can: for a given write pattern, state which one is safe to lose and compute the exposure window. Used next in → 85 (invalidation, the read-side counterpart), → 87 (Redis, where both are implemented).

References

None — this topic makes no external claims.

Source directory

Search the guide