DEV Community

Young Gao
Young Gao

Posted on

Caching Strategies Explained: When, Where, and How to Cache in 2026

Caching Strategies: When, Where, and How to Cache

Your API hits the database on every request. 90% of those queries return the same data. You are paying for the same computation over and over.

The Cache Hierarchy

Browser Cache -> CDN -> Application Cache (Redis) -> Database Cache -> Database
Enter fullscreen mode Exit fullscreen mode

Each layer catches requests before they hit the next. The closer to the user, the faster the response.

Cache-Aside (Lazy Loading)

The most common pattern. Check cache first. On miss, query database and populate cache:

async function getUser(id: string): Promise<User> {
  const cached = await redis.get(`user:${id}`);
  if (cached) return JSON.parse(cached);
  const user = await db.query("SELECT * FROM users WHERE id = $1", [id]);
  await redis.setex(`user:${id}`, 3600, JSON.stringify(user));
  return user;
}
Enter fullscreen mode Exit fullscreen mode

Write-Through vs Write-Behind

Write-through: Write to cache and database simultaneously. Consistent but slower writes.

Write-behind: Write to cache, asynchronously sync to database. Fast writes but risk data loss on crash.

Cache Invalidation

The hardest problem in computer science (after naming things). Three approaches:

  1. TTL-based: Set expiry. Simple but data can be stale for up to TTL duration.
  2. Event-based: On write, delete or update the cache key. Consistent but adds complexity.
  3. Version-based: Include version in cache key. Bump version on write. Old entries expire naturally.

HTTP Caching Headers

Cache-Control: public, max-age=3600, s-maxage=86400
ETag: "abc123"
Vary: Accept-Encoding
Enter fullscreen mode Exit fullscreen mode

max-age: browser cache. s-maxage: CDN cache. stale-while-revalidate: serve stale while refreshing in background.

Common Mistakes

  1. Caching without TTL: Memory fills up. Always set expiry.
  2. Cache stampede: 1000 requests hit empty cache simultaneously. All query the database. Use a lock or stale-while-revalidate.
  3. Caching user-specific data with shared keys: Cache key must include user ID or session.
  4. Not monitoring cache hit rate: If your hit rate is below 80%, your TTL or key strategy is wrong.

Part of my Production Backend Patterns series. Follow for more practical backend engineering.

Top comments (0)