skip to content

In caching, compare the cache-aside, write-through, and write-behind (write-back) strategies for keeping a cache and its backing database in sync: describe how each handles reads and writes, and the consistency/durability trade-off of each.

level: middleimportance: must knowfreq 80%

answer

  1. cache-aside: app fills cache on miss, write DB then invalidate
  2. write-through: sync write to both, no staleness
  3. write-behind: write cache first, flush DB later async, durability risk
  4. cache-aside cache crash = safe, just cold
  5. write-behind crash = data loss window

basics

~20 s

Cache-aside: the app checks the cache first, and only saves to it after reading from the database, while writes go straight to the database and the cache entry is cleared. Write-through: every write goes to the cache and the database together, right away. Write-behind: writes go to the cache immediately but reach the database later, in the background.

solid answer

~50 s

Cache-aside (lazy loading) has the application check the cache on read; on a miss it reads the database and populates the cache, while writes go directly to the database and the corresponding cache entry is invalidated or updated separately — simple and cache-agnostic, but leaves a window where the cache can be stale or a write can race a read-repopulation. Write-through writes to the cache and database synchronously as part of the same write operation, so the cache is never stale relative to committed writes, at the cost of added write latency (two synchronous writes) and cache churn for data that's rarely read. Write-behind (write-back) writes to the cache immediately and acknowledges the client, then asynchronously flushes to the database later, batching or coalescing writes for much lower write latency and higher throughput — at the cost of a durability window: if the cache node crashes before the flush, those writes are lost, and readers of the database directly (bypassing the cache) can see stale data.

go deeper

for a junior

Should describe the basic read/write flow of cache-aside (check cache, fall back to DB, populate) as the most common pattern.

for a middle

Should correctly describe all three strategies' read and write paths and name the latency vs. consistency trade-off in each.

for a senior

Should articulate the specific staleness race in cache-aside, the atomicity concern in write-through, and the durability window in write-behind, and match each to an appropriate real workload.

for a principal

Should choose and justify a write strategy (or hybrid) for a given system's SLAs, including how to bound the write-behind durability window (e.g., persistent/replicated cache, write-ahead log) and how to detect and repair drift between cache and database.

## Why writes need a policy at all Keeping a cache and its backing database consistent requires a policy for what happens on both reads and writes, because a cache is fundamentally a second copy of data that can drift from the source of truth. The three classic strategies — **cache-aside**, **write-through**, and **write-behind** — differ mainly in when and how writes touch the cache, and each makes a different trade between write latency, consistency, and durability risk. ## Cache-aside (lazy loading) Cache-aside, also called lazy loading, is the most common pattern in practice and puts the application (not the cache infrastructure) in charge of populating the cache. - **On a read**, the application checks the cache first; on a hit, it returns the cached value directly. - **On a miss**, the application queries the database itself, returns the result to the caller, and — as a separate step — writes that result into the cache so the next read is a hit. - **On a write**, the application writes to the database and then either deletes the corresponding cache entry (invalidate) or updates it directly; deleting is generally preferred because it avoids a subtle race where a slow read that started before the write repopulates the cache with data that's already stale by the time it writes it back. Crucially, the cache is completely passive here: it doesn't know how to load data on its own, and it starts empty for any key until something reads it. Cache-aside's strength is simplicity and resilience: because the database is always the durable source of truth for writes, a cache crash never loses data — it just means more cache misses (and therefore more database load) until the cache warms back up. Its weakness is a staleness window: between the database write and the cache-delete (or between an in-flight stale read repopulating the cache), readers can briefly see old data, and under high write concurrency this window can widen. ## Write-through Write-through makes the cache an active participant in every write: the application (or a caching layer that wraps the database) writes to the cache and the database together as part of a single logical write operation, and the write is only considered complete once both succeed. This guarantees the cache is never behind a committed write — any subsequent read is guaranteed fresh, because the cache and database are updated atomically from the caller's perspective. The cost is **write latency**: every write now pays for two synchronous operations (cache write plus database write) instead of one, and if the two can't be made truly atomic, there's a narrow window where one succeeds and the other fails, requiring compensating logic. Write-through also caches everything that's written, even data that's rarely or never subsequently read, which can waste cache memory on cold data — a mismatch it shares with any policy that writes to cache eagerly rather than lazily on first read. ## Write-behind (write-back) Write-behind (also called write-back) also writes to the cache first, but instead of synchronously propagating to the database, it acknowledges the write to the caller immediately and flushes to the database asynchronously later — often batching multiple writes to the same key into a single database write, or coalescing many writes over a short window to reduce database load dramatically. This gives the lowest possible write latency from the caller's perspective and the highest write throughput, since the database is shielded from the full write rate and can be updated in efficient batches. The trade-off is a **durability and consistency risk**: there's a window during which the authoritative copy of the data lives only in the cache and not yet in the database. If the cache node crashes, loses power, or is evicted before the flush completes, those writes are permanently lost unless the cache itself is made durable (e.g., persisted to disk, replicated). It also means any process reading directly from the database — bypassing the cache — can see stale or missing data during that window, so write-behind is only safe when all reads are guaranteed to go through the same cache. ## Choosing between them Choosing between them is a direct trade of write latency and cache-infrastructure complexity against consistency and durability guarantees. - **Cache-aside** is the default for most web applications and read-heavy workloads (e.g., a `Redis` cache in front of a `Postgres` database for a typical CRUD API) precisely because it's simple, database-is-truth, and cache failure is a performance problem rather than a data-loss problem. - **Write-through** suits systems that need read-after-write consistency guarantees and can tolerate the extra write latency — for example, a session store where a user's next request must always see their just-written session state. - **Write-behind** suits extremely write-heavy systems where database write capacity is the bottleneck and some risk of losing the most recent few writes on a crash is acceptable — for example, batching high-frequency metrics or activity-log writes, or systems like CPU/disk caches at the hardware level, which is where the write-back terminology originates.

  • In cache-aside, why is deleting the cache entry on write generally safer than updating it directly?
    Updating on write creates a race: if a concurrent read had already started fetching stale data from the database before the write committed, that read can finish and overwrite the cache with the old value right after the write's update, leaving the cache permanently stale until the next write. Deleting instead forces the next read to go back to the database and repopulate fresh, which is a smaller and self-correcting window.
  • Why is write-behind considered risky for systems where multiple services or the analytics pipeline read directly from the database?
    Write-behind's whole benefit comes from delaying the database write, which means there's a window where the cache holds the true value but the database doesn't yet. Any reader that bypasses the cache and queries the database directly during that window sees stale or missing data, so write-behind is only safe when every reader is guaranteed to go through the same cache.
  • What happens to a cache-aside system's correctness if the cache infrastructure crashes entirely and restarts empty?
    Correctness is preserved because the database remains the sole durable source of truth for all committed writes — an empty cache just means every read is temporarily a miss, so the system falls back to hitting the database directly for everything until the cache warms back up. It's a performance/latency degradation, not a data-loss or correctness incident.

Cache-aside is like checking your notebook for an answer, and only writing it down after you look it up in the textbook. Write-through is like updating both your notebook and the master file cabinet at the same moment, every time. Write-behind is like scribbling notes fast and promising yourself you'll file them in the cabinet later — great for speed, risky if you lose the notes before filing them.

saying these in an interview costs you the question

  • Thinks write-through and write-behind are the same thing
  • Doesn't identify the data-loss risk in write-behind
  • Assumes cache-aside guarantees no staleness window at all
  • Can't explain why deleting on write is safer than updating the cache directly
  • Believes a crashed cache in cache-aside causes data loss rather than just cold misses

context