Skip to content
Velloreth
Programming 6 min read

Caching Strategies: Cache-Aside, Write-Through and TTL

A cache trades freshness for speed. Learn the strategies, the invalidation problem, and the patterns that avoid a stampede.

By

What a cache is

A cache is a copy of data that is faster to read than the source. The trade is freshness for speed: the copy may be stale.

Cache when the data is expensive to compute or fetch, and it is read far more often than it changes.

Do not cache data that must be exact, such as an account balance, unless the invalidation is reliable.

Cache-aside

The most common pattern. The application manages the cache.

async function getUser(id) {
  const cached = await redis.get(`user:${id}`);
  if (cached) return JSON.parse(cached);

  const user = await db.users.findById(id);
  await redis.set(`user:${id}`, JSON.stringify(user), 'EX', 300);
  return user;
}

On a hit, return the cached value.

On a miss, read the source, write the cache, and return.

The advantages: simple, and it caches only what is requested.

The disadvantages: the first request after a miss is slow, and the application must handle the invalidation.

Read-through

The cache itself reads the source on a miss.

const user = await cache.get(`user:${id}`, () => db.users.findById(id));

The advantage: the application does not handle the miss, so the logic is in one place.

The disadvantage: the cache needs a way to read the source, which couples it to the data layer.

Write-through

Write to the cache and the source together.

async function updateUser(id, data) {
  await db.users.update(id, data);
  await redis.set(`user:${id}`, JSON.stringify(data), 'EX', 300);
}

The advantage: the cache is always fresh, so a read after a write is correct.

The disadvantage: every write pays the cache latency, and a value that is never read is cached anyway.

Write-behind

Write to the cache and defer the source write.

async function updateUser(id, data) {
  await redis.set(`user:${id}`, JSON.stringify(data));
  queue.push({ id, data });  // flushed to the database later
}

The advantage: the write is fast, because it does not wait for the source.

The disadvantage: a crash before the flush loses the write. This is only acceptable when a small data loss is tolerable.

Write-around

Write to the source and invalidate the cache.

async function updateUser(id, data) {
  await db.users.update(id, data);
  await redis.del(`user:${id}`);
}

The advantage: the cache holds only data that is actually read, and the next read repopulates it.

The disadvantage: the next read is a miss, which is slow.

This is the most common pattern for a write-heavy workload, because it avoids caching a value that may never be read.

Invalidation

Cache invalidation is the hard part, and there are three approaches.

A TTL — the value expires after a fixed time. Simple, and it bounds the staleness. The pragmatic default.

An explicit invalidation — delete the key when the source changes. Exact, but it requires every writer to remember.

A versioned key — include a version in the key, and bump the version to invalidate.

const key = `user:${id}:v${version}`;

The versioned key is useful when a whole group of keys must be invalidated at once: bump the version, and every old key is ignored.

The honest assessment: a TTL is the answer for most cases. An exact invalidation is worth the complexity only when the staleness is unacceptable.

The cache stampede

Many requests miss the same key at once, so they all read the source and recompute.

The cause: a popular key expires, and every request for it misses.

The fix — a lock:

async function getWithLock(key, fetch) {
  const cached = await redis.get(key);
  if (cached) return JSON.parse(cached);

  const lock = await redis.set(`${key}:lock`, '1', 'NX', 'EX', 10);
  if (!lock) {
    await sleep(50);
    return getWithLock(key, fetch);  // retry, the other request is filling it
  }

  const value = await fetch();
  await redis.set(key, JSON.stringify(value), 'EX', 300);
  await redis.del(`${key}:lock`);
  return value;
}

The fix — a jittered TTL: add a random amount to the TTL, so keys do not expire together.

const ttl = 300 + Math.floor(Math.random() * 60);

The fix — a background refresh: refresh the value before it expires, so a request never sees a miss.

What to cache

Cache the expensive and the frequent. A query that takes 200ms and runs on every page load is a good candidate.

Do not cache the cheap. A query that takes 1ms gains little, and the cache adds a network round trip.

Do not cache the user-specific in a shared cache without keying by the user. A shared cache that returns one user’s data to another is a serious bug.

Cache at the right layer:

  • The browser — Cache-Control on a static asset.
  • The CDN — a response cached by URL at the edge.
  • The application — a computed value in Redis.
  • The database — a query cache or a materialised view.

HTTP caching

Cache-Control is the main directive.

Cache-Control: public, max-age=31536000, immutable

immutable tells the browser never to revalidate, which is safe for a hashed filename.

Cache-Control: no-cache

no-cache does not mean “do not cache”; it means “revalidate before using”. The name is a common source of confusion.

Cache-Control: no-store

no-store means do not cache at all, which is the right directive for sensitive data.

ETag and If-None-Match enable a conditional request, so a revalidation returns a 304 with no body.

Common mistakes

Caching without a TTL. A value that never expires is stale forever.

A shared cache for user data. One user sees another’s data.

No stampede protection. A popular key expires and the source is overwhelmed.

Caching a negative result forever. A miss that is cached as “not found” hides a record that is later created.

Invalidating the wrong key. A write that deletes a key with a different format leaves the stale value in place.

A cache that is a single point of failure. If the cache is down and the application cannot fall back to the source, the cache has reduced availability rather than improved it.

Practical guidance

  • Use cache-aside as the default.
  • Set a TTL on everything.
  • Jitter the TTL to avoid a stampede.
  • Use a lock for an expensive key.
  • Cache the expensive and the frequent.
  • Key a user-specific value by the user.
  • Use no-store for sensitive data.
  • Fall back to the source if the cache is down.
  • Remember no-cache means revalidate, not “do not cache”.

For the related guides, see how-cdns-actually-work and database-indexing-explained. For the broader context, rest-api-design-principles and improve-inp-core-web-vitals-static-site.

Quotable summary

A cache is a copy of data that is faster to read than the source, at the cost of being potentially stale, so cache the data that is expensive to compute and read often rather than everything. Cache-aside is the most common pattern: the application checks the cache, and on a miss reads the source and populates the cache. Cache invalidation is the hard part, and a TTL is the pragmatic answer when an exact invalidation is difficult, with a jittered TTL preventing a cache stampede where many requests miss the same key at once. Remember that no-cache means revalidate before using rather than do not cache, and no-store is the directive for sensitive data.

Frequently asked questions

What is cache-aside?
The application checks the cache first. On a hit it returns the value; on a miss it reads the source, writes the value to the cache, and returns it. The application manages the cache.
What is the difference between write-through and write-behind?
Write-through writes to the cache and the source together, so the cache is always fresh but the write is slower. Write-behind writes to the cache and defers the source write, which is faster but risks data loss.
What is a cache stampede?
Many requests miss the same key at once, so they all read the source and recompute the value. A lock, a jittered TTL, or a background refresh prevents it.
What TTL should I use?
As short as the freshness requirement allows. A value that changes rarely can have a long TTL; a value that must be fresh needs a short one or an explicit invalidation.
What is a CDN cache?
A cache at the edge, close to the user. It caches a response by URL, which is why a static asset with a hashed filename can be cached for a year.
Should I cache a user-specific response?
Only with care. A shared cache that returns one user's data to another is a serious bug. Use a private cache directive, or key the cache by the user.