Catalog My learning Blog
System Design Roadmap: 100-Day Course for Engineers · Phase 4 · Lesson 32 · 4 min read

Cache-Aside vs Write-Through vs Write-Behind

On this page
  1. Cache-aside vs write-through vs write-behind compared
  2. Cache-aside (lazy loading): the default
  3. Write-through caching
  4. Write-behind (write-back) caching
  5. Worked example: two caching strategies for one product
  6. Version your cache keys across deploys
  7. Check yourself

Cache-aside vs write-through vs write-behind compared

You've decided to cache. Now: who puts data into the cache, and what happens on writes? There are four standard answers. The right one depends on how often the data is read versus written, and on what a stale read or a lost write would cost.

StrategyWho loads the cacheWhat happens on writeMain risk
Cache-asideThe app, on a missApp writes the database, then deletes the cache keyStale reads in between
Read-throughThe cache library, on a missUsually combined with one of the write strategiesSame as cache-aside
Write-throughWrites populate itCache and database written togetherSlower writes; caches unread data
Write-behindWrites populate itCache now, database laterLosing writes not yet flushed
Cache-aside loads on a miss, write-through writes the cache and database together, and write-behind writes the cache first and the database later.
Cache-aside loads on a miss, write-through writes the cache and database together, and write-behind writes the cache first and the database later.

Cache-aside (lazy loading): the default

The application handles everything:

Code
read(key):
    value = cache.get(key)
    if value is missing:
        value = db.load(key)
        cache.set(key, value, ttl)
    return value

update(key, new_value):
    db.save(key, new_value)
    cache.delete(key)          # next read reloads fresh data

Only data someone reads ends up cached, and if the cache is down, reads fall back to the database. Delete the key on update rather than writing the new value into the cache: two concurrent updates writing values into the cache can leave the older one there, while a delete just forces a fresh load. Delete narrows the window for stale data but doesn't close it; Day 36 shows the race that remains and how a TTL bounds it.

Read-through is the same idea with the loading logic moved into the cache library: the app calls cache.get and the library loads from the database on a miss. Cleaner code, same behavior.

Write-through caching

Every write updates the database and the cache together. Reads right after a write see the new value, and the cache is always warm for written data. Costs: each write does two writes, and you cache data that may never be read. It fits data that's read immediately and often after it changes.

The two writes can't be one transaction, so order them: commit the database first, then write the cache. If the cache write fails, delete the key, and let the TTL catch anything the delete misses. Never write the cache first; if the database write then fails, the cache holds a value that never existed.

Write-behind (write-back) caching

Writes go to the cache, the caller gets success immediately, and a background process flushes changes to the database, often in batches. Write latency is minimal and the database gets far fewer, bigger writes. But anything not yet flushed is lost if the cache node fails. Use it only where losing a few seconds of updates is acceptable.

Worked example: two caching strategies for one product

  • Product details: read thousands of times per minute, edited a few times a week. Cache-aside with a 10-minute TTL, plus a delete when someone edits the product.
  • Product view counter: tens of thousands of increments per minute; nobody cares if it's off by a few for a few seconds. Write-behind: INCR in Redis, and a job writes the totals to the database every 30 seconds. The database sees 2 writes a minute per product instead of 20,000.

Two pieces of data on the same page, two different strategies, each chosen by its own read/write pattern and tolerance for staleness or loss.

Version your cache keys across deploys

Cached values are serialized objects. Deploy a release that renames a field on ProductDto, and new servers read old cached JSON in the new shape: missing values or deserialization errors until every old entry expires. During a rolling deploy, old and new servers also overwrite each other's format. Put a schema version in the key, product:v3:42, and bump it when the shape changes. Each version reads only its own entries, and old ones age out.

Check yourself

Concurrent updates can write values into the cache in the wrong order, leaving stale data. A delete forces the next read to load the current value from the database.

Write-through: update the cache immediately after the database commit, as part of the same operation, so the next read sees the new balance. Or don't cache the balance at all and read it from the primary. Plain cache-aside allows a window of stale reads that the requirement forbids.

Orders accepted but not yet flushed to the database are lost if the cache fails. Orders need durable storage before you confirm them.

New code read cached entries in the old shape until they expired. Include a schema version in cache keys and bump it when the cached type changes.