Caching Strategies
Definition
Caching strategy
- In plain English
- A caching strategy is the rule for when data goes into the cache and how writes reach the database.
- Think of it like
- It is like a café's rule for when the counter display is restocked from the kitchen.
Cache-aside
- In plain English
- Cache-aside means the app checks the cache, and on a miss reads the database and puts the result in the cache itself.
- Think of it like
- Like checking the fridge and, if it's empty, shopping and restocking it yourself.
Read-through
- In plain English
- Read-through means the app only talks to the cache, and the cache fetches missing data from the database.
- Think of it like
- Like a butler who fetches anything missing from the cellar for you.
Write-through
- In plain English
- Write-through means every write goes to the cache and the database together before it counts as done.
- Think of it like
- Like updating the shop sign and the price list at the same moment.
Write-behind
- In plain English
- Write-behind means writes land in the cache first and reach the database later in batches.
- Think of it like
- Like jotting orders on a notepad and typing them up every hour.
Write-around
- In plain English
- Write-around means writes go straight to the database and the cache fills only when the data is read.
- Think of it like
- Like filing new books directly in the archive, displaying them only when someone asks.
Why it exists
You change a score, but the cache still shows the old value. Who should update that copy?
Saving only in memory makes writes fast. A crash can erase those writes before the database receives them.
How it works
| Strategy | Who fills it | Write path | Freshness | Risk | Good for |
|---|---|---|---|---|---|
| Cache-aside | Application on miss | Database, then remove cached copy | Until expiry or copy removal | Stale copies | Book details |
| Read-through | Cache on miss | Separate write policy | Depends on write policy | Cache dependency | Repeated reads |
| Write-through | Write path | Both stores before success | Updated path sees new value | Slower writes and partial failures | Live scores |
| Write-behind | Write path | Cache, then database later | Cache has latest writes | Unflushed data loss | Recoverable counts |
| Write-around | Read path on miss | Database directly | Old copies need removal | Stale existing copies | Rarely read writes |
The app checks the cache, reads the missing book from the database, and saves it with an expiry.
Writing both stores is not automatically one transaction. Handle partial failures and prevent concurrent readers from restoring older copies.
Examples
Library ebooks
Book details use cache-aside with a 1-hour expiry; a miss loads from the database and fills the cache.
Only requested books occupy the cache. A one-hour TTL limits how long a missed update can remain.
book = cache.get(book_id)
if book is missing:
book = database.read(book_id)
cache.set(book_id, book, ttl_seconds=3600)
return bookThe insight
Cache-aside is simple and only caches what's actually read.
Esports scoreboard
Live scores use write-through. A successful update completes both writes before readers on that cache path see the new score.
Readers must use the updated cache path. Other copies still need their own freshness rules.
Score update → Write database and cache → Confirm score.
The insight
Write-through keeps reads fresh at the cost of slower writes.
Fitness wearable
Step counts reach the cache first and the database every minute. An unprotected cache crash can lose unflushed steps.
Assume the device cannot resend its counts. Losing the cache before a flush can lose up to one minute of steps.
- Confirm that this loss is acceptable for the product.
- Do not use this unprotected path for payments.
The insight
Write-behind trades durability for write speed.
Trade-offs
Pros
- Match storage rules to each data type.
- Reduce repeated reads.
- Batch database writes when loss is acceptable.
Cons
- Some policies allow stale reads.
- Write-through waits for more work.
- Unprotected write-behind can lose data.
Use when
- Choose using freshness requirements.
- Choose using acceptable data loss.
Avoid when
- Avoid unprotected write-behind for money.
- Avoid extra cache policies without repeated reads.
Quick check
Takeaways
- Cache-aside lets the application fill misses.
- Write-through waits for writes; write-behind saves to the database later.
- Choose each policy using freshness and loss tolerance.