Interview frequency: …
Short answer
The server checks the Cache first and returns the data if present; otherwise it fetches the data from the database, caches it with a time-to-live (TTL), and invalidates it when the data changes.
Explanation
With Cache-Aside (Lazy Loading), the Node.js application directly controls reads and writes to the Cache, often Redis. To avoid serving stale data, set a considered TTL and immediately invalidate the key on updates or deletes, or use strategies such as Write-Through for critical systems.
Code example
const cached = await redis.get(key);
if (cached) return JSON.parse(cached);
const data = await db.fetch(id);
await redis.set(key, JSON.stringify(data), "EX", 300);
return data;Common mistakes
- Caching objects without a TTL, causing memory growth and stale data to be served indefinitely.