Caching Strategies with Redis
Caching keeps frequently read data in fast memory so you do not hit the database every time. Interviewers ask about cache-aside, write-through, TTLs, invalidation and the cache stampede problem.
What you will learn
- Know the cache layers from browser to database
- Implement cache-aside with TTLs
- Compare write-through, write-behind and cache-aside
- Prevent stampedes, penetration and stale data
Key concepts
Cache-aside
The app reads the cache; on a miss it reads the database and writes the result to the cache with a TTL. On updates it deletes the key. The most common pattern.
Write-through
Writes go to the cache and the database together, so the cache is always fresh but every write is slower.
Write-behind
Writes go to the cache and are flushed to the database later. Fast, but data can be lost if the cache dies before the flush.
Eviction
When memory is full Redis evicts keys by policy, such as allkeys-lru (least recently used) or volatile-ttl.
Step by step
Three classic problems
- Stampede: a hot key expires and 1,000 requests hit the database at once → a lock so one request rebuilds, or refresh early with jittered TTLs
- Penetration: requests for ids that do not exist always miss → cache the "not found" briefly, or use a Bloom filter
- Stale data: update the database then delete the cache key; use short TTLs as a safety net
Examples
Cache-aside with stampede protection
async function getProduct(id) {
const key = 'product:' + id
const hit = await redis.get(key)
if (hit) return JSON.parse(hit)
const lock = await redis.set(key + ':lock', '1', { NX: true, EX: 5 })
if (!lock) { await sleep(50); return getProduct(id) } // someone else is rebuilding
try {
const product = await db.product.findUnique({ where: { id } })
const ttl = 300 + Math.floor(Math.random() * 60) // jitter
await redis.set(key, JSON.stringify(product ?? null), { EX: product ? ttl : 30 })
return product
} finally {
await redis.del(key + ':lock')
}
}
async function updateProduct(id, data) {
const p = await db.product.update({ where: { id }, data })
await redis.del('product:' + id) // invalidate after the write
return p
}Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Decide what to cache for a product page and the TTL for each piece.
- Explain why "update cache, then database" can leave the cache wrong.
- Use Redis sorted sets to build a top-10 leaderboard.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
What is Redis used for besides caching?
Sessions, rate limits, distributed locks, queues (BullMQ, Streams), pub/sub for WebSockets, leaderboards with sorted sets, counters, and de-duplication with sets.
Why is cache invalidation hard?
Data is copied to places that do not know when the source changes, especially with several caches, concurrent writes and derived data (lists that contain the item). You trade freshness for speed and choose how stale each piece may be.
Summary
Cache-aside with TTLs is the default; delete keys after writes, add jitter and locks against stampedes, cache negative results against penetration, and choose eviction policies on purpose.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus