Md Tarek — Portfolio© 2026

Full-stack Developer

System Design — built to hold up.

dhaka → remote, worldwide

Loading experience

Md TarekTarek
Tech

Redis Caching Patterns: Cache-Aside, Write-Through & Invalidation

By: Md Tarek · 17 Oct 2026

Redis caching architecture showing cache-aside pattern between app and database
Redis caching architecture showing cache-aside pattern between app and database

Executive Summary & Key Takeaways

  • Caching is a consistency trade, not a free lunch. Every pattern answers the same question: how stale can this value be before someone gets hurt?
  • Cache-aside is the default. The app checks the cache, falls back to the DB on miss, and writes the value back. It is simple and lazy, but the first request after an invalidation pays the DB cost.
  • Write-through / write-behind keep the cache fresh by updating it on write — at the cost of write latency (through) or durability risk (behind).
  • Invalidation is the hard part. Namespaced keys, versioned keys, and short TTLs beat trying to perfectly delete every related key on every mutation.
  • Cache stampede is a real outage. On a hot-key miss, hundreds of requests hit the DB at once — use request coalescing, early refresh, or jittered TTLs.

1. Cache-Aside (Lazy Loading)

In cache-aside the application owns the logic: read from cache, on miss read from the database and populate the cache. It is resilient — if Redis goes down, the app just hits the DB directly — and it never caches data nobody asked for.

The downside is the thundering-herd on a cold start and the fact that the first reader of a just-invalidated key pays the full DB latency.

const KEY = (id) => `note:${id}`;

async function getNote(id) {
  const cached = await redis.get(KEY(id));
  if (cached) return JSON.parse(cached);
  const row = await db.query('SELECT * FROM notes WHERE id = $1', [id]);
  await redis.set(KEY(id), JSON.stringify(row), 'EX', 300);
  return row;
}

2. Write-Through and Write-Behind

Write-through updates the cache synchronously on every write, so reads always see fresh data — but every write now pays cache latency. Write-behind batches writes to the DB asynchronously, which is fast but can lose data on crash and makes the cache the source of truth, which is dangerous.

| Pattern | Read latency | Consistency | Complexity | Failure mode | |---|---|---|---|---| | Cache-aside | low after warm | eventual | low | stampede, cold misses | | Write-through | low | strong | low | write latency | | Write-behind | low | eventual (risky) | high | data loss on crash |

3. Invalidation Strategies

Delete-on-write is the simplest correct approach: when data changes, delete the key and let the next read repopulate it. For related objects, use a namespaced or versioned key (note:${version}:${id}) so you can bump the version instead of deleting dozens of keys. Short TTLs act as a safety net for whatever invalidation you inevitably miss.

FAQ

When should I not cache? Data that changes every second, data where staleness is unacceptable (payments), or queries that are already indexed and fast. Cache what is read-heavy and relatively stable.

How do I avoid the thundering herd? Coalesce concurrent misses with a single-flight lock, or refresh the key slightly before it expires (early refresh), or add random jitter to TTLs.