GuidesCaching strategies
Caching strategies for system design: cache-aside, write-through and more
10 min read · Updated 1 December 2026
Thousands of people open the same product page every minute. Each time, your app asks the database for the same product, and the database does the same work over and over.
What if the app kept the answer for a while and reused it? That copy is a cache: a small, fast store of data you read often, so you don't fetch it from the slow place every time. Think of the documents you use most, kept on your desk instead of in the record room down the hall.
Adding a cache is easy. The interview questions are about the details: who fills the cache, what happens on a write, how long a copy may stay, and what breaks when a popular entry disappears. This guide covers each of those, with the terms interviewers expect.
Key takeaways
- A cache read from memory takes about a millisecond over the network; a database query often takes several to tens.
- Cache-aside is the usual default: the app reads the cache, falls back to the database, and fills the cache on a miss.
- On a write, update the database and then delete the cache key, with a TTL as a safety net.
- Stampedes happen when a hot key expires; fix them by letting only one request refill it.
- Cache at every layer that helps: browser, CDN, app and database each save a different trip.
Why a cache helps
Let's put numbers on it. These are rough, but the gaps between them are what matter:
- Reading from the app server's own memory: well under a microsecond.
- Reading from a cache server like Redis or Memcached in the same data centre: often around half a millisecond to a millisecond, mostly network time.
- A database query that hits an index: often a few milliseconds; one that joins tables or reads from disk can take tens of milliseconds.
Say your database handles 5,000 queries a second comfortably, and traffic grows to 50,000 reads a second. If a cache answers 90% of those, the database sees only 5,000. That share of reads served from the cache is called the hit rate, and it is the number that decides whether a cache is worth having.
Caches work best when the same data is read many times, it changes rarely, and slightly old data is acceptable for a short while. A product page, a user profile or a feed of trending posts fit well. A bank balance shown right before a transfer does not.
Reading: cache-aside and read-through
There are two common ways to fill a cache on reads.
Cache-aside (also called lazy loading). The app does the work:
- 1.Look for the key in the cache.
- 2.On a hit, return it.
- 3.On a miss, read the database, write the result into the cache with an expiry time, then return it.
value = cache.get(key)if value is null: value = db.query(key) cache.set(key, value, ttl = 300)return valueOnly data someone actually asked for ends up in the cache, and if the cache goes down the app can still read the database. This is the default in most designs and the one to describe first in an interview.
Read-through. The same logic, but it lives in the cache layer instead of your code. The app only ever asks the cache, and the cache loads from the database on a miss. Code is tidier, but you need a cache library or product that supports a loader, and the cache now depends on the database schema.
Writing: write-through, write-behind and write-around
Reads are the easy half. The question that trips people up is: what happens to the cached copy when the data changes?
Write-through. Every write goes to the cache and the database together, and is only confirmed when both succeed. The cache is always fresh, but every write is slower, and you may fill the cache with data nobody reads.
Write-behind (also called write-back). The write goes to the cache, is confirmed at once, and is copied to the database a little later, often in batches. Writes are very fast. The cost is real: if the cache node dies before copying, those writes are gone. Use it only for data you can afford to lose or rebuild, like view counters.
Write-around. Writes go straight to the database and skip the cache. The cache only fills on reads. This suits data that is written often but rarely read soon after, such as logs.
In practice, cache-aside reads are usually paired with "update the database, then delete the cache key". The next read misses and loads the fresh value. Deleting is safer than writing the new value into the cache, because two writes racing to set the cache can leave the older one there.
| Strategy | How writes work | Good at | Watch out for |
|---|---|---|---|
| Cache-aside | App writes DB, then deletes the cache key | Simple; cache failure is not fatal | First read after a write is a miss |
| Read-through | Cache loads from DB on a miss | Tidy app code | Needs cache support for loaders |
| Write-through | Cache and DB updated together | Cache always fresh | Slower writes; caches unread data |
| Write-behind | Cache first, DB later in batches | Very fast writes | Data loss if the cache dies |
| Write-around | DB only; cache fills on reads | Avoids caching write-only data | Recent writes always miss |
TTLs, invalidation and eviction
Time to live (TTL) is how long an entry may stay before it expires. Short TTLs (seconds) keep data fresh but lower the hit rate. Long TTLs (hours) raise the hit rate but let stale data linger. Pick by asking "how wrong can this be, and for how long?" A product price might get 60 seconds; a country list might get a day.
Invalidation means removing an entry because the data changed. Deleting on write handles most cases, but there is a small race: a slow reader can fetch the old value from the database just before a write, then put it into the cache just after the delete. A TTL is your safety net, since even a wrong entry expires. For tighter guarantees, some systems listen to the database's change log and delete keys from there.
Eviction is what the cache does when memory is full. It must throw something away:
- LRU (least recently used) drops the entry nobody has touched for the longest time. It is the common default.
- LFU (least frequently used) drops the entry with the fewest reads. It is better when a few keys stay popular for a long time and you don't want a one-off scan to push them out.
Unlike a real desk, a cache can throw a document away at any moment. Your code must always be ready for a miss.
Cache-aside step by step, and the stampede
Here is the full life of one popular key, including the failure that catches most designs out.
Picture a product page read 5,000 times a second. The cache answers all of them. Then its TTL runs out. In the next few milliseconds, all 5,000 requests miss at once, and all 5,000 run the same database query. The database, sized for far less, slows to a crawl. This is a cache stampede, also called the thundering herd.
Fixes, from simplest:
- One refill at a time. The first request that misses takes a short lock (or a "someone is loading this" marker) and loads the value. Others wait a few milliseconds and read the cache, or get the old value. This is often called request coalescing.
- Serve stale while refreshing. Keep the old value a little past its TTL and refresh it in the background.
- Refresh early. Reload popular keys shortly before they expire, so they never actually go missing.
- Add jitter to TTLs. If you cache 10,000 items at the same moment with the same TTL, they all expire together. A random few seconds on each TTL spreads them out.
Read miss
A user opens product 42. The app asks the cache, which has nothing for that key yet.
Hot keys and what to cache where
Hot keys. A distributed cache splits keys across nodes (see consistent hashing). One key always lives on one node. If a celebrity's profile gets 200,000 reads a second, that one node takes all of it while the others sit idle. Two common fixes:
- Keep a tiny in-process cache on each app server for the hottest keys, with a TTL of a few seconds.
- Store several copies under different names (
profile:7:a,profile:7:b…) on different nodes, and have each read pick one at random.
What to cache where. Caches exist at every layer, and each saves a different trip:
- Browser: static files and some API responses, controlled with HTTP cache headers. Saves the whole network round trip.
- CDN (content delivery network, servers spread around the world): images, scripts, video and public pages. Serves users from a nearby city instead of your data centre.
- App layer: Redis or Memcached for query results, sessions and computed views. This is where most interview designs put their cache.
- Database: the database keeps recently used data pages in memory on its own. Read replicas also act as a kind of cache for reads.
The closer to the user, the faster, but the harder to invalidate. A wrong value in a million browsers cannot be deleted; it can only expire.
Trade-offs to say out loud
- Freshness vs hit rate: longer TTLs mean fewer database reads and staler data.
- Cache-aside vs write-through: cache-aside is simple and survives a cache outage; write-through keeps the cache fresh at the cost of slower writes.
- Write-behind: the fastest writes, paid for with possible data loss.
- Delete vs update on write: deleting avoids racing writers, at the cost of one extra miss.
- Cache outage: a design that relies on a 95% hit rate sends 20× the load to the database when the cache disappears. Say how the database survives that, for example with replicas, rate limits or a warm standby cache.
See these ideas used in a full design in design a URL shortener.
Questions people ask
What is the difference between cache-aside and write-through?
Cache-aside fills the cache only when a read misses, and the app usually deletes the cache key on a write. Write-through updates the cache and the database together on every write, so the cache is always fresh but writes are slower. Many systems use cache-aside for reads with delete-on-write.
Which caching strategy is best?
Cache-aside is the most common default because it is simple and the app still works when the cache is down. Use write-through when reads right after writes must be fresh, and write-behind only for data you can afford to lose, such as counters.
What is a cache stampede?
It happens when a popular key expires and many requests miss at the same moment, all querying the database together. Fix it by letting only one request reload the value while the others wait or get the old copy, and by adding a little randomness to TTLs.
What is the difference between LRU and LFU eviction?
LRU removes the entry that has gone the longest without being read. LFU removes the entry that has been read the fewest times. LRU is the common default; LFU protects long-term popular keys from being pushed out by a burst of one-off reads.
Should I update or delete the cache when data changes?
Usually delete. Update the database first, then delete the key, and let the next read load the fresh value. Writing the new value directly can leave an older value in the cache when two writes race.