MidDatabases

What caching patterns do you use in front of a database, and how do you keep the cache consistent?

What they are really testing: Whether you know cache-aside vs write-through, and that invalidation/consistency is the genuinely hard part, not whether to cache.

A real interview question

What caching patterns do you use in front of a database, and how do you keep the cache consistent?

What most people say

drag me

I would put Redis in front of the database to cache query results.

It names a tool but no pattern and ignores the actual hard problem: invalidation and consistency. Caching reads is easy; keeping the cache correct on writes is what an interviewer is probing.

The follow-ups they ask next

  • Why is cache invalidation considered one of the hard problems?

    On every write you must reliably update or evict the right cached entries, across multiple keys and nodes, or readers get stale data. Missed invalidations cause subtle, hard-to-reproduce staleness bugs; TTLs bound the damage but do not eliminate it.

  • When would you choose write-through over cache-aside?

    When you need the cache and DB to stay tightly consistent and can accept slower writes, the cache is updated on every write so reads never miss freshly written data, avoiding the cache-aside staleness window.

What the interviewer is listening for

  • Names cache-aside vs write-through/behind
  • Focuses on invalidation/consistency as the hard part
  • Uses TTL + per-data-type staleness tolerance

What sinks the answer

  • Only "add Redis"
  • Ignores invalidation/consistency
  • No TTL or staleness reasoning

If you genuinely do not know

Say this instead of freezing. Reasoning out loud from what you do know beats silence every single time, and a good interviewer is listening for exactly that.

Default [cache-aside: check cache, on miss read DB and populate]. Alternatives [write-through (write both, consistent, slower) / write-behind (async, fast, risk of loss)]. The hard part is [invalidation: on a write, invalidate or update the entry, with a TTL as a self-healing safety net]. Caching [trades consistency for performance], so [set staleness tolerance per data type].

Keep going with databases

All 336 cloud engineer questions

Knowing the answer is not the same as recalling it under pressure

Sign in to send the questions you fumble to spaced recall, so they come back right before you would forget them, and learn the concepts behind them with hands-on labs.

Start free