Caching, step by step
A cache makes a slow site fast in an afternoon, and then shows the wrong price. Watch a shop’s database drown, add a cache, and learn to keep it fresh and safe.
3 steps · about 5 minutes · free, no sign-up
Step 1: The sale starts
8,000 page views a second, mostly the same 50 products: the database is drowning.
Thousands of shoppers open the same few product pages, and every view queries the database. Pages take 6 seconds. What do you do?
- Add more app servers (Not this one): The app servers weren’t the problem. More of them just send the database even more queries.
- Check a cache first; on a miss, read the database and save it there (Right fix): The first view of a product reads the database and stores the result in the cache. The next thousand views come straight from memory. Cache-aside: The app looks in the cache first. On a miss it reads the database and puts the answer in the cache for next time. It is the most common way to cache, because the app stays in control.
- Add indexes to the database (Helps for a while): Indexes make each query cheaper, so it copes for now. But it still answers the same question thousands of times a second, and the next traffic spike swamps it.
Step 2: The wrong price
The team cut a price in the database, but the cache still shows the old one.
The cache keeps serving the old price after it changed. Shoppers are charged a different price from the one they saw. What do you do?
- Delete the cached item whenever its price changes (Right fix): Saving a new price also deletes that product from the cache. The next view misses, reads the new price and caches it. A TTL (say, 1 hour) catches anything you forget to delete. Cache invalidation + TTL: Invalidation removes a cached item when its data changes, so readers get fresh data. A TTL (time to live) makes every item expire after a while anyway, as a safety net.
- Turn the cache off (Not this one): Prices are right again, but every view hits the database and the site is back to 6-second pages.
- Expire everything after 10 seconds (Helps for a while): Prices are at most 10 seconds old, which is often good enough. But items expire constantly, so many more views reach the database, and 10 seconds can still be wrong at checkout.
Step 3: A stampede
The top product’s cache entry expires, and 5,000 requests rebuild it at the same moment.
When a very popular item expires, thousands of requests miss together and all hit the database with the same query. It falls over for a few seconds. What do you do?
- Let one request rebuild it while the rest wait or get the old copy (Right fix): The first request takes a short lock and refreshes the item; everyone else briefly gets the old copy or waits a few milliseconds. The database sees one query, not five thousand. Cache stampede protection: Only one request refreshes an expired item (a lock or "single flight"), or items are refreshed a little before they expire. Either way, a popular expiry doesn’t become a flood.
- Make the cache bigger (Not this one): Size wasn’t the problem: the item expired on purpose. A bigger cache still has thousands of requests miss at the same moment.
- Never expire the popular items (Helps for a while): No expiry, no stampede. But those items can now go stale for good unless every change is invalidated perfectly, which is the problem the TTL was protecting you from.
What you built
- Cache-aside: Read the cache first; on a miss, read the database and store the answer.
- Invalidation: Delete the cached item when its data changes, so readers see the new value.
- TTL: Every item expires after a while, so forgotten items can’t stay wrong forever.
- Stampede protection: Only one request rebuilds an expired item, so the database sees one query.
| Problem | Fix | Trade-off |
|---|---|---|
| Same reads over and over | Cache-aside | First read of each item is still slow |
| Stale data after a change | Invalidate + TTL | Every write path must invalidate |
| Popular item expires | One rebuild at a time | A few requests briefly see old data |
- Lesson: from 1 user to 1 million
- Lesson: database sharding and replication
- Read: caching strategies explained
Frequently asked questions
What is cache-aside?
A caching pattern where the app checks the cache first, and on a miss reads the database and stores the result in the cache. It is the most common pattern with Redis or Memcached.
What is a TTL in caching?
Time to live: how long an item may stay in the cache before it expires. Short TTLs keep data fresher but send more reads to the database.
Why is cache invalidation hard?
Every place that changes data must also remove or update the cached copy, and a missed one leaves stale data. Teams combine invalidation with a TTL so mistakes fix themselves.
What is a cache stampede?
When a popular cached item expires and many requests miss at once, all querying the database for the same thing. Locks, early refresh or serving the old copy prevent it.