Redis Caching Patterns: Cache-Aside, TTL, and Invalidation
October 10, 2026 · 6 min read
Adding Redis to a backend is easy; keeping cached data consistent with your primary database is the real design work.
The Cache-Aside pattern
- 1. Check Redis first using a structured key like `user:profile:42`.
- 2. On a cache hit, return immediately without touching Postgres/MongoDB.
- 3. On a cache miss, query the DB, store the result in Redis with a TTL (`EX 300`), and return it.
- 4. On writes (`PATCH /users/42`), delete (`DEL`) the stale key so the next read refreshes cleanly.
const cached = await redis.get(key);
if (cached) return JSON.parse(cached);
const fresh = await db.findUser(id);
await redis.set(key, JSON.stringify(fresh), { EX: 300 });
return fresh;Always set a TTL on cached keys — even if an invalidation bug misses a write path, bounded expiration prevents stale data from living forever.