Engineering concepts

Cache Really Matters

What a cache is, where it shows up, and how write-through, write-back, and write-around trade speed, consistency, and data-loss risk.

By Pawan Bisht

A cache is a small, fast storage layer that keeps a copy of frequently or recently used data, so future requests for that data can be served faster than fetching it from the original (slower) source.

Where caches show up?

  • CPU cache (L1/L2/L3) — stores data close to the processor so it doesn't have to fetch from RAM every time.
  • Browser cache — stores web page assets (images, CSS, JS) locally so a page loads faster on repeat visits.
  • DNS cache — stores resolved domain-to-IP mappings so lookups aren't repeated.
  • Database/query cache — stores results of expensive queries.
  • CDN (Content Delivery Network) — caches content geographically closer to users.
  • Application-level cache (e.g., Redis, Memcached) — stores computed results or session data in memory.

Cache Write Policies

Write-Through

Data is written to both the cache and the main database at the same time (synchronously), before the write is considered complete.

  • App writes data
  • Data is written to the cache
  • Data is immediately also written to the database.
  • Only after both succeed does the app get a write successful response.

Pros

  • Cache and database are always in sync - strong consistency
  • No data loss risk, since the database is updated immediately

Cons

  • Slower writes, since every write waits on the (slower) database too.
  • The cache gets filled with data that may never be read again, wasting cache space.

Write-Back

Data is written only to the cache first, and the database is updated later, asynchronously — usually in a batch, or after a delay.

  • App writes data to the cache.
  • App immediately gets a success response — it doesn't wait for the database.
  • In the background, the cache periodically flushes changes to the database.
  • Multiple writes can be grouped together and written to the database in a batch.

Pros

  • Very fast writes, since the app doesn't wait on the slower database.
  • Reduces database load by batching multiple writes together.
  • Works well for high-throughput workloads.

Cons

  • Risk of data loss if the cache crashes before the async flush happens.
  • Cache and database can temporarily be out of sync.
  • More complex to implement because it needs a reliable flush or synchronization mechanism.

Write-Around

Data is written directly to the database, bypassing the cache entirely. The cache is updated later when that data is read.

  • App writes data directly to the database.
  • The cache is not updated at write time.
  • When the data is requested later, the cache is checked first.
  • If the data is not in the cache, it results in a cache miss.
  • The data is fetched from the database and then stored in the cache.

Pros

  • Avoids filling the cache with data that might not be read soon.
  • Reduces cache pollution from write-heavy workloads.
  • Keeps the cache focused on data that is actually being read.

Cons

  • The first read after a write is usually a cache miss and is therefore slower.
  • Cache can briefly serve stale data if something else already cached the old value.
  • The cache is not populated at write time.

#cache, #write-back, #write-through, #write-around