The application owns the miss path#
Cache-aside is a common read pattern: check the cache, read the source on a miss, then populate the cache. The database remains the source of truth and only requested values enter the cache.
value = cache.get("user:42")
if value is missing:
value = database.read_user(42)
cache.set("user:42", value, ttl)
return valueWrites need a separate decision#
Change Maya to May
1. UPDATE users SET name = 'May' WHERE id = 42
2. DELETE cache key user:42
3. Next read reloads the committed valueUpdating the source and then deleting the cached copy is often simpler than updating every cached representation. It still has a failure window: the database can commit while deletion fails. No ordering is universally safe; state the consistency requirement and recovery mechanism.
Use these prompts to extend the design. The useful answer is a reasoned change, not a memorized sentence.
- What happens if two requests miss simultaneously?
- What if the database update succeeds but cache deletion fails?
- Should a not-found result be cached?
- Can callers bypass the cache for a read-after-write?
Related practice problems
Use the manifest-backed prompts below to apply this lesson in a complete design.
- Caching challenges and strategiesAmazon Builders' Library