Design building blocks · Trade-off
Cache-aside, write-through or write-back
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
Our product catalogue API is slow, and someone says 'just add Redis'. Which caching strategy would you use: cache-aside, write-through or write-back? And how do you keep the cache from showing old prices?
Then
Follow-ups the interviewer asks next
- Walk me through a read and a write with cache-aside.
- When is write-through or write-back the better choice?
- What TTL would you give a price, and a product description?
- A price changes. How does the cache find out?
- On an update, do you write the database first or delete the cache key first, and what race is left?
- What gets evicted when memory is full?
- Redis restarts and the cache is empty. What happens to the database?
- Where else can you cache besides Redis?
The answer
What a strong answer covers
- Cache-aside: read the cache; on a miss, read the database and set the key with a TTL. On update, write the database, then delete the key. Simple and the most common.
- Write-through: write the cache and the database together; reads are always warm, writes are slower. Write-back (write-behind): write the cache, flush to the database later; fast, but data can be lost, so fine for view counts and wrong for orders or money.
- TTL by data: long for descriptions and images; seconds, or event-based invalidation, for price and stock.
- Invalidation: change events from the database (CDC with Debezium into Kafka) delete keys; or versioned keys.
- The race: after "write, then delete", a slow reader can still put an old value back; a short TTL as a safety net, a delayed second delete, or version checks reduce it.
- Eviction: LRU or LFU with a memory limit; watch for hot keys.
- Cold start: request coalescing (one database call per key), warm-up, and jitter on TTLs.
- Layers: browser and CDN, gateway, in-process cache, a shared cache (Redis, or Valkey, its BSD-licensed fork; Redis 8 itself is AGPLv3 since May 2025), and the database's own buffer pool.
The red pen
Common traps
Avoid:caching with no TTL and no invalidation; write-back for payments; updating the cache before the database.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.