Performance

5 min read

November 22, 2026

Redis caching strategies that don't serve stale data

Redis caching patterns explained: cache-aside, write-through and write-behind, safe invalidation, versioned keys, stampede protection, the right data structures and designing for Redis being down.

By Parth Patel

Adding Redis in front of a slow database is one of the quickest performance wins available. It is also one of the quickest ways to serve stale prices, show one user another user's data, or take the database down the moment the cache restarts. The difference is in the caching strategy.

This guide covers the main Redis caching patterns, how to keep cached data correct, and the failure modes to design for.

First, decide what is worth caching

Cache data that is read far more often than it changes and expensive to produce: product catalogues, configuration, permission lookups, computed dashboards, responses from slow third-party APIs.

Be careful with data where staleness has a cost: stock levels at checkout, account balances, anything a user just changed and expects to see. For those, either don't cache or use the shortest safe lifetime and explicit invalidation.

Measure before caching. If a query is slow because of a missing index, fix the index; a cache only hides it.

Cache-aside: the default pattern

In cache-aside (lazy loading), the application checks the cache first and fills it on a miss:

TypeScriptTypeScript
async function getProduct(id: string): Promise<Product> {
  const key = `product:v2:${id}`;
  const cached = await redis.get(key);
  if (cached) return JSON.parse(cached);

  const product = await db.products.findById(id);
  await redis.set(key, JSON.stringify(product), "EX", 300 + Math.floor(Math.random() * 60));
  return product;
}

Three details in that snippet matter:

  • A version in the key (v2). When the shape of the cached object changes, bump the version instead of flushing the whole cache.
  • A time-to-live on every key. Even with invalidation, a TTL is your safety net against stale data that nothing invalidated.
  • Jitter on the TTL. If thousands of keys expire at the same moment, the database gets the whole load at once.

Write-through and write-behind

Write-through updates the cache at the same time as the database. Reads are always warm, but writes are slower and you cache data that may never be read.

Write-behind writes to the cache and persists to the database later. It is fast, but data can be lost if the cache fails before the write lands. Avoid it for anything you can't afford to lose.

For most applications, cache-aside plus explicit invalidation is the right balance.

Invalidation: the hard part

When the underlying data changes, the cached copy must go. The safe order is update the database, then delete the cache key, not update the cache:

TypeScriptTypeScript
await db.products.update(id, changes);
await redis.del(`product:v2:${id}`);

Deleting (rather than writing the new value) avoids a race where two concurrent updates leave the older value in the cache.

In a system with several services, invalidate from events: when Catalogue publishes ProductUpdated, every service that caches product data deletes its keys. That keeps invalidation logic next to the cache that needs it.

Protect the database from stampedes

When a popular key expires, hundreds of requests can miss at once and all hit the database together: a cache stampede. Defences:

  • Request coalescing. Only one request recomputes; the rest wait for its result. A short Redis lock (SET key:lock 1 NX EX 10) works across instances.
  • Early refresh. Refresh a hot key in the background shortly before it expires.
  • Serve stale while revalidating. Keep a stale copy for a little longer and return it while one worker refreshes.

Choose the right Redis data structure

Redis is more than a key-value store. Using the right structure often removes work:

  • Hashes for objects whose fields you update independently.
  • Sorted sets for leaderboards, "latest N" feeds and time-ordered data.
  • Sets for membership checks such as feature flags per tenant.
  • Counters with INCR and a TTL for rate limiting.

Plan for Redis being unavailable

A cache is an optimization, so the application should survive without it:

  • Wrap cache calls with short timeouts and fall back to the database on failure.
  • Limit the fallback load with a circuit breaker, or a cold cache becomes a database outage.
  • Configure a sensible eviction policy (allkeys-lru for pure caches) and a memory limit, so Redis evicts instead of failing writes.

Never make Redis the only copy of important data unless you have configured persistence and replication for exactly that purpose.

Measure the cache

Track hit rate, latency, memory use and evictions. A cache with a low hit rate costs money and adds a network hop for nothing. A sudden drop in hit rate after a deploy usually means a key format changed.

Summary

  1. Cache data that is read often, changes rarely and is expensive to produce.
  2. Default to cache-aside, with versioned keys, TTLs and jitter.
  3. Update the database first, then delete the key; invalidate from events across services.
  4. Put the user or tenant in every key that holds their data.
  5. Prevent stampedes with coalescing or stale-while-revalidate.
  6. Design for Redis being down, and measure hit rate.

Caching sits alongside the other levers in PostgreSQL performance optimization; use it after the database is healthy, not instead.

Next step

Working on something like this? Let’s talk it through.

Send a short brief. You’ll get a written reply from me within two business days — either a few clarifying questions, a suggested first step, or an honest “I’m not the right fit” with a pointer elsewhere.

Parth Patel

Senior full-stack engineer