← Back to Backend, page 1

backend-0016 · Easy fresher

How do the caching strategies cache-aside, read-through, write-through and write-behind differ, and what are their trade-offs?

Cache-aside: the application manages the cache. Read-through: the cache loads from the database on a miss for you. Write-through: every write goes to the cache and the database together, so reads are fresh but writes are slower. Write-behind (write-back): writes go to the cache and are saved to the database later in batches, which is fast and merges repeated updates, but data can be lost if the cache fails before the flush.

Full answer
  • Cache-aside: the app reads the cache, on a miss loads the database and fills the cache. The app deletes keys on writes. Flexible, the most common. Stale data is possible until a delete or the TTL.
  • Read-through: the cache layer itself fetches from the database on a miss (the app only talks to the cache). Same freshness as cache-aside, less code in the app, depends on the cache product.
  • Write-through: a write updates the cache and the database in the same operation (the write is complete only when both succeeded). Reads after a write are fresh, but each write is as slow as the database, and rarely read data fills the cache.
  • Write-behind: a write updates the cache and returns. A background job flushes changes to the database later. Very fast writes, repeated updates to one key become one database write (coalescing). Risk: data loss if the cache node dies before the flush, and the database is briefly out of date, which complicates other readers.
  • Write-around: write to the database only, and let the cache fill on reads.

Choosing: cache-aside for most services. Write-through when reads must see the latest value and the write rate is modest. Write-behind only for data you can afford to lose a little of (counters, view counts, analytics), with a durable queue or a log when you cannot.

javascript
// Simulated database and cache with counters, to compare the write strategies.
const makeDb = () => ({ rows: new Map(), writes: 0, write(k, v) { this.writes++; this.rows.set(k, v); } });

function writeThrough(db) {
  const cache = new Map();
  return { cache, write(k, v) { db.write(k, v); cache.set(k, v); } };       // both, before returning
}

function writeBehind(db) {
  const cache = new Map(), dirty = new Set();
  return {
    cache,
    write(k, v) { cache.set(k, v); dirty.add(k); },                          // returns at once
    flush() { for (const k of dirty) db.write(k, cache.get(k)); dirty.clear(); },
    crash() { cache.clear(); dirty.clear(); },                               // the cache node dies
  };
}

const updates = Array.from({ length: 5 }, (_, i) => ['views:page1', 100 + i]);   // the same counter updated 5 times

const db1 = makeDb(), wt = writeThrough(db1);
updates.forEach(([k, v]) => wt.write(k, v));
console.log('write-through: database writes =', db1.writes, '| database value =', db1.rows.get('views:page1'));

const db2 = makeDb(), wb = writeBehind(db2);
updates.forEach(([k, v]) => wb.write(k, v));
console.log('write-behind : before flush, database writes =', db2.writes, '| database value =', db2.rows.get('views:page1'));
wb.flush();
console.log('write-behind : after flush,  database writes =', db2.writes, '| database value =', db2.rows.get('views:page1'));

const db3 = makeDb(), wb2 = writeBehind(db3);
updates.forEach(([k, v]) => wb2.write(k, v));
wb2.crash();
wb2.flush();
console.log('write-behind : crash before flush, database has the value =', db3.rows.has('views:page1'), '(updates lost)');

Counters show the cost of each strategy. This is a model, not a Redis test, and real write-behind systems use durable queues to reduce loss. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): write-through: database writes = 5 | database value = 104 write-behind : before flush, database writes = 0 | database value = undefined write-behind : after flush, database writes = 1 | database value = 104 write-behind : crash before flush, database has the value = false (updates lost)

Key points

  • Cache-aside is app-managed; read-through is cache-managed.
  • Write-through keeps cache and database in step at the cost of write latency.
  • Write-behind is fast and batches writes but can lose data.
  • Pick by freshness need and loss tolerance.
  • Most systems start with cache-aside.

Common mistakes

  • Using write-behind for money or orders.
  • Expecting write-through to make the cache and database atomic across crashes.
  • Caching write-through data that is never read.
  • No monitoring of the flush backlog in write-behind.
  • Mixing strategies for the same key.

Follow-ups

  • How does a write-ahead log help write-behind?
  • What happens in write-through if the cache update fails after the database write?
  • When is write-around a good choice?

Tips

  • Define each in one line.
  • Give the main risk of each.
  • Name an example use case.