Cache-aside pattern pros and cons
knowing lazy-loading caching and its consistency cost.
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's really being asked
The interviewer wants confirmation that you know the dominant caching pattern, where the application orchestrates loading, and can articulate its consistency and operational tradeoffs.
The full answer
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.
What usually comes next
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.
A 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.
Interview question
A key advantage of cache-aside over write-through is that it:
- a.Guarantees the cache and database are always perfectly consistent
- b.Keeps the cache off the write path, so reads survive a cache outage by falling back to the databaseCorrect
- c.Caches the entire dataset proactively before any request arrives
- d.Eliminates the need for the application to manage cache keys
Why? this is the answer
Cache-aside caches lazily and isn't on the write path, so a cache outage degrades gracefully to database reads. It does not guarantee perfect consistency, and the application still manages keys and invalidation.
Just read this? Test yourself on what you have been reading.
Read the original → learn.microsoft.com
- #caching
- #redis
- #cache-aside
- #consistency
- #patterns
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on caching — each one lists the topics its interview covers.
See open roles