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
- The application sends
put(key, value)to the cache. - The cache writes the database (the source of truth).
- If that succeeds, it stores the value in memory.
- 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.