Course contents

06. Caching Lesson 4 of 8

Write Through

Send writes through the cache to the database: see the write path, why reads stay fresh, and how write latency and availability follow the database.

13 minIntermediate7 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Write-through updates the database and the cache in the same request and acknowledges after both.
  • The next read of a written key is a hit with the new value; recently written data is warm.
  • Write latency is the database write plus the cache update; writes fail when the database is down.
  • Every write is a synchronous database write, so bursts hit the database at full force.
  • Every written value takes cache memory: route write-only data around the cache.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

Write-through is a write policy: every write goes to the cache and the underlying database synchronously, as part of the same request, and is acknowledged only when both have the new value. It is usually paired with read-through, so the cache sits in front of the database for reads and writes.

The term is used for two arrangements that behave the same for the caller: a cache product that writes to its configured store (a cache "store" or "writer" behind it), or application code that writes the database and then updates the cache before returning. The lesson draws the first.

Why it exists

Cache aside and read-through only fill the cache on reads, and handle writes by invalidating (deleting) the key. That leaves a miss after every write and race windows in which an old value can be put back. Write-through keeps the cache current instead: the value just written is already in memory, so the next read is a hit with the new value, and data that was recently written (often the data about to be read) is warm.

How it works

  1. The application sends put(key, value) to the cache.
  2. The cache writes the database (the source of truth).
  3. If that succeeds, it stores the value in memory.
  4. It acknowledges the write.

Ordering matters. Writing the database first means a failed database write never leaves a value in the cache that the database does not have. Updating memory before the database risks exactly that if the database write then fails, unless it is undone.

Latency. The steps are sequential, so write latency ≈ database commit + cache update + hops. Write-through never makes writes faster than writing the database directly; it shifts the benefit to reads.

Availability. A write cannot be acknowledged without the database. During a database outage writes fail, while reads that hit the cache can still be served (with the last written values) and misses fail.

Load. Every write is a database write at the moment it happens, so the database must be sized for the peak write rate.

Writes that bypass the cache (migrations, scripts, other services writing the database directly) leave the cache stale. Keep a TTL as a backstop, or route all writes through the same path.

When to use it

  • Data that is read soon and often after being written, and must look current: prices, profiles, settings, inventory displays.
  • When you already use a read-through cache and want reads to see writes without a miss.
  • When write rates are moderate relative to database capacity.

When not to

  • Write-heavy or bursty data where the database cannot take the peak synchronously (consider write-behind or a queue).
  • Write-only or rarely read data (logs, events, audit trails): use write-around, which writes the database and skips the cache.
  • When the lowest possible write latency matters more than read freshness.

Trade-offs

Choice You gain You pay
Write-through Fresh reads, no miss after a write, warm recent data Write latency = DB + cache; writes bound to DB availability and capacity
Write-around Cache holds only data that is read First read after a write misses; needs invalidation for cached keys
Invalidate on write (cache aside) Simple, no cache write cost Miss after every write, race windows
Write-behind Fast writes, burst smoothing Durability risk, database lags the cache

Alternatives

  • Cache aside with invalidation: delete the key on write and let the next read reload it.
  • Write-around: skip the cache for writes; reads fill it when needed.
  • Write-behind: acknowledge after the cache and flush to the database asynchronously.
  • Change data capture: update or invalidate caches from the database's change stream, which also catches writes that bypass the cache.

Failure modes

  • Database outage: all writes fail; only cached reads survive.
  • Slow database: write latency rises one-for-one with commit time (for example when synchronous replication is added).
  • Write bursts: the database saturates and writes queue, time out or fail; reads that miss compete for the same connections.
  • Partial failure: database write succeeds but the cache update fails (or the reverse in the wrong order). Detect and invalidate, and keep a TTL.
  • Cache pollution: write-heavy, rarely read data evicts the hot read set.

In production

Write the database first, then the cache, and treat a failed cache update as "invalidate the key". Keep TTLs as a safety net for writes that bypass the cache. Route write-only data around the cache. Monitor write latency against database commit latency, cache eviction rates, and database write utilisation; plan database capacity for the peak write rate.

Common misconceptions

  • "Write-through makes writes faster." It makes them slightly slower than writing the database alone.
  • "Write-through guarantees the cache is always consistent." Writes that bypass it, and partial failures, still create stale entries.
  • "The cache protects the database from write spikes." Every write reaches the database synchronously.
  • "Write-through and write-behind differ only in speed." They differ in durability and in when the database sees the write.

Interview angle

Place write-through on the spectrum: invalidate on write (cache aside), write-through, write-behind. Say when it acknowledges, what it costs (latency, database-bound availability and capacity), and what it buys (fresh, warm reads). Mention write-around for write-only data, the database-first ordering, and TTLs as the backstop for bypassing writes.

Model assumptions

The simulation is a simplified model, not a benchmark. The cache service is drawn as two simulated handlers sharing one memory (an LRU of 1,000 keys): the read path checks memory and loads misses from the database; the write path writes the database, then stores the value in memory, then acknowledges. The store is modelled as a memory lookup right after the write, which always hits; those confirmations are included in the hit figure on the memory node, so read hit ratios quoted in the lesson count read lookups only. Product pages read 5,000 products (Zipf(1)), price updates write the same products with the same skew; both arrive as Poisson processes. The database has 8 connections, 40 ms reads and 30 ms writes (lognormal, cv 0.4); waiting more than 300 ms for a connection fails a query. The slow-motion walk-through is scripted packets, not simulated traffic. Numbers show behaviour and orders of magnitude only.

The lesson as textEvery step of the simulation, written out.
  1. 1. Writes go through the cache to the database

    Product pages read through the cache as in the read-through lesson. Price updates now also go through the cache: the cache writes the new price to the database, stores it in its memory, and only then acknowledges the write. Both copies are updated synchronously, inside the request.

    Watch the lower path: every update reaches the database, and right after it the cache memory. If the database write fails, the update fails and the memory is not touched.

    Scene: Product pages read through the cache service. Price updates go to the cache’s write path, which writes the database, then stores the value in cache memory, then acknowledges. Nothing fails.

    Key idea: Write-through: a write is acknowledged only after both the database and the cache have the new value.

  2. 2. Writes pay for the database, reads do not

    Compare the two paths. A read that hits costs a few milliseconds. A write always waits for the database commit (about 30 ms here) plus the cache update, so the typical write is several times slower than the typical read.

    Write-through never makes writes faster: it adds the cache step on top of the database write. What it buys is on the read side: the value just written is already in memory, so the next read of that product is a hit with the new price, and recently written data is warm.

    Scene: Same system. Reads that hit the cache finish in a few milliseconds; every write waits for the database commit and the cache update and takes several times longer.

    Key idea: Write latency = database commit + cache update. Write-through buys fresh, warm reads, not fast writes.

  3. 3. Read your writes from the cache

    Live traffic is paused to follow one update. The price of product 42 changes: the write path commits it to the database, stores it in memory, then acknowledges. The next page view of product 42 is a hit with the new price; the database is not involved.

    With cache aside the write would delete the key instead, so the next read misses and reloads it, and a slow concurrent reader can even put the old price back. Write-through avoids that miss and that race for writes that go through the cache. A write that bypasses it (a script updating the database directly) still leaves the cache stale, so keep a TTL. This walk-through is scripted.

    Scene: Live traffic is paused. A scripted price update goes to the write path, which writes the database, stores the new value in memory and acknowledges. Then a page view reads the same product from memory: a hit with the new price, no database query.

    Key idea: Because the cache is updated in the same request, the next read sees the new value without a miss.

  4. 4. Write latency follows the database

    Traffic is back. One second in, the database’s writes slow down from 30 ms to 75 ms (say, it now waits for a synchronous replica in another zone).

    Every update waits for that, so the typical write latency jumps by about the same 45 ms. Reads that hit the cache do not notice; only the misses wait a little longer, because they share the database with the slower writes. With write-through, your write latency is the database’s write latency plus a bit; the cache cannot hide it.

    Scene: Traffic is back. One second in, database writes slow from 30 to 75 ms. Every price update takes correspondingly longer; reads that hit the cache stay fast.

    Key idea: Write-through couples write latency to the slowest synchronous store.

  5. 5. No database, no writes

    Writes are back to 30 ms. One second in, the database goes down. Every price update now fails: write-through cannot acknowledge a write the database did not take, and the cache must not keep a value the database never stored.

    Reads behave differently: hits are still served from memory, misses fail. So during the outage the shop keeps showing popular products, as of the last write.

    Scene: One second in, the database goes offline. Every price update fails. Page views that hit the cache are still served; those that miss fail.

    Key idea: Write-through writes are only as available as the database; cached reads survive a database outage.

  6. 6. The cache does not absorb writes

    The database is back. Two seconds in, a bulk price import pushes updates from 40 to 300 per second. Every one of them is a synchronous database write, so the database saturates: writes queue for connections, latency climbs and some updates fail. Page views that miss suffer too, because they wait for the same connections.

    Write-through protects reads, not the database’s write capacity. To absorb write bursts you need to acknowledge before the database write, which is the write-behind pattern in the next lesson, with its own risks.

    Scene: The database is back. Two seconds in, price updates jump to 300 per second. Every update is a synchronous database write; the database saturates, latency climbs and some requests fail.

    Key idea: Every write-through write is a database write: write bursts hit the database at full force.

  7. 7. When to choose write-through

    The import is over. Write-through fits data that is read soon after it is written and must look current: prices, profiles, settings. Writes cost a database commit plus a cache update and are only as available as the database; reads get fresh values without a miss.

    Two costs to manage: every written value takes cache memory, even if nobody reads it (for write-only data such as logs, skip the cache: write-around), and writes that bypass the cache still need invalidation or a TTL.

    Scene: Price updates are back to 40 per second. Reads mostly hit the cache, writes go through the cache to the database, and nothing fails.

    Key idea: Write-through: fresh reads and simple consistency, paid for with write latency and database-bound write capacity.