# Caching strategies explained

Source: https://systemdesignsim.com/system-design/caching-strategies

System design · Cache-aside, write-through, stampedes

Use cache-aside with a TTL for most reads: check the cache, on a miss read the database and fill the cache. At 50,000 reads a second, a 90% hit ratio sends 5,000 reads a second to the database and a 99% hit ratio sends only 500. The interview starts after "put Redis in front of it".

Updated October 25, 2026 · 5 min read

[Survive a cache stampede](https://systemdesignsim.com/challenges/the-stampede)

![Caching Strategies architecture diagram](https://systemdesignsim.com/diagrams/caching-strategies-system-design-diagram.svg)

A read can be answered by the browser, a CDN edge or an in-process cache. On a miss the app checks a shared cache, then the database (cache-aside).

## The short version

* Default to cache-aside with a TTL. On a write, update the database and delete the key.
* The TTL bounds staleness. Pick it from how wrong the data can be, and for how long.
* Reason about the miss ratio. Going from 90% to 80% hits doubles database load.
* A hot key expiring causes a stampede. Coalesce rebuilds, jitter TTLs and refresh early.

## Clarifying questions to ask first

* How stale can this data be? (Assume a user profile can be up to five minutes old. That sets the TTL.)
* How do reads compare to writes? (Assume reads far outnumber writes.)
* How big is the hot set? (Assume about 20% of records get about 80% of reads.)
* Can recent writes be lost if a cache node fails? (Assume not, which rules out write-behind except for data like view counters.)
* Are some keys far hotter than others? (Assume yes, so plan for a stampede when one expires.)

## Why cache

A memory read takes well under a millisecond; a database query often takes several. Every cache hit is a read the database never sees. The price is two copies of the data that can disagree.

The browser obeys Cache-Control and can't be purged. A CDN holds static files and cacheable API responses near users. An in-process cache skips the network hop, but each server's copy drifts. A distributed cache (Redis, Valkey or Memcached) is shared by all app servers and is the usual interview answer.

## Read strategies

Cache-aside, or lazy loading, puts the application in charge. It checks the cache, and on a miss reads the database, writes the result to the cache and returns it. Only requested data gets cached, and if the cache goes down reads still work, just slower.

```
def get_user(user_id):
    key = f"user:{user_id}"
    cached = redis.get(key)
    if cached is not None:
        return deserialize(cached)
    user = db.query("SELECT * FROM users WHERE id = %s", user_id)
    redis.set(key, serialize(user), ex=300)
    return user

def update_user(user_id, fields):
    db.update("users", user_id, fields)
    redis.delete(f"user:{user_id}")
```

Read-through moves that logic into the cache layer, which loads from the database on a miss. Either way the first read of every key misses, so a cold cache sends a burst to the database.

## Write strategies

| Strategy                  | How it works                                            | Consistency                        | Write latency         | Main risk                                    |
| ------------------------- | ------------------------------------------------------- | ---------------------------------- | --------------------- | -------------------------------------------- |
| Write-through             | Write cache and database; return when both succeed      | Strong                             | Higher, two writes    | Caches data never read                       |
| Write-behind (write-back) | Write cache, return, flush to database later in batches | Eventual                           | Lowest                | Data loss if the cache fails before flushing |
| Write-around              | Write database only; cache fills on next read           | Stale until expiry or invalidation | Same as no cache      | Fresh writes always miss first               |
| Cache-aside with delete   | Write database, then delete the key                     | Short stale window                 | Write plus one delete | A race can re-cache an old value             |

Write-around suits data rarely read back, such as logs. Cache-aside with delete-on-write plus a TTL is the safe default.

## Expiry and eviction

Expiry removes data because it's old; eviction removes it because the cache is full. With a 60-second TTL nobody sees data older than 60 seconds, even if an invalidation was missed, which is why a TTL stays as a backstop behind deletes. Shorter TTLs trade hits for freshness.

LRU evicts the key unread for longest. LFU evicts the key with fewest reads, which protects steadily popular keys from a one-off scan of many cold keys.

## The stampede

When a popular key expires, every request misses and rebuilds the same value. At 5,000 reads a second and a 200 ms rebuild, up to 1,000 requests hit the database for one key.

* Coalesce: one request rebuilds behind a lock or single-flight helper while the others wait.
* Jitter TTLs, such as 300 seconds plus or minus 30, so keys written together don't expire together.
* Refresh ahead: reload hot keys in the background shortly before they expire, so readers never see the miss.
* If the rebuild is slow or fails, serve the stale value a little longer rather than an error.

Watch one happen in the [caching simulation](https://systemdesignsim.com/embed/caching-basics), then try to survive one in [the stampede challenge](https://systemdesignsim.com/challenges/the-stampede).

## Sizing and hit ratio

| Hit ratio | Share reaching database | Database reads at 50,000 reads/s |
| --------- | ----------------------- | -------------------------------- |
| 99%       | 1%                      | 500/s                            |
| 95%       | 5%                      | 2,500/s                          |
| 90%       | 10%                     | 5,000/s                          |
| 80%       | 20%                     | 10,000/s                         |

Dropping from 90% to 80% doubles database load; 99% to 95% multiplies it by five. Size from the hot data: with 100 million records at 1 KB, the full dataset is 100 GB, and the hot 20% needs about 20 GB plus overhead for keys and memory fragmentation.

## Common traps

* Updating the key on a write instead of deleting it. Two concurrent updates can land out of order and leave the old value cached.
* Treating eviction as expiry. A full cache can evict a fresh value before its TTL ends.
* Promising to purge a browser-cached response. You can't, so pick Cache-Control lifetimes the product can live with.
* Drawing Redis as a database proxy in cache-aside. The application runs the fallback query and fills Redis.
* Calling per-process caches shared. Servers can hold different versions of the same profile.

## Follow-up questions interviewers ask

* "What happens when the cache goes down?" With cache-aside reads still work, but all of them hit the database. Check it survives that, and a cold start.
* "When would you use write-behind?" For write-heavy data where losing a few seconds of updates is fine, such as view counters. Never for data you can't lose.
* "LRU or LFU?" LFU when one-off scans of cold keys would push out steadily popular ones.

## Frequently asked questions

### What is the most common caching strategy?+

Cache-aside. The application reads the cache, falls back to the database on a miss and stores the result with a TTL. Writes update the database and delete the key.

### What is the difference between write-through and write-behind?+

Write-through writes both before returning, so they stay in sync but every write pays twice. Write-behind returns after the cache write and flushes later: faster, but updates are lost if the cache fails first.

### How big should a cache be?+

Big enough for the hot data. If about 20% of records get most reads, size for that plus overhead, then watch the hit ratio, because every lost point lands on the database.

## More system design guides

[All 76 guides](https://systemdesignsim.com/system-design)

* [Design a Twitter TimelineFan-out and the celebrity problem](https://systemdesignsim.com/system-design/twitter-timeline)
* [Design a Netflix CDNEdge caching and the launch stampede](https://systemdesignsim.com/system-design/netflix-cdn)
* [CQRS and Event Sourcing explainedRead models, event logs, projections and replay](https://systemdesignsim.com/system-design/cqrs-event-sourcing)
* [Fault Tolerance explainedNines, retries, circuit breakers, chaos](https://systemdesignsim.com/system-design/fault-tolerance)
* [Design a Maps ETA and Routing Service](https://systemdesignsim.com/system-design/maps-eta)
* [Design a Quick Commerce App like Zepto or Blinkit](https://systemdesignsim.com/system-design/quick-commerce-delivery)

## Now fix it yourself.

Run the design under real traffic and see what breaks first. No signup.

[Survive a cache stampede](https://systemdesignsim.com/challenges/the-stampede)
