Delivery in10 minutes
Home · Indiranagar, BLR-07
Dairy
Fresh milk
500 ml
price:milk₹32price:bread₹52price:eggs₹100price:butter₹56
milk · price₹32stock50,000 products
Delivery in10 minutes
Home · Indiranagar, BLR-07
Dairy
Fresh milk
500 ml
price:milk₹32price:bread₹52price:eggs₹100price:butter₹56milk · price₹32stock50,000 productsDelivery in10 minutes
Home · Indiranagar, BLR-07
Dairy
Fresh milk
500 ml
price:milk₹32price:bread₹52price:eggs₹100price:butter₹56milk · price₹32stock50,000 productsOne 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 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.
| Difference | Cache-aside | Write-through |
|---|---|---|
| Who fills the cache | The app, when a read misses | Every write |
| After a write | The key is deleted; the next read misses once | The cache holds the new value |
| The cache holds | Only what’s read | Everything written, read or not |
| If the cache fails | Reads fall back to the database | Writes stall: the cache is in their path |
| Use it for | Most read-heavy data | Data read right after it’s written |
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.
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.
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.
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.