Cache-Aside, Write-Through, Write-Back: Picking the Right One Isn't Obvious
Three cache write strategies trade consistency for write latency differently. Pick wrong and you get slow writes, or data loss you didn't expect.
On this page
Every cache eventually has to answer two separate questions that get bundled together as one vague decision: “how do I cache this.” The first is a write-strategy question — when data changes, how does the cache find out, and does the application wait for that to finish. The second is an eviction question — when the cache is full, what earns the right to stay. Both questions have real, well-understood answers, and picking the wrong one for your actual workload is a quiet, compounding cost rather than an obvious failure.
The write-strategy decision: who’s in charge of staying in sync
Cache-aside: simple, widely used, and not automatically safe
Cache-aside is the pattern most people mean by “just cache it” — read from cache, fall back to the database on a miss, write to the database, then update or invalidate the cache entry. Its simplicity is real, but so is a coordination gap: there’s a window between the database write completing and the cache update happening where a concurrent read can observe stale data, and if the application crashes in that window, the cache can stay stale until it separately expires.
write(key, value):
db.write(key, value) // step 1
cache.invalidate(key) // step 2 — if this never runs, cache is stale
This isn’t a reason to avoid cache-aside — it’s the most common pattern in production for good reason — but “the application manages both steps” is a responsibility, not a guarantee that comes bundled in for free.
Write-through: consistency by construction, at write-latency cost
Write-through closes that gap by making the cache itself responsible for the database write, synchronously, before the write is considered done. The application never has to remember a second step — but every write now pays the latency of the database write on the critical path, which is exactly the cost cache-aside’s “update the database, then separately touch the cache” pattern was trying to keep out of the hot path for reads.
Choosing write-through is choosing to never have a cache/database consistency gap, at the direct cost of every write now waiting on the database. If your workload is read-heavy with occasional writes, that trade is usually a clear win. If it’s write-heavy, that cost compounds fast.
Write-back: the fastest option, with a real failure mode
Write-back defers the database write entirely — the application gets acknowledged the instant the cache is updated, and the database catches up asynchronously, later. This is meaningfully faster for write-heavy workloads, and it’s also the only one of the three with genuine data-loss exposure: if the cache fails before the deferred write reaches the database, that write is gone.
Do
Reach for write-back specifically when write latency is the dominant cost and the data involved is either replaceable, redundantly available elsewhere, or explicitly judged worth the risk against the latency win.
Don't
Use write-back for data where loss is genuinely unacceptable — financial transactions, anything without a redundant source of truth. The latency win isn’t worth the failure mode there, and write-through or cache-aside with a durable, synchronous database write is the safer default.
The eviction decision: what earns the right to stay when the cache is full
A cache with unlimited capacity wouldn’t need an eviction policy at all — the whole question exists because memory is finite and something has to go when it fills up.
- LRU (Least Recently Used) — bets on recency
Evicts whichever entry hasn’t been accessed in the longest time, on the assumption that recent access predicts near-future access. Cheap to implement, works well for workloads with genuine temporal locality — a user’s recent activity, a recently-viewed product.
- LFU (Least Frequently Used) — bets on overall popularity
Evicts whichever entry has the lowest total access count, on the assumption that historical popularity predicts future access better than recency alone. Protects consistently popular items from being evicted just because they weren’t touched in the last few minutes — at the cost of adapting more slowly when popularity genuinely shifts.
- TTL (Time-To-Live) — bounds staleness, independent of capacity
Expires an entry after a fixed duration regardless of access pattern or cache fullness. Solves a different problem entirely from LRU/LFU: not “what should we evict under memory pressure” but “how stale is this value allowed to become before we no longer trust it.”
The LRU-versus-LFU divergence shows up clearest on one specific case: a genuinely popular item that just hasn’t been touched in the last few minutes. LRU sees “not recently accessed” and evicts it as apparently stale. LFU sees “high total access count” and protects it. Neither is universally correct — it depends on whether your workload’s popularity is closer to “recency-driven” (LRU’s bet) or “genuinely sticky over time” (LFU’s bet).
Most production systems don’t pick one axis alone — they combine a capacity-driven policy (LRU or LFU, deciding what to evict when full) with a TTL (deciding when something’s too old to trust, independent of whether the cache is even full).
Takeaway
“Add a cache” quietly bundles two separate engineering decisions, and treating either one as an afterthought is how a caching layer becomes a source of subtle bugs instead of a clean performance win. The write-strategy choice — cache-aside, write-through, or write-back — is a direct trade between write latency and consistency risk, and the right answer depends on how write-heavy your workload is and how tolerable a small data-loss window actually is for that specific data. The eviction choice — LRU, LFU, TTL, or some combination — is a bet about what “worth keeping” means for your actual access pattern. Neither decision has a universally correct default; both deserve to be made deliberately, for the specific data going through them, rather than inherited from whatever the caching library ships as its default.
Frequently asked questions
What's the fundamental difference between cache-aside and write-through?
Cache-aside puts the application in charge: on a write, the application updates the database and either updates or invalidates the cache entry itself, as a separate step. Write-through puts the cache in charge: the application writes to the cache, and the cache synchronously writes through to the database before the write is considered complete. The difference is who owns the responsibility of keeping the two in sync, and whether that sync happens as one atomic-feeling operation or two separate ones the application coordinates.
Why would anyone accept write-back's data-loss risk instead of just using write-through?
Write-back's entire value proposition is write latency — the application gets an acknowledgment the instant the cache is updated, without waiting for the database write at all, which meaningfully speeds up write-heavy workloads. The trade is real: if the cache fails before the deferred database write happens, that data is gone. Systems that accept this have usually decided the specific data involved is either replaceable, redundantly stored elsewhere, or worth the latency win despite the risk.
Does cache-aside guarantee the cache and database never disagree?
No — there's a real window between the database write and the cache update/invalidation where a concurrent read could see stale data, and if the application crashes between those two steps, the cache can be left stale indefinitely until it separately expires. Cache-aside is simple and widely used, but 'the application manages both steps' means both steps have to actually both happen, which is a real coordination responsibility, not a guarantee that comes for free.
What's the difference between LRU and LFU eviction, and when does the choice matter?
LRU (Least Recently Used) evicts whichever entry hasn't been accessed in the longest time — it assumes recent access predicts near-future access. LFU (Least Frequently Used) evicts whichever entry has been accessed the fewest total times — it assumes overall popularity predicts future access. They diverge on a specific case: a very popular item that hasn't been touched in the last few minutes. LRU might evict it as 'stale'; LFU would protect it because of its high historical frequency, at the cost of being slower to adapt when popularity genuinely shifts.
Why does TTL-based eviction exist alongside LRU or LFU instead of replacing them?
TTL (time-to-live) solves a different problem than capacity management — it bounds how stale a cached value is allowed to become, independent of how often it's accessed. LRU and LFU decide what to evict when the cache is full; TTL decides when a value should be considered too old to trust regardless of capacity. Most production caches combine both: TTL for staleness bounds, LRU or LFU for what to evict under memory pressure.


/* Comments */
Comments are offline right now — we reconnect automatically, nothing is lost.