Lesson 5 / 25
Read-Through, Write-Through, Write-Behind and Write-Around
Compare write strategies and their consistency and durability trade-offs.
Who writes, and when
In read-through, the cache library or service itself loads missing data from the source using a loader you configure, so application code only talks to the cache. Write-through writes to the cache and the database together on every update, so cached data is always fresh, at the cost of slower writes and caching data that may never be read. Write-behind (write-back) writes to the cache first and flushes to the database asynchronously, often in batches: very fast writes and smoothed database load, but data can be lost if the cache fails before flushing, and the database lags behind. Write-around writes only to the database and lets reads populate the cache later, avoiding cache pollution with write-once data. Most web applications combine cache-aside reads with invalidate-on-write; write-behind suits high-volume counters or metrics where losing a few seconds of data is acceptable.
Comparing write strategies
Choose by freshness, write latency and tolerance for data loss.
strategy write path freshness write latency data-loss risk
-------------- ------------------------------ ---------- -------------- ---------------
invalidate DB, then delete cache key high DB only none
write-through DB and cache together high DB + cache none
write-behind cache now, DB later (batched) cache high very low yes, unflushed
DB lags
write-around DB only, cache fills on read stale until low none
miss/TTLNotes and the register
Write-through is updating the official register and your notebook at the same time. Write-behind is scribbling in your notebook and copying it into the register at the end of the day: quick, unless you lose the notebook first.
Quick check: Which strategy risks losing recent updates if the cache crashes?
- Write-through
- Write-behind (write-back)
- Write-around
- Cache-aside with invalidation
Answer
Write-behind (write-back) — Write-behind acknowledges writes before they reach the database.