Node.js & Libuv•Part 5: Production Scale, Deployment & Observability (51 - 60)
Cache-Aside pattern with Redis in Node.js.
In Simple Words: App checks Redis first; if key is found, returns cached JSON immediately; otherwise fetches from DB, caches in Redis with TTL, and returns.
javascript
async function getProduct(id) {
const cached = await redis.get(`product:${id}`);
if (cached) return JSON.parse(cached);
const product = await db.products.findById(id);
if (product) await redis.set(`product:${id}`, JSON.stringify(product), 'EX', 3600);
return product;
}