# Cache-Aside: The App Fills a Cache on Demand and Clears It on Change

> Learn how an app checks a cache first, loads from the database on a miss, and deletes cached entries on change, plus the trade-offs.

**In short:** Cache-Aside is a pattern where the app checks a cache first, loads from the database on a miss, fills the cache, and deletes cached entries when data changes.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/cache-aside-2

## What You Will Learn

This lesson explains Cache-Aside, a common way to use a cache in a system. By the end you will know how the read flow works, how to stop a cache from showing old data after a change, and when this pattern is a good fit. A system design is the plan for how the parts of an app work together. The cache is one of those parts, and Cache-Aside tells the app how to use it.

- **Cache:** A fast place that keeps a copy of data so the next read can be quick.
- **Data store:** The main, trusted place where data is kept, such as a database. It is the source of truth.
- **Cache hit:** A read that finds the item in the cache.
- **Cache miss:** A read that does not find the item in the cache, so the app must go to the data store.
- **TTL:** Time to live. How long a cached item stays valid before it expires and is removed.
- **Stale:** Old data that no longer matches the latest value in the data store.
- **Invalidate:** Remove a cached item, or mark it as no longer valid, so the next read gets fresh data.

## What Is the Read Path?

Cache-Aside is named for the cache sitting beside the data store. The cache is not a gate that every read must pass through on its own. The application code does the work. It checks the cache, and if the item is missing, it loads the item and fills the cache. The cache itself never loads anything.

1. **Check the cache:** The app asks the cache for the item using a key, such as a product ID.
2. **On a hit, return it:** If the cache has the item, the app returns it straight away. The database is not touched.
3. **On a miss, load from the data store:** If the cache has nothing, the app reads the item from the database.
4. **Store it with a TTL:** The app saves the result in the cache and sets an expiry time. That expiry is the TTL.
5. **Return the result:** The app sends the value back to the caller. Later reads of the same item become hits until the TTL runs out.

Each step belongs to the application. The cache only stores keys, returns them, and removes them when their time is up. It does not know which database the value came from.

### Who Owns the Logic?

In this pattern the application owns the cache logic. Azure's pattern page says the app emulates read-through caching by doing the loading itself. Many commercial caches can do this loading on their own, but when a cache cannot, the app must do it. This is also why the pattern is called on demand. Only items that someone actually asked for end up in the cache, so the cache does not fill up with data nobody reads.

## What Is the Write Path?

Reads are only half of the job. When data changes, the cache can still hold the old value. The usual fix has three steps. They run in a fixed order.

1. **Write to the data store first:** The app saves the change in the database. The database stays the source of truth.
2. **Delete the cache entry:** The app removes the cached item, so the next read is a miss.
3. **Let the next read refill it:** The next request loads the new value from the database and stores it again with a fresh TTL.

> **Tip:** If the delete step is skipped, readers can keep seeing the old value until the TTL ends. Skipping this step can leave readers seeing old values.

### Why Delete Instead of Update?

You could write the new value into the cache instead of deleting the old one. Many teams avoid that. The Redis Patterns page describes deleting the cache key as the simplest and safest choice. When the app deletes, the next read rebuilds the cached value from the database, the same way every other read does. When the app updates, it must build the cached value itself. That copy can differ from what the database holds, for example after a formatting step or a second change arrives at about the same time.

### What Are the Race Conditions?

A race condition happens when two actions run at nearly the same time, and the result depends on which one finishes first. Here is one case. Reader A misses the cache and reads the old price from the database. Writer B saves the new price and deletes the cache key. Reader A then stores the old price in the cache, because it read the database before the change. Now the cache holds the old price until the TTL ends. Deleting instead of updating lowers this risk, and a short TTL limits how long the damage lasts. Even so, Cache-Aside does not guarantee that the cache and the database always match.

### Choosing the TTL

The TTL is a trade-off. A short TTL keeps data fresher, but items expire often and more reads go to the database. A long TTL protects the database, but a stale value can live longer if a delete was ever missed. Azure's guidance says the expiry should match the way the application reads the data. If the expiry is too short, the app keeps reloading the same items. If it is too long, the data goes stale. A good starting point is to ask two questions: how often does this data change, and how much harm does an old value cause?

## Worked Example: A Product Page

Consider an online store. Each product page shows a name, a price, and a stock count. Many visitors read the same product, but the price changes only now and then. The store uses Redis, a cache server that holds items under keys, and a product database as the data store. Each cached product lives under a key such as product:p-001.

1. **A visitor opens the page:** The server builds the key product:p-001 and asks Redis for it.
2. **Redis has no entry (a miss):** The server queries the product database for p-001.
3. **Save and show:** The server stores the result in Redis with a 10-minute TTL, then renders the page.
4. **Next visitors get hits:** For the next 10 minutes, the server reads the product from Redis and skips the database.
5. **A seller changes the price:** The app writes the new price to the database first.
6. **The app deletes the cached entry:** The app deletes product:p-001 from Redis, so the old price is gone from the cache.
7. **The next visitor sees the fresh price:** That visitor gets a miss, reads the new price, and the cache is filled again.

The order in the write path matters. If the app deleted the cache entry first and wrote to the database second, a visitor could reload the old price between those two steps and store it in the cache. Writing first and deleting second closes most of that gap.

### Running the Read Flow in Python

The sample below copies the idea without a real Redis server. A dictionary plays the cache and another dictionary plays the database. The time is a number we pass in, measured in seconds, so 600 means 10 minutes. The sample shows a miss, a hit, a price change with a delete, and then a miss again.

```python
cache = {}
database = {'p-001': {'name': 'Mug', 'price_cents': 899}}
db_reads = 0

def get_product(product_id, now):
    global db_reads
    entry = cache.get(product_id)
    if entry is not None and entry['expires_at'] > now:
        return entry['value'], 'hit'
    db_reads += 1
    product = dict(database[product_id])
    cache[product_id] = {'value': product, 'expires_at': now + 600}
    return product, 'miss'

def update_price(product_id, new_price):
    database[product_id]['price_cents'] = new_price
    cache.pop(product_id, None)

print(get_product('p-001', 0))
print(get_product('p-001', 60))
update_price('p-001', 799)
print(get_product('p-001', 61))
print('db reads:', db_reads)

```

Output:

```text
({'name': 'Mug', 'price_cents': 899}, 'miss')
({'name': 'Mug', 'price_cents': 899}, 'hit')
({'name': 'Mug', 'price_cents': 799}, 'miss')
db reads: 2

```

Read the output line by line. The first read is a miss, so the database is queried. The second read is a hit, so the database is skipped. After the price change, the cache entry is removed, so the third read is a miss and returns the new price. The database was read twice in total.

## When Cache-Aside Fits and What It Costs

Cache-Aside suits data that many people read, that changes rarely, and that can be a little out of date for a short time. Product details, article text, and display names often fit. It is a weak fit for data that must always be exact, such as a bank balance, and for data that is read only once. Azure's pattern page says storing sensitive data in a cache might be inappropriate, especially in a shared cache, and that such data should always be retrieved from the primary source.

- **Lower latency:** Hits come back from the cache, so users wait less than they would for a database read.
- **Less database load:** Repeated reads never reach the database, which leaves it free for writes and new requests.
- **Survives a cache failure:** If the cache is down, the app can read from the database. It is slower, but the system keeps working.
- **Caches only what is used:** Because items load on demand, cache space goes to data people actually request.

- **Cold-start misses:** After a restart or on an empty cache, many early reads miss at once and hit the database together.
- **Possible staleness:** Readers can see old data until the entry is deleted or its TTL ends.
- **Extra code:** Every place that reads, writes, or deletes this data must follow the same rules.
- **Eviction policy:** A cache is smaller than the database. When it is full, it removes items, often the least recently used, so popular items can be dropped early.

> **Tip:** If each server keeps its own private in-memory cache, copies on different servers can disagree. A shared cache, such as Redis, lets one delete clear the entry for every server.

**Quick check:** A news site caches article pages with Cache-Aside and a 1-hour TTL. An editor fixes a serious error in an article, but readers still see the old text for up to an hour. Which step was missing from the write path, and how would you fix it?

A. Nothing was missing. The 1-hour TTL is working as designed, so the team should wait for it to expire.
B. The editor should have written the fix to the cache first and then to the database.
C. After saving the fix to the database, the app should delete the cached article so the next read reloads it from the database.
D. The read path should skip the cache for every article, so readers never see old text.

**Answer:** C. The write path saved the fix but never invalidated the cached copy. The fix is to delete the cache entry right after the database write, so the next reader loads the corrected article. The TTL is only a backstop for missed deletes, not the main way to keep content fresh.

## Common Mistakes to Avoid

- **Forgetting the delete:** The app updates the database but never clears the cache, so readers see old values until the TTL ends.
- **Skipping the TTL:** Without an expiry, one missed delete can leave bad data in the cache for a very long time.
- **Caching data that rarely repeats:** If most requests miss, checking the cache first only adds delay. The overhead of loading and storing data may outweigh the gain.
- **Caching sensitive data in a shared cache:** Private or security-related data should be read from its main source, not kept in a cache that other users might reach.

Cache-Aside is simple, but it is not free. It works best when the team writes down which data is cached, how long it lives, and which writes must delete which keys. Those rules are what keep the pattern predictable.

## Check Your Understanding

Try one more scenario. Use the read flow, the write path, and the race condition idea from above.

**Quick check:** A team caches user profile pages with Cache-Aside and a 30-minute TTL. A user changes a display name. The app writes the new name to the database and deletes the cache key. A few seconds later, another user still sees the old name. Which explanation fits best?

A. The TTL is too short, so the cache expired early and returned old data.
B. The app wrote the new name to the cache first, so the cache kept the old copy.
C. A reader loaded the old name just before the write, then stored that old value in the cache after the delete.
D. Cache-Aside always returns the newest value after any write, so the browser must be caching the page.

**Answer:** C. The old value came back because a slow reader loaded it before the write and filled the cache after the delete. The short TTL is not the cause, and the write did not go to the cache first. The browser is not part of the Cache-Aside flow.

## Key Takeaways

Cache-Aside puts the app in charge. On a read, the app checks the cache, loads from the data store on a miss, stores the result with a TTL, and returns it. On a write, the app saves to the data store and deletes the cached entry, so the next read loads fresh data. The pattern lowers latency and database load, and it keeps working when the cache fails. The costs are cold-start misses, possible staleness, extra code, and the limits of eviction. Use a short, honest TTL, delete on every write, and do not expect perfect consistency.

## Sources

- [Cache-Aside pattern, Azure Architecture Center (Microsoft Learn)](https://learn.microsoft.com/en-us/azure/architecture/patterns/cache-aside)
- [Caching guidance, Azure Architecture Center (Microsoft Learn)](https://learn.microsoft.com/en-us/azure/architecture/best-practices/caching)
- [No-Caching antipattern, Azure Architecture Center (Microsoft Learn)](https://learn.microsoft.com/en-us/azure/architecture/antipatterns/no-caching/)
- [Redis cache-aside with redis-py (Redis documentation)](https://redis.io/docs/latest/develop/use-cases/cache-aside/redis-py/)
- [Redis cache-aside with StackExchange.Redis (Redis documentation)](https://redis.io/docs/latest/develop/use-cases/cache-aside/dotnet/)
- [Cache-Aside Pattern (Lazy Loading), Redis Patterns](https://redis.antirez.com/fundamental/cache-aside.html)
