midcaching
The application checks the cache -> on miss, reads from the DB -> writes to the cache.
The most popular caching pattern. Read: check cache -> cache hit -> return. Cache miss -> read DB -> write to cache -> return. Write: update DB -> invalidate cache. Lazy loading: only requested data is cached. Stale data is possible between updating the DB and invalidating the cache.
// Cache-Aside pseudo-code
async function getData(key) {
let data = await redis.get(key);
if (!data) {
data = await db.query(key);
await redis.set(key, data, 'EX', 3600);
}
return data;
}When yes
Read-heavy workloads, data changes infrequently, tolerance for eventual consistency
When no
Data changes frequently and strict consistency is required
Interview tip
This is the default pattern — if not specified otherwise, use Cache-Aside
Alt:Write-ThroughWrite-BehindRead-Through