Untitled Lesson
Skill Matrix
UNLOCK NODES BY LEARNING NEW TAGS.
Which caching invalidation strategy ensures that data is practically NEVER stale by updating the Redis cache at the exact same moment the SQL database is updated?
š» Code Challenge | +75 XP
Write the Node.js/Backend code snippet to implement a Node Cache Invalid Strategies pipeline. Include the setup and basic execution steps.
You are reviewing a Node Cache Invalid Strategies pipeline and the output is incorrect. Reorder the following pipeline stages in the correct logical order to fix the bug: Input Data, Process, Output.
Task: Reorder the blocks in logical sequence to solve the problem.
A.D.A. Interface
Adaptive Didactic Assistant

Pascual Vila
Frontend Instructor // Code Syllabus
The Error //
Forgetting to invalidate the cache on delete, only handling update
// Wrong: delete forgets to clear the cache
const deleteProduct = async (id) => {
await db.products.delete(id);
// missing: await redis.del(`product:${id}`);
};
// Correct
const deleteProduct = async (id) => {
await db.products.delete(id);
await redis.del(`product:${id}`);
};The Solution //
Teams often wire up cache invalidation for updateProduct() but forget deleteProduct() entirely, so a deleted item keeps 'existing' in the cache and gets served to users until its TTL finally expires. Every code path that mutates a row ā create, update, and delete ā needs the matching cache invalidation call, not just the most common one.
The Error //
Rebuilding a hot cache key with no lock, causing a thundering herd on expiry
// Wrong: every concurrent miss queries the DB
let data = await redis.get('home');
if (!data) { data = await db.query(); await redis.set('home', data); }
// Correct: only one request rebuilds, others wait
if (!data) {
const locked = await redis.setnx('lock:home', '1');
if (locked) { data = await db.query(); await redis.set('home', data); await redis.del('lock:home'); }
else { await sleep(50); data = await redis.get('home'); }
}The Solution //
When a popular key's TTL expires, every concurrent request sees a cache miss and hits the database at once, which can take an otherwise healthy database down in seconds under real traffic. Use a lock (e.g. Redis SETNX) so only the first request rebuilds the cache while the rest wait briefly and re-read the now-populated cache.