Write-Back Cache
Table of Contents + −
Imagine you’re building something where writes have to be blazing fast.
- Picture a game where every player click, jump, and score has to be saved.
- Or a counter that ticks up every single time someone likes a post.
- Writing all of that straight to the database would be slow, and the user would feel it.
So here’s the question: what if the user didn’t have to wait for the database at all? What if you could say “saved!” almost instantly, and deal with the slow database part quietly in the background? That’s exactly what a write-back cache does. Let’s see how.
✍️ What is Write-Back
Let’s start with the name, then the idea.
- A write-back cache is a strategy where your app writes new data to the cache first, and the cache confirms it right away.
- The cache stands between your app and the database. A cache is just a small, fast store that keeps data close so you don’t always hit the slower database.
- The actual save to the database happens later, in the background, not right when the user is waiting.
- This strategy is also called write-behind, because the database write happens “behind” the user, after the fact. Write-back and write-behind mean the same thing.
So the user’s write lands in the cache, gets a quick “done!”, and moves on. The database catches up a little later. Here’s the flow.
⚙️ How It Works
Let’s walk through what actually happens when a write comes in.
- Your app sends a write, like “set score to 500”. The cache takes that value and stores it in its fast memory.
- The cache immediately replies “done”. The user is free to move on, and they never waited for the database.
- The new value now lives only in the cache for a little while. The database still has the old value, or no value at all.
- A background process wakes up after some time and pushes those changes into the database. A background process is just a helper task running quietly on its own, separate from the user’s request.
Now here’s the clever part. That background flush doesn’t have to send writes one by one.
- Say a player’s score changed fifty times in ten seconds. The cache doesn’t need fifty database writes.
- It can keep only the latest value and write it once. Or it can group many changes together and send them in one go.
- Bundling many writes into fewer trips like this is called batching, and it saves the database a ton of work.
⚡ Benefits
So why would you reach for write-back? It comes down to speed and efficiency.
- Very fast writes. The user only waits for the cache, never the database. Memory is way faster than disk, so the response feels instant.
- Fewer database writes. Thanks to batching, many small changes can collapse into one write. Your database does less work and handles more traffic.
- Great for bursts. When writes come in fast and heavy, like a flood of likes or live game updates, the cache soaks up the burst and drips it into the database calmly.
Why this feels so fast
Writing to a database often means writing to disk, which is slow. Writing to a cache means writing to memory, which is quick. Write-back lets the user feel the memory speed and never feel the disk speed.
⚠️ The Big Risk
Now, nothing comes for free. Write-back is fast, but it has a real danger you must understand.
- For a short window, the new data lives only in the cache. The database hasn’t been updated yet.
- If the cache crashes during that window, before the background flush runs, that data is gone. There’s no copy in the database to fall back on.
- This means you can actually lose data, which never happens when every write goes straight to the database.
That gap, the time between “cache said done” and “database actually saved”, has a name.
- It’s called the consistency window. During this window, the cache and the database disagree about what the real value is.
- A bigger window means faster, more batched writes, but more data at risk if something fails.
- A smaller window means safer data, but less of the speed benefit. You’re trading safety for speed, and you get to pick where on that line you sit.
The cache is temporarily the only copy
In write-back, freshly written data sits in just one place, the cache, until the flush happens. If you treat that data as if it’s already safely saved, a crash can bite you. Always plan for the case where the cache disappears.
🔀 Write-Back vs Write-Through
To really get write-back, it helps to compare it with its safer cousin, write-through.
- In write-through, every write goes to the cache and the database at the same time, before the user gets a “done”. Nothing is confirmed until the database has it.
- That makes write-through safe, because the database always has the latest data. But it’s slower, because the user waits for the database every single time.
- Write-back flips that. It confirms fast and saves later, so it’s quick but riskier.
Here’s the side-by-side.
| Question | Write-Through | Write-Back |
|---|---|---|
| When does the DB get the data? | Right away, with the cache | Later, in the background |
| How fast are writes? | Slower (waits for the DB) | Very fast (waits for cache only) |
| Risk of data loss? | None, the DB always has it | Yes, if the cache fails before flush |
| Can it batch writes? | No, each write hits the DB | Yes, many writes become fewer |
| Best for | Data you can’t afford to lose | Write-heavy, loss-tolerant workloads |
✅ When to Use It
So when does write-back actually make sense? Think about the shape of your workload.
- Write-heavy workloads. If your system takes way more writes than reads, and those writes come fast, write-back keeps things fast.
- When some risk is okay. Things like view counts, like counts, or game telemetry. If you lose a few of these in a rare crash, the world doesn’t end.
- When you add safeguards. Serious systems don’t just hope the cache survives. They make the cache itself durable.
Those safeguards are worth knowing about.
- Persistence means the cache also saves its data to disk, so a restart doesn’t wipe everything. Persistence is just storing data somewhere it survives a crash.
- Replication means keeping copies of the cache on more than one machine. If one dies, another still has the data. Replication is simply keeping duplicate copies running.
- With these in place, write-back gets much safer, and that’s how real systems use it for serious traffic.
⚠️ Common Mistakes and Misconceptions
A few ideas trip people up here. Let’s clear them out.
- “Write-back is just a faster write-through with no downside.” Not true. The speed comes from delaying the database write, and that delay is exactly where the data-loss risk lives. There’s no free lunch.
- “The data loss risk doesn’t really matter.” It absolutely can. If the cache crashes mid-window, unflushed writes vanish for good. Ignoring this is how people get burned.
- “I’ll use write-back for critical data because it’s fast.” Be very careful. For things like payments or orders, plain write-back without persistence or replication is risky. Use write-through, or add strong safeguards first.
- “The database always has the current value.” In write-back it often doesn’t, at least not yet. During the consistency window the cache is ahead, and the database is catching up.
🛠️ Design Challenge
Try this one yourself to test the idea.
You’re building a live leaderboard for a mobile game. Scores update many times per second per player, and players want their rank to update instantly. But the game studio also wants final scores saved reliably for prizes.
- Where would write-back help, and why? Think about which part needs raw speed.
- Where is the data-loss risk dangerous, and how would you guard against it?
- Would you use write-back everywhere, or mix it with write-through for the final, prize-deciding scores?
Sketch out your answer. Mixing strategies based on how much each piece of data matters is exactly the kind of thinking interviewers love to see.
🧩 What You’ve Learned
You can now explain the write-back strategy and when to reach for it. Here’s what you’ve picked up.
- ✅ Write-back (also called write-behind) writes to the cache first, confirms instantly, and saves to the database later in the background.
- ✅ A background process flushes data to the database, and can batch many writes into far fewer trips.
- ✅ It gives very fast writes and is great for write-heavy bursts.
- ✅ The big risk is data loss if the cache fails during the consistency window, before the flush.
- ✅ Write-through is safe but slower, while write-back is fast but riskier.
- ✅ For serious use, add safeguards like persistence and replication, and keep critical data on safer strategies.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is a write-back cache?
Why: Write-back (write-behind) confirms the write from the cache immediately and flushes to the database later in the background.
- 2
Why is write-back faster than write-through?
Why: The user waits only for the in-memory cache, while the slow database write is pushed to the background.
- 3
What is the main risk of write-back?
Why: During the consistency window the cache is the only copy, so a crash before the flush loses that data.
- 4
How does write-back reduce database load?
Why: Batching lets many changes to the same data collapse into a single write, so the database handles far fewer operations.
🚀 What’s Next?
You’ve got one half of the write-strategy picture. Next, line it up against the alternatives.
- Write-Through Cache shows the safe, write-everything-now approach you’ve been comparing against.
- Distributed Caching explains how caches spread across many machines, which is exactly how write-back stays safe at scale.
Once you’ve got both, you’ll be able to pick the right write strategy for any system you design.