Caching Patterns: Read-Through, Write-Through, Cache-Aside in Practice
Three caching patterns, three failure modes. The one we use most, the one that bit us, and the rule that decides which pattern fits which workload.
Key takeaways
- Use cache-aside by default: the app reads the cache, falls back to the database on a miss, and deletes the key on every write.
- Read-through moves that miss logic into a library, and write-through updates the cache on every write for fresher reads at higher write latency.
- Avoid write-behind wherever losing writes matters.
On this page
Default to cache-aside: read the cache, fall back to the database on a miss, populate with a TTL, and delete the key on every write. Read-through is the same flow hidden inside a library. Write-through keeps reads fresh at the cost of slower writes. Write-behind trades durability for write speed, so keep it away from data you cannot lose.
Verified against
Redis SET options (NX, EX) against the redis.io command reference, and lazy loading, write-through and TTL guidance against the Amazon ElastiCache caching strategies docs. Checked October 2026.
What decides between them is the failure modes: what happens when the cache is stale, when it is down, and when two writes race. After running each pattern in production, this is the working version.
The three patterns, briefly
Cache-aside (lazy loading). The application reads from the cache directly. On miss, it reads from the DB, writes the result to the cache, returns it. On write, the application writes to the DB and either invalidates the cache or writes a fresh value.
Read-through. Application reads from the cache via an abstraction; the cache layer is responsible for fetching from the DB on miss. Application doesn't know about misses.
Write-through. Writes go to the cache first; the cache propagates the write to the DB. Cache and DB stay in sync by the cache library's choice of "write to both" or "write to cache, then async DB."
There's also write-behind: writes go to the cache, and DB writes are deferred and batched.
| Pattern | Who handles a miss | Stale window | Cache outage means | Write latency | Fits |
|---|---|---|---|---|---|
| Cache-aside | Application | Up to TTL, or ms with delete-on-write | Slower reads, still works | DB only | Most read-heavy apps |
| Read-through | Cache library | Same as cache-aside | Read failure unless library falls back | DB only | Uniform entity lookups |
| Write-through | Application or library on write | Near zero | Write path fails | DB plus cache | Read-mostly data needing fresh reads |
| Write-behind | Library, async | Near zero in cache | Lost unflushed writes | Cache only | Counters you can afford to lose |
Cache-aside: the default
This is what most teams should use, and what we use for most of our caching. The flow:
def get_user(user_id):
cache_key = f"user:{user_id}"
cached = cache.get(cache_key)
if cached is not None:
return cached
user = db.query(User).filter_by(id=user_id).one_or_none()
if user is not None:
cache.set(cache_key, user, ttl=300) # 5 min TTL
return user
def update_user(user_id, data):
db.update(User, user_id, data)
cache.delete(f"user:{user_id}") # invalidate
Where it wins: the application controls what gets cached, and a cache outage means slower reads, not failure.
Where it doesn't: a thundering herd on a hot miss (100 concurrent misses, 100 DB queries), a stale window up to the TTL, and invalidation discipline that lives in your code. The herd is the one that bites; the stampede section below covers the fix.
Read-through: rare in our stack
Read-through wraps the cache and DB behind a single read interface:
def get_user(user_id):
return read_through_cache.get(f"user:{user_id}") # may fetch from DB internally
Where it wins: cleaner application code, one place to change TTLs and key formats, and often built-in single-flight.
Where it doesn't: reads now depend on the library's fallback when the cache is down, and ad-hoc queries still need a manual path.
We use read-through in one specific service where the cache and DB are managed by the same library (Hibernate's second-level cache). For most services it's overkill.
Write-through: niche
Writes go to the cache first; the cache writes to the DB:
def update_user(user_id, data):
write_through_cache.set(f"user:{user_id}", data) # cache propagates to DB
Where it wins: reads are fresh after every write, and invalidation logic mostly disappears.
Where it doesn't: every write pays DB plus cache latency, the cache joins the write path's failure modes, and a replaced cache node starts empty. AWS's ElastiCache docs recommend pairing write-through with lazy loading for exactly that reason.
Write-through fits when reads vastly outnumber writes and consistency on reads is critical. For typical apps, cache-aside is better.
Write-behind: dangerous
Write-behind queues DB writes and applies them asynchronously. Writes are fast, and a cache crash before the flush loses them. We only use it for high-throughput counters where losing the last few seconds of writes is an accepted cost.
The consistency window
All caching patterns have a stale window: a time during which the cache has old data while the DB has new data. The window's size depends on the pattern:
- Cache-aside with TTL only: stale window = up to the TTL. A 5-minute TTL means up to 5 minutes stale.
- Cache-aside with explicit invalidation on write: stale window = the time between the write and the invalidation (usually milliseconds).
- Write-through: stale window close to zero (cache and DB updated together).
"Up to TTL" stale is fine for most reads, but not for a user who just edited their profile and reloads. That's where explicit invalidation on write matters.
The cache-stampede problem
When a hot entry expires, every request for that key misses at once and each one queries the DB to repopulate it. Result: a stampede.
Fixes:
- Single-flight (request coalescing). Only one request triggers the DB fetch; others wait for it. Some client libraries ship this; where they don't, a short-lived Redis lock does the job, as below.
- Stale-while-revalidate. Serve stale content while fetching fresh in the background. The first request after expiration serves stale and triggers an async refresh; subsequent requests see fresh.
- Probabilistic early refresh. Add jitter: refresh keys before they expire with some probability. That spreads the refresh load across time.
redis-cli SET lock:user:42 worker-7 NX EX 5
OK
redis-cli SET lock:user:42 worker-9 NX EX 5
(nil)
The first caller gets OK and rebuilds the entry; the second gets (nil) and waits or serves stale. EX 5 releases the lock if the rebuilder dies. The Redis docs point to Redlock when you need stronger lock guarantees, but for stampede protection a lost lock only costs a duplicate DB read.
We use stale-while-revalidate where it matters, single-flight elsewhere.
Cache invalidation by tag (not just key)
Invalidating by exact key is fine until you have related caches. "User updated their profile" should also invalidate the user list, the team page, and maybe the dashboard summary, and maintaining that list per write gets unmaintainable. With tag-based invalidation, cache writes specify tags (user:42, team:5) and invalidation clears every key with a tag.
Not all cache libraries support this. We use it where available and list keys manually elsewhere.
When NOT to cache
A few patterns where caching is wrong:
- User-specific content with low reuse. Caching user 42's dashboard for 5 minutes only helps if user 42 hits the page within 5 minutes. Many users won't.
- Write-heavy workloads. If you invalidate the cache on every write, you're doing extra work without read benefit.
- Things changing more often than your TTL. Hit rate approaches zero; you're just adding latency.
Start without caching, and add it when measurements justify it. If you are still picking the store itself, our Redis vs Memcached comparison covers the engine side.
What we monitor
- Cache hit rate per key prefix. We aim for above 80% on hot caches; lower means the cache isn't doing its job.
- Cache memory usage. Eviction during normal traffic means the cache is undersized.
- Average miss latency. What a cold read costs; a rising number points at the upstream DB.
Questions people ask
What is the difference between cache-aside and read-through?
The flow is identical: check the cache, query the database on a miss, store the result. The difference is who owns that logic. In cache-aside your application code does it, so a cache outage just means slower database reads. In read-through a cache library does it behind one get call, which gives cleaner code but couples reads to the library's fallback behaviour.
Should I update or delete the cache key on write?
Delete it. Writing the new value into the cache on update opens a race where two concurrent writers leave the older value cached. Deleting the key forces the next read to load the current row from the database. Pair the delete with a TTL as a safety net, so any invalidation you forget still expires within a known window.
How do I prevent a cache stampede?
Make sure only one request rebuilds an expired hot key. Either coalesce requests in process (single-flight), take a short Redis lock with SET key value NX EX 5 so other callers wait or serve stale, or refresh popular keys early with random jitter. Stale-while-revalidate is the gentlest option when slightly old data is acceptable.
What to read next
- CDN cache invalidation strategies: the same patterns applied at the edge
- Postgres connection pooling with PgBouncer in front of RDS: when cache misses make the DB the bottleneck
- Edge databases for low-latency apps: when the cache itself becomes the database
- Multi-provider LLM routing: failover, cost, load balancing: the same caching patterns applied to LLM responses
Cache-aside with a good invalidation story is enough for most teams. Reach for the other patterns only when measurements point at a specific bottleneck.
Sources
Cite this article
Kiril Urbonas (2026). Caching Patterns: Read-Through, Write-Through, Cache-Aside in Practice. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/caching-patterns-read-write-through-cache-aside
[Caching Patterns: Read-Through, Write-Through, Cache-Aside in Practice](https://www.devopsness.com/blog/caching-patterns-read-write-through-cache-aside) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)
More from Cloud
Explore more articles in this category
Best Practices: Cloud Networking Segmentation Patterns
A flat VPC is fine until you need to prove who can reach what. Five segmentation patterns that work in AWS without requiring a service mesh.
Kubernetes Cost Optimization for Teams: FinOps Tactics That Actually Work
Cut Kubernetes spend without hurting reliability using a practical FinOps playbook for rightsizing, autoscaling guardrails, showback, and weekly waste cleanup.
AWS Cost Audit: 7 Things We Found Wasting Money Every Month
A real cost audit uncovered idle load balancers, oversized RDS instances, and forgotten snapshots. Here's what we found and how we fixed each one.
You might have missed
Evergreen posts worth revisiting.