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

Write-Through Caching

Write operations update both cache and data source synchronously, ensuring cache consistency but adding latency to writes.

Write-Through Caching

You'll learn how write-through caching keeps your cache and data source perfectly synchronized at write time—at the cost of added latency.

What Is Write-Through Caching?

In write-through caching, every write operation updates both the cache and the underlying data source (like a database) at the same time. The write is only considered successful when both updates complete. This is the opposite of writing only to cache first or bypassing cache entirely.

Think of it like keeping a personal notebook and an official ledger in perfect sync: whenever you write something down, you update both immediately before moving on. Nothing gets out of sync, but it takes longer to complete each entry.

How It Works

When your application writes or updates data:

  1. The cache layer receives the write request
  2. The cache updates its own stored value
  3. The cache also writes the same data to the underlying data source
  4. Only after both complete does the operation return success

This ensures strong consistency—the cache always reflects what's in the database. There's no window where they disagree.

The Trade-Off

The benefit is cache consistency: you never have stale data in cache that contradicts your source of truth.

The cost is write latency: every write now requires two operations (cache + database), making writes slower than if you only wrote to one place. If your database is slow, your cache writes become slow too.

Write-through works well when you need guaranteed consistency and can tolerate slightly slower writes—or when reads vastly outnumber writes.

Key Takeaway:
Write-through caching updates both cache and data source synchronously, guaranteeing consistency but increasing write latency by requiring both updates to complete before confirming success.