tezvyn:

Cache-aside pattern pros and cons

AI-drafted, machine-checkedSource: interviewintermediate
WHAT IT TESTS

knowing lazy-loading caching and its consistency cost.

OUTLINE

app reads cache, on miss loads DB and populates, invalidates on write; pros are resilience and lean cache, cons are stale windows and app-managed invalidation.

WHAT THIS TESTS The interviewer wants confirmation that you know the dominant caching pattern, where the application orchestrates loading, and can articulate its consistency and operational tradeoffs.

A GOOD ANSWER COVERS In cache-aside, also called lazy loading, the application is responsible for the cache. On a read it first checks Redis; a hit returns immediately, a miss triggers a database read, after which the application stores the value in Redis with a time-to-live and returns it. On a write the application updates the database and then invalidates, or sometimes updates, the corresponding cache key so subsequent reads repopulate fresh data.

PROS Only data that is actually requested gets cached, keeping memory usage efficient. The cache is not on the critical write path, so the system degrades gracefully: if Redis is unavailable, reads simply fall through to the database. It is decoupled from the data store, so it works with any backend.

CONS Consistency is eventual. Between the database write and the cache invalidation, or within a TTL, reads can return stale data, and a missed invalidation leaves a key wrong until it expires. The application carries invalidation complexity and bugs there cause subtle staleness. On a hot key expiring under load, many concurrent misses stampede the database, the cache-stampede problem, mitigated with locks, request coalescing, or jittered TTLs.

LIKELY FOLLOW-UPS How does it differ from write-through and write-back. How do you size TTLs. How do you prevent stampedes. What happens during a Redis failover.

ONE CONCRETE EXAMPLE A user-profile service caches user:42 in Redis with a ten-minute TTL. The first request misses and loads from Postgres; later requests hit the cache. When the user updates their email, the service writes Postgres and deletes user:42, so the next read fetches and recaches the new value.

Read the original → learn.microsoft.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.