Compare

Cache-aside vs Write-through

Cache-asideThe app fills the cache on a miss

Delivery in10 minutes

Home · Indiranagar, BLR-07

Dairy

Fresh milk

500 ml

₹32
App servercache, then DB
Cache · Redis≈1 ms
  • price:milk₹32
  • price:bread₹52
  • price:eggs₹100
  • price:butter₹56
Database≈20 ms
  • milk · price₹32
  • stock50,000 products
Write-throughEvery write goes through the cache

Delivery in10 minutes

Home · Indiranagar, BLR-07

Dairy

Fresh milk

500 ml

₹32
App servervia the cache
Cache · Redis≈1 ms
  • price:milk₹32
  • price:bread₹52
  • price:eggs₹100
  • price:butter₹56
Database≈20 ms
  • milk · price₹32
  • stock50,000 products

One Zepto price in a cache in front of the database: the price changes, a thousand shoppers open it, then an overnight restock. Illustrative times.

Cache-aside or Write-through?

Cache-aside is the usual default: it caches only what’s read, and if the cache fails, the app still reads the database. Write-through when data is read right after it’s written, and must never be stale. Many systems mix both.

DifferenceCache-asideWrite-through
Who fills the cacheThe app, when a read missesEvery write
After a writeThe key is deleted; the next read misses onceThe cache holds the new value
The cache holdsOnly what’s readEverything written, read or not
If the cache failsReads fall back to the databaseWrites stall: the cache is in their path
Use it forMost read-heavy dataData read right after it’s written

Cache-aside vs Write-through, explained step by step

1. The price changes

Zepto shows every price from a cache: a fast copy in memory, in front of the database. Reading the cache takes about a millisecond; the database, about twenty.

On the left, cache-aside: the app fills the cache itself, only when a read misses. On the right, write-through: every write goes through the cache, into the database. Let’s follow one product, milk, through three moments.

First, milk drops from thirty-two rupees to twenty-eight.

The app writes the new price to the database, then deletes milk from the cache, so nobody is shown the old price.

With write-through, the new price goes to the cache, and the cache writes it to the database before saying done.

The cache already holds twenty-eight.

2. Shoppers open milk

Next, a thousand shoppers open milk.

The first read misses: milk isn’t in the cache, so the app reads the database, twenty milliseconds, and puts it back.

Everyone after reads it from the cache in a millisecond.

With write-through, milk is already in the cache with its new price, so every read hits, even the first.

3. The overnight restock

Finally, overnight, the warehouse updates the stock of fifty thousand products.

Cache-aside writes them to the database and only clears their keys, so the cache keeps just what shoppers actually read, like milk.

With write-through, all fifty thousand go through the cache.

Most of them nobody opens tonight, and they push out what shoppers do read.

In the morning, milk misses again, and the first shopper waits for the database.

In short

So which should you pick? Cache-aside is the usual default: it caches only what’s read, and if the cache fails, the app still reads the database. Pick write-through when data is read right after it’s written, and must never be stale. Many systems mix both.