Loading post…
Loading post…
4 Sept 20261 min read
Cache-aside, TTL discipline and stampede protection without overengineering.
async function getProduct(id) {
const hit = await redis.get(`product:${id}`);
if (hit) return JSON.parse(hit);
const doc = await Product.findById(id).lean();
if (doc) await redis.set(`product:${id}`, JSON.stringify(doc), "EX", 300);
return doc;
}
When a hot key expires, fifty requests can dogpile Mongo at once. The cheap fix: stale-while-revalidate, serve the stale copy and refresh in the background.
Cache the reads that hurt. Measure first, cache second.
Links