Write-Through Cache
Table of Contents + −
So you’ve added a cache to make your reads fast. Nice. But now there’s a tricky question hiding in there:
- A cache is just a fast copy of some data that lives somewhere slower, like a database. (A database is the main, permanent store of your data.)
- Reading from the cache is great, until someone changes that data.
- The moment data changes, you’ve got two copies floating around, the one in the cache and the one in the database.
So here’s the real headache: when data changes, how do you keep the cache and the database in sync on every write? That’s exactly what a write strategy decides, and write-through is one of the cleanest answers. Let’s walk through it.
🎯 The Write Problem
Before we name any strategy, let’s feel the actual pain. Picture this:
- Alex updates their profile name from “Al” to “Alex”. That write needs to land somewhere.
- If you only update the database and forget the cache, the cache is now lying. It still says “Al”.
- The next read hits the cache, sees the old value, and shows the wrong name. That stale value is called a cache miss on freshness, or more simply, a stale cache. (Stale just means out of date.)
So the problem is this:
- A cache speeds up reads, but writes can break it.
- Every time data changes, you have to decide what happens to the cached copy.
- That decision is your write strategy. It tells the system how a write should touch the cache and the database.
Write-through is one such strategy. Let’s see what it actually does.
✍️ What is Write-Through
Write-through is a strategy where every write goes to the cache and the database at the same time, before you tell the user the write succeeded. Let’s unpack that:
- When a write comes in, you don’t pick one place. You write to the cache and the database together.
- Only after both have the new value do you confirm success back to the user.
- So the data is written “through” the cache and straight into the database in one go. That’s where the name comes from.
Here’s the flow when Alex saves that new name:
Notice both arrows fire together, and only when both finish does the user hear “saved”. That’s the heart of write-through.
⚙️ How It Works
Let’s slow it down and trace one write from start to finish:
- A write request comes in, say Alex changing their name.
- The system writes the new value into the cache.
- It also writes the same value into the database.
- It waits until both writes are done.
- Then, and only then, it confirms success.
Now here’s the payoff on the read side:
- Because the cache was updated on the write, the cache always holds the latest value.
- So the next read just grabs it straight from the cache, and it’s guaranteed fresh.
- No checking the database to see if the cache went stale. The cache and database already agree.
Write-through is about writes, reads stay simple
The clever part of write-through happens on the write. Reads stay boring on purpose: you just read from the cache. Since every write already refreshed the cache, a normal read is always up to date. Boring reads are a good thing.
⚡ Benefits
So why would you reach for write-through? A few solid reasons:
- Cache and database stay consistent. Every write touches both, so they never drift apart. Consistent here means both copies hold the same value.
- Reads after a write are fresh. The moment a write finishes, the cache already has the new value. So a read right after a write never sees old data.
- No lost data. Because the database is written on every single write, nothing lives only in the cache. Even if the cache is wiped, the database still has everything.
That last point matters a lot. Some strategies keep data only in the cache for a while, and if the cache crashes, that data is just gone. Write-through doesn’t take that risk.
⚠️ The Cost
Nothing is free, right? Write-through buys you freshness, but you pay for it:
- Writes are slower. You’re writing twice, once to the cache and once to the database, and waiting for both. So each write takes longer than writing to just one place.
- You cache data nobody reads. Every write fills the cache, even for data that might never be read again. That wastes cache space on cold data. (Cold data is data that rarely gets read.)
- More write traffic. Doubling every write means more load on your system at write time.
Don't use write-through for write-heavy data
If your data gets written constantly but read rarely, write-through hurts. You’d be paying the double-write cost over and over, and filling the cache with stuff nobody reads. Write-through shines when reads heavily outnumber writes, not the other way around.
🔀 Write-Through vs Write-Back
Write-through has a close cousin called write-back, and people mix them up all the time. Here’s the quick difference:
- Write-through writes to the cache and the database together, then confirms. Safe and fresh, but the write waits for both.
- Write-back writes to the cache first, confirms right away, and writes to the database later in the background. Faster writes, but riskier. If the cache crashes before that later write, you lose data.
Let’s put them side by side:
| Aspect | Write-Through | Write-Back |
|---|---|---|
| When the database is written | Immediately, with the cache | Later, in the background |
| Write speed | Slower (waits for both) | Faster (cache only, then confirm) |
| Risk of data loss | Very low | Higher (cache crash loses pending writes) |
| Consistency | Cache and database always agree | Can lag until the background write lands |
We’ll go deeper on the faster-but-riskier cousin in the write-back lesson linked at the bottom.
✅ When to Use It
Write-through fits a specific shape of workload. Reach for it when:
- Your data is read-heavy. Lots of reads, fewer writes. That way the slow write happens rarely, but every read benefits from a fresh cache.
- Freshness really matters. If serving stale data would be a problem, like account balances or profile info, write-through keeps things current.
- A bit of write latency is okay. You can afford writes to take a little longer in exchange for safety and consistency. (Latency is just how long an operation takes.)
A user profile is a perfect example: read often, updated rarely, and you really don’t want to show someone the wrong name or email.
⚠️ Common Mistakes and Misconceptions
A few things trip people up. Let’s clear them out:
- “Write-through is always the best strategy.” No. It’s great for read-heavy, freshness-sensitive data, but for write-heavy data it just adds cost. The right strategy depends on your read-to-write mix.
- “Write-through is as fast as writing to the cache only.” It isn’t. You write to both the cache and the database and wait for both, so it’s slower than a cache-only write by design.
- “Write-through and write-back are the same thing.” They’re opposites in spirit. Write-through writes the database right away; write-back delays it. One favors safety, the other favors write speed.
- “It removes the database.” Not at all. The database is still your permanent store. Write-through just keeps the cache in sync with it on every write.
🛠️ Design Challenge
Try this on your own to test yourself.
Imagine you’re designing the settings page for a banking app. Account balances and contact details are read constantly but updated rarely, and showing a stale balance would be a serious problem. Walk through it:
- Would write-through be a good fit here? Why?
- What happens to a read that comes right after a balance update?
- Now flip it: imagine a feature that logs every button click a user makes, written thousands of times a second but almost never read. Is write-through still a good idea there?
Reason through the read-to-write mix each time. That’s exactly how you’d defend a caching choice in an interview.
🧩 What You’ve Learned
You can now explain how write-through keeps a cache honest. Here’s what you’ve picked up.
- ✅ A write strategy decides how a write touches the cache and the database.
- ✅ Write-through writes to the cache and the database together, before confirming success.
- ✅ Reads are always fresh because every write already updated the cache.
- ✅ It keeps the two in sync and never loses data, since the database is written every time.
- ✅ The cost is slower writes and caching data that may never be read.
- ✅ It fits read-heavy, freshness-sensitive data, not write-heavy workloads.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is a write-through cache?
Why: Write-through updates both the cache and the database together, and only confirms success once both have the new value.
- 2
Why are reads always fresh with write-through?
Why: Because each write refreshes the cache, a later read just returns the up-to-date value without checking the database.
- 3
What is the main downside of write-through?
Why: Each write goes to both the cache and database and waits for both, so writes are slower and may cache data nobody reads.
- 4
When would you choose write-through?
Why: Write-through fits read-heavy data where freshness matters and a bit of extra write latency is acceptable.
🚀 What’s Next?
You’ve got the safe, consistent write strategy down. Next, compare it with the alternatives.
- Write-Back Cache shows the faster-but-riskier cousin that writes to the database later.
- Cache-Aside Pattern shows the strategy where your app loads the cache on demand instead of on every write.
Once you’ve seen all three, you’ll be able to pick the right caching strategy for any workload.