Full answer
Why cache at all
A cache is a fast store (usually memory, such as Redis or Memcached) that keeps copies of data that is expensive to fetch. Reading a key from memory over the network typically takes well under a millisecond, while a database query with joins can take tens of milliseconds. A cache also takes read load off the database.
Read strategies
Cache-aside (lazy loading). The application owns the logic:
def get_student(student_id):
key = f"student:{student_id}"
data = cache.get(key)
if data is None: # miss
data = db.fetch_student(student_id)
cache.set(key, data, ttl=300) # keep for 5 minutes
return dataOnly data that is actually requested ends up cached. If the cache goes down, the app still works (slower). This is the most common pattern.
Read-through. Same flow, but the cache library or service loads from the database itself. Application code is simpler; you need a cache that supports loaders.
Write strategies
- Write-through: every write goes to the cache and the database before success is returned. Reads are fresh, writes are slower, and you may cache data nobody reads.
- Write-back (write-behind): write to the cache, return, and flush to the database later in batches. Very fast writes, but data is lost if the cache node fails before flushing. Suits counters and metrics, not marks or payments.
- Write-around: write only to the database and let the next read populate the cache (with cache-aside). Avoids filling the cache with write-once data like logs.
With cache-aside, the usual write path is "update the database, then delete the cache key". Deleting is safer than updating the cache, because two concurrent updates could otherwise leave the older value in the cache.
Choosing
| Need | Strategy |
|---|---|
| Read-heavy, tolerate seconds of staleness | Cache-aside with TTL |
| Reads must be fresh right after writes | Write-through, or delete-on-write |
| Very high write rate, some loss acceptable | Write-back |
| Data written once, rarely read | Write-around |
The code above is illustrative pseudocode using a generic cache and db object; it was not run.
By level
Fresher: Explain cache-aside step by step with a small example and describe write-through and write-back in one or two sentences each. The read path should be explained in order: check cache, miss, load, store with TTL.
Mid: Compare all strategies on freshness, latency and durability, explain delete-on-write and TTL choice, and estimate hit ratio impact on database load. Expect the reason deleting a key on write is safer than updating it.
Senior: Discuss race conditions between concurrent writers and cache fills, versioned keys, change-data-capture driven invalidation, cache failure modes and how much database headroom is needed if the cache disappears. It should also cover what happens to the database if the cache cluster is lost.
Key points
- Cache-aside: app checks cache, loads from DB on miss, sets with TTL
- Write-through keeps cache fresh at the cost of write latency
- Write-back is fastest but can lose data
- Write-around avoids polluting the cache with write-once data
- Prefer delete-on-write over update-on-write for cache-aside
Common mistakes
- Using write-back for data that cannot be lost
- Updating the cache on write and creating race conditions with stale values
- Caching without a TTL and never expiring wrong data
- Assuming the cache is a source of truth
Follow-ups
- What race can leave stale data in cache-aside, and how do you narrow it?
- How would you warm a cold cache after a restart?
- What should happen to reads if the cache cluster is down?
Tips
- Draw the read path and the write path separately
- Tie each strategy to a type of data
- Mention TTL as a safety net with any strategy
Evaluation rubric
- Correctly describes read and write strategies
- Names freshness, latency and durability trade-offs
- Explains delete-on-write reasoning
- Matches strategies to sensible workloads