System Design Academy

Saving…

Lesson 2 of 9 in Module 40 of 54 lessons complete

Caching Strategies

Module 4, lesson 2Concept10 min

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

StrategyWho fills itWrite pathFreshnessRiskGood for
Cache-asideApplication on missDatabase, then remove cached copyUntil expiry or copy removalStale copiesBook details
Read-throughCache on missSeparate write policyDepends on write policyCache dependencyRepeated reads
Write-throughWrite pathBoth stores before successUpdated path sees new valueSlower writes and partial failuresLive scores
Write-behindWrite pathCache, then database laterCache has latest writesUnflushed data lossRecoverable counts
Write-aroundRead path on missDatabase directlyOld copies need removalStale existing copiesRarely read writes

The app checks the cache, reads the missing book from the database, and saves it with an expiry.

The application fills a cache-aside miss.

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.

Cache-aside steps written as plain pseudocode.
Text
book = cache.get(book_id)
if book is missing:
    book = database.read(book_id)
    cache.set(book_id, book, ttl_seconds=3600)
return book

The insight

Cache-aside is simple and only caches what's actually read.

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

Which strategy can lose recent writes if the cache crashes?
In cache-aside, who loads missing data into the cache?

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.

Try the quick check above whenever you feel ready.

Previous: What Is Caching?