Course contentsShow
← Software Engineering
Lesson 978 of 1,93330. Caching StrategiesFree lesson

Write-Through and Write-Back Invalidation

Updating or invalidating cache entries synchronously or asynchronously on writes.

Write-Through and Write-Back Invalidation

You'll learn how caches decide when and how to invalidate stale entries after data is written.

When data changes in your underlying system, your cache needs to reflect that change—or risk serving stale data. Write-through invalidation handles this synchronously: the moment a write occurs, the cache entry is immediately invalidated or updated before the write operation completes. Your application waits for both the database write and the cache update to finish.

Think of it like updating both your whiteboard and your notebook at the same time during a meeting—you don't consider the note "saved" until both are done.

Write-back invalidation (also called write-behind invalidation) takes an asynchronous approach: the write completes in the primary store first, and the cache invalidation happens afterward—sometimes delayed by seconds or even queued for batch processing. Your application doesn't wait; it moves on immediately after the database write.

This is like jotting something in your notebook during the meeting, then erasing the outdated whiteboard version during your next break.

Trade-offs

Write-through guarantees immediate consistency between cache and database. Users never see stale data after a write completes. However, it adds latency to every write operation—you pay the synchronous cost.

Write-back offers faster write operations since you're not blocked by cache updates. The downside? Temporary inconsistency. A read immediately after a write might hit stale cached data until the invalidation catches up. This works well for eventually consistent systems or when you can tolerate brief staleness windows.

When to Use Which

Choose write-through when correctness is critical and users expect immediate visibility of their changes. Choose write-back when write performance matters more and your system can handle brief inconsistency windows.

Key Takeaway: Write-through invalidation updates caches synchronously for immediate consistency but slower writes; write-back invalidation updates asynchronously for faster writes but temporary staleness.