Series — 7 Cache Patterns (3 of 8). Previous: read-through. This post covers write-through. Next: write-behind. Overview hub publishes last.
Introduction
Read patterns (cache-aside, read-through) answer: “How do we load data into the cache?” Write patterns answer: “What happens when data changes?”
Write-through means every write goes to the cache and the database in the same call. When the write method returns successfully, both stores agree. The next read can hit the cache and see the new value immediately—no waiting for TTL expiry, no hoping someone remembered Cache::forget.
This guide covers write-through with a diagram, a real user-profile example, Laravel implementation, when it shines, and when write-behind or write-around is a better fit.
What is write-through?
On create/update/delete:
- Write to the database (source of truth).
- Write (or delete) the matching cache entry.
- Only then return success to the caller.
Some designs write cache first then DB; for application-level write-through, prefer DB then cache (or a transaction-style unit of work) so you never serve a cache value that never landed in the DB. The defining idea is the same: both happen in the write path before the caller is done.
Diagram: one write, two stores
Contrast with plain cache-aside updates that only touch the DB and leave a stale cache entry until TTL or a manual forget.
Real-world example: user profile after save
A user edits their profile (name, avatar, phone). The UI then redirects to “View profile” or an API client immediately GETs /me.
- If you only update MySQL and leave Redis stale, the next screen shows the old name until TTL expires.
- If you only forget the key (cache-aside invalidation), the next read is correct but pays a DB round-trip.
- With write-through, you update MySQL and put the new profile object in Redis in the same request. The following read is a cache hit with fresh data.
That matches the rule of thumb: use write-through for data that is read right after it is written.
Other good fits
- Shopping cart totals shown immediately after “update quantity”
- Feature flags flipped in admin, then checked on the next page load
- Document metadata after save in an editor that reloads the doc header
How to implement it (Laravel + Redis)
use App\Models\User;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
public function updateProfile(Request $request)
{
$user = $request->user();
$data = $request->validated();
$fresh = DB::transaction(function () use ($user, $data) {
$user->update($data);
$payload = $user->only([
'id', 'name', 'email', 'avatar_url', 'phone',
]);
// Write-through: cache matches DB before we return
Cache::put(
"user:profile:{$user->id}",
$payload,
now()->addHour()
);
return $payload;
});
return response()->json($fresh);
}
public function me(Request $request)
{
$id = $request->user()->id;
$profile = Cache::remember(
"user:profile:{$id}",
now()->addHour(),
fn () => User::query()
->select(['id', 'name', 'email', 'avatar_url', 'phone'])
->findOrFail($id)
->toArray()
);
return response()->json($profile);
}
Reads can still use cache-aside/remember for cold starts. Writes keep the cache warm and correct.
Deletes
Cache::forget("user:profile:{$user->id}");
// or Cache::put with a tombstone if you intentionally cache "missing"
Write-through vs write-behind vs invalidate-only
| Approach | On write | Freshness | Write latency |
|---|---|---|---|
| Invalidate only | DB + delete cache key | Next read reloads DB | Low |
| Write-through | DB + update cache | Immediate | Higher (two stores) |
| Write-behind | Cache first; DB later | Cache fresh; DB lags | Lowest for client |
When to use write-through
- Data is read immediately after write
- Stale cache after update is unacceptable (profiles, carts, flags)
- Write volume is moderate—you can afford a cache SET on every save
- You want a simple consistency story: “successful write = DB and cache agree”
When not to use write-through
- Very high write rate — consider write-behind (batch DB) if you can accept crash risk
- Write-often, read-rarely — write-around avoids filling the cache with cold keys
- Huge payloads — rewriting a large cached blob on every tiny field change can waste Redis bandwidth; invalidate may be cheaper
- Multi-key fan-out — one DB row feeds many cache keys; invalidating related keys may be safer than updating all of them inline
Failure modes to plan for
- DB succeeds, cache fails — prefer treating the request as failed or immediately falling back to invalidate; never claim success if you require both
- Cache succeeds, DB fails — avoid writing cache before DB commits; use transactions and put cache after commit
- Partial multi-key updates — document which keys must stay in sync
How to verify it works
- Update the profile via API.
- Read
/me(or profile page) — expect new data with no DB query if the cache was written. - Flush only Redis and read again — one DB load, then hits resume.
- Simulate cache put failure after DB update — confirm your error/invalidation handling.
Summary
Write-through = every write updates the cache and the database before the call returns. Use it when data is read right after it is written and you want the next request to hit a warm, correct cache. Trade-off: slightly slower writes; gain: simple freshness.
Next: Write-behind — write the cache first and flush to the database later in batches.