Cache Aside: The App Fills a Cache on Demand and Clears It on Change
Cache Aside means the app checks a cache first, loads from the database on a miss, and deletes cached copies when the data changes.
In short
Cache aside means the app checks a cache first, loads from the database on a miss, and deletes cached copies when the data changes.
10 min readFree
What You Will Learn
Cache Aside is a way to make an app faster by keeping copies of data in a fast store close to the app. In this lesson you will learn how reads pass through the cache, how the app clears old copies when data changes, and what you give up in return. The pattern is also called lazy loading, because data enters the cache only when someone asks for it.
Key words
- Application (app)
- The program code that sits between users and the data. In Cache Aside, the app decides when to use the cache.
- Cache
- A fast store, often kept in memory, that holds copies of data so the app does not have to fetch them again every time.
- Database
- The main place where the system keeps its data for good. It is the official copy that everything else is measured against.
- Cache hit
- A request where the wanted item is already in the cache.
- Cache miss
- A request where the item is not in the cache, so the app must go to the database.
- Key
- A unique name for one cached item, such as product:1001. The app uses the key to store and find the copy.
- TTL (time to live)
- A timer set on a cached item. When the timer ends, the cache removes the item by itself.
- Invalidate
- Remove a cached copy so that nobody reads it again. The next read then loads fresh data.
- Stale
- Old data that no longer matches the database.
- Cold start
- The slow period when the cache is empty or nearly empty, so most requests are misses.
What Is the Read Flow?
Every read in Cache Aside follows the same path. The app asks the cache first. Only when the cache has nothing does the app query the database, and then it saves the result in the cache for the next request. The cache never fetches data on its own. The app runs every step.
- 1
Check the cache
Ask the cache for the item using its key, such as product:1001.
- 2
Return on a hit
If the cache has the item, send it back at once. The database is not touched.
- 3
Load on a miss
If the cache has nothing, query the database for the matching row or record.
- 4
Store the copy
Save the loaded data in the cache under the same key, with a TTL, so later reads are fast.
- 5
Return the data
Send the result to the caller. The caller gets the same answer whether it was a hit or a miss.
Tip
Build keys in one consistent pattern, such as product:1001. If two parts of the app build keys in different ways, one part will miss the cache and another may delete the wrong copy.
The big idea is that the cache is optional. It is a helper that stands beside the main data path, not a layer that every request must pass through in a fixed way.
The app
Runs every step. It chooses what to cache, which key to use, and how long a copy should live.
The cache
Stores the copies it is given and returns them on request. It does not reach into the database by itself.
The database
Holds the official data and answers only when the app asks it a question.
Who Keeps the Cache Correct?
Because the app owns both the cache and the database calls, the app must also keep the two in line. If the app forgets to clear a copy after an update, the cache keeps the old value. No hidden part of the system will fix that for you.
What Is the Write Flow?
When data changes, the order of steps matters. The app updates the database first, because the database is the official copy. Then the app deletes the cached copy. It does not write the new value into the cache. The next read finds no cached copy, loads the new data from the database, and stores it again.
- 1
Update the database
Save the change to the official record first.
- 2
Delete the cache key
Remove the old copy stored under that key.
- 3
Let the next read refill
The next request misses, loads the fresh row, and stores it again with a new TTL.
Why Delete Instead of Update?
Writing the new value straight into the cache can seem simpler. But two requests can race each other. A reader may load an old value from the database. Before that reader stores its copy, a writer updates the database and clears the key. The reader then stores its old value, and that stale copy stays in the cache until its TTL runs out. Deleting the key avoids most of this trouble, because the next reader fetches the latest value from the database. The race does not vanish completely, so a TTL is still needed.
Why Use a TTL as a Safety Net?
A delete can fail. A bug, a crash, or a lost network message can skip the step that clears the key. A TTL is the backup plan. Every cached item gets a timer, such as 10 minutes. When the timer ends, the copy expires, and the next read loads fresh data. A short TTL keeps data fresher but sends more reads to the database. A long TTL reduces database load but lets a wrong copy survive for longer.
Tip
Choose the TTL by asking two questions: how often does this data change, and how long can readers wait before a wrong copy disappears?
Worked Example: A Shop's Product Page
Imagine an online shop that sells mugs. Each product page reads its details from a products table in a database. The team uses Redis as its cache. Each product is saved under a key that includes its ID, such as product:1001. The steps below follow two visitors and then a seller who changes the price.
- 1
First visitor asks for product 1001
The app checks Redis for product:1001. Nothing is there, so this is a cache miss.
- 2
Load the row from the table
The app queries the products table for the row with ID 1001 and gets the current details, including the price.
- 3
Save with a 10-minute TTL
The app stores the row in Redis under product:1001 and sets a TTL of 10 minutes. The copy will expire on its own after that.
- 4
Second visitor asks within a minute
Redis still has the copy. This is a cache hit, so the page is built without touching the products table.
- 5
Seller edits the price
The app updates the price in the products table first, because the table is the official record.
- 6
Delete the cache key
The app deletes product:1001 from Redis, so the old price is no longer stored there.
- 7
Next visitor reloads
The next request is a miss. The app loads the fresh price, stores it again with a new 10-minute TTL, and returns it.
Notice what happens if the delete step fails. The cache would keep the old price until the 10-minute TTL ends. That is the reason the TTL is there: it limits how long a missed delete can hurt.
Tip
When you design a page like this, write down three things for each cached item: its key, its TTL, and the code path that must delete the key after every change.
Run the Idea in Code
The sample below copies the product page flow in plain Python. A dictionary plays the cache, and another dictionary plays the database. The code counts how many times it reads the database, so you can see the cache doing its job.
DB = {"1001": {"name": "Ceramic mug", "price": 900}}
cache = {}
db_reads = 0
def get_product(pid):
global db_reads
key = "product:" + pid
if key in cache:
return cache[key]["price"], "hit"
db_reads += 1
cache[key] = dict(DB[pid]) # a real system also sets a TTL here
return cache[key]["price"], "miss"
def set_price(pid, price):
DB[pid]["price"] = price
cache.pop("product:" + pid, None)
print(get_product("1001"))
print(get_product("1001"))
set_price("1001", 750)
print(get_product("1001"))
print("database reads:", db_reads)
Output
(900, 'miss') (900, 'hit') (750, 'miss') database reads: 2
The second read never reaches the database, because the first read saved a copy. After the price changes, the code deletes the key, so the next read goes back to the database once and then the cache works again.
Trade-Offs
Cache Aside is popular because it is simple and forgiving. It also has costs that you must plan for. The list below shows both sides.
Only requested data is cached
Items enter the cache only when someone asks for them, so memory is not spent on rows nobody reads.
A cache failure does not stop reads
If the cache is down, the app can fall back to the database. Pages get slower, but they still work.
The first request is slow
A miss costs a cache check plus a database query, and the cache must refill after a restart or after copies expire.
Readers may see old data for a while
Between a write and the next read, or until a TTL ends, a reader can receive an outdated value.
The Cold Start
When the cache is empty, for example after a deploy or a restart, almost every read is a miss. This slow beginning is called a cold start. Some teams fill the cache with popular items when the app starts, which is called priming. Cache Aside still works without priming, because each miss fills the cache one item at a time.
The Staleness Window
Cache Aside does not promise that the cache always matches the database. A reader might load a copy just before an edit lands. An outside process might change a row without clearing the key. In both cases the wrong copy lasts until the key is deleted or the TTL ends. Before you choose this pattern, ask how long the data can be wrong before someone notices. If the answer is never, Cache Aside alone is not enough.
Tip
Cache Aside suits pages that many people read and few people change, such as product pages and articles. Be careful with data that must be exact at every moment, such as a bank balance.
Check Your Understanding
Try these two short scenarios. Each one needs the ideas from this lesson.
Quick check
A news site's article page uses Cache Aside with Redis. An editor fixes a typo in the database, but readers still see the old text for several minutes. What probably happened?
Quick check
A team stores site settings in Redis with no TTL. They delete the key whenever a setting changes. One day a bug stops the delete from running. What happens next?
Key Takeaways
Read: check, load, store
The app checks the cache, loads from the database on a miss, stores the copy with a TTL, and then returns the data.
Write: update, then delete
The app updates the database first and deletes the cache key. The next read refills the copy with fresh data.
Always set a TTL
The TTL is the safety net. It limits how long a missed delete, or a lost update, can leave stale data in the cache.
Sources
Last updated on 17 November 2026
Related lessons
- System designCache-Aside: The App Fills a Cache on Demand and Clears It on Change
- System designExternal Config Store: Change App Settings Without Redeploying
- System designWrite-Through: Save to the Cache and Database Together Before Success
- System designWeb Server Caching: Serve Saved Responses Before the App Runs
Learn with me
Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.