Backend & Node.js · APIs, data and failure
What caching strategies should a backend engineer know?
Short answer
Know cache-aside, read-through, write-through and write-behind, plus TTLs and layered caches. Cache-aside is the common default: read the cache, load from the database on a miss, then populate with a TTL.
A cache trades freshness and complexity for latency and load reduction. The right strategy depends on read and write patterns, how stale data can be and whether a miss storm would overwhelm the origin.
The common patterns
- Cache-aside: the application reads the cache, loads on a miss and writes the result back
- Read-through: the cache layer loads from the origin on a miss
- Write-through: writes go to the cache and the origin together, favoring consistency
- Write-behind: writes go to the cache and are flushed to the origin later, risking data loss
- Layered caches: browser, CDN, application memory and a shared store such as Redis
- Negative caching: store not-found results briefly to stop repeated misses
async function getUser(id) {
const key = "user:" + id;
const cached = await redis.get(key);
if (cached) return JSON.parse(cached);
const user = await db.user.findUnique({ where: { id } });
if (user) {
await redis.set(key, JSON.stringify(user), { EX: 300 });
}
return user;
}Every cache needs a policy for misses. If many requests miss the same key at once, they all hit the database. Add a short lock, a single-flight mechanism or TTL jitter to prevent a stampede. Also decide what happens when the cache is unavailable: serving from the origin is often better than failing the request.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is cache-aside and when does it break down?
- 2How do you prevent a cache stampede?
- 3What is the difference between write-through and write-behind?
- 4Should a cache failure fail the request?
Say it in the interview
Cache-aside is my default: check the cache, load from the database on a miss and populate with a TTL. I use write-through when consistency matters more than write latency and write-behind only when some data loss is acceptable. The hard parts are invalidation, stampedes and negative caching, so I add TTL jitter and single-flight protection. A cache is a trade-off, not a free performance win.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Backend & Node.js mock interview and get scored feedback on depth, clarity and confidence.