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
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;
}
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:
- TTL-based: Set expiry. Simple but data can be stale for up to TTL duration.
- Event-based: On write, delete or update the cache key. Consistent but adds complexity.
- 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
max-age: browser cache. s-maxage: CDN cache. stale-while-revalidate: serve stale while refreshing in background.
Common Mistakes
- Caching without TTL: Memory fills up. Always set expiry.
- Cache stampede: 1000 requests hit empty cache simultaneously. All query the database. Use a lock or stale-while-revalidate.
- Caching user-specific data with shared keys: Cache key must include user ID or session.
- 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)