How it works · 18 of 49 Core
What are caching patterns and cache invalidation?
Fast, and how wrong
Caching patterns & invalidation: A cache keeps a copy of data close so most reads skip the slow store; every copy can be out of date. With cache-aside, a read checks the cache, and on a miss fetches from the store and keeps the result for a set time (the TTL, time to live). A longer TTL answers more reads from the cache and serves more stale ones; deleting the cached item when the data changes (invalidation) fixes most of that, if the store is updated first. Choose the TTL by how wrong, for how long, the reader can afford to be.
Loading the animation…
Say it like this A cache trades freshness for speed: the longer you keep a copy, the more reads it answers and the more of them are wrong, so delete the copy when the data changes, after the store is updated, and keep a TTL as a backstop.
Under the hood — for builders
- The sources, in their words
- Microsoft ("Cache-Aside pattern"): "When an application updates information, it writes the change to the data store and then invalidates the corresponding item in the cache." Jas Chhabra (Amazon Builders' Library): "Cached data necessarily grows inconsistent with the source over time"; "The TTL is chosen based on client requirements, such as how tolerant the client can be to stale data."
- Cache-aside, step by step
- Cache hit: the answer was in the cache. Miss: it wasn't, so the store is read. TTL (time to live): how long a copy is kept before it's thrown away.
- Simulated here: the TTL dial
- One price read once a second; it changes on average every 10 minutes (assumptions). Longer TTLs answer more reads from the cache and serve more stale ones:
- Simulated here: invalidation and its order
- Same run with a 5-minute TTL. Deleting the cached item on every change, after the store is updated, removes nearly all stale reads; deleting first lets a read in the gap put the old price back:
- Cold cache
- When the cache is empty (a restart, a new server), every read goes to the store. Chhabra: this "could cause a surge of traffic to downstream services." Size the store for it.
- Measure it
- Hit rate · age of the oldest entry served · reads that saw a value older than the store's · store load with the cache off.
- Watch out
- Microsoft: cache-aside "doesn't guarantee consistency"; for read-after-write freshness consider write-through. Microsoft also advises against caching data that is sensitive or security related.
- Sources
- Microsoft, "Cache-Aside pattern", Azure Architecture Center, 2025 · Jas Chhabra, "Caching challenges and strategies", Amazon Builders' Library, 2019 · both read in session · simulation, Python, run 19 Nov 2026.
What happens in the caching patterns & invalidation animation, step by step?
- A cache is the shelf tag: a copy of the price kept close. Shoppers read the tag; only when it expires does a clerk walk to the back office.
- The price changes in the database. Until the tag expires, the shelf says ₹40 and checkout charges ₹45.
- Your turn: drag how long tags are kept, below. Watch both bars.
- Better: when the price changes, take the tag down. The next shopper's question fetches ₹45. Wrong prices fall to 0.02%.
- Order matters: take the tag down before the change and ₹40 goes back up for five minutes. Update the store, then invalidate. Next: a board built only for reading.
What everyday example explains caching patterns & invalidation?
The shelf price tag vs the till. Shoppers read the tag; the till reads the back office. When the price changes, someone has to take the old tag down, after the new price is in the system.
Why should a team care about caching patterns & invalidation?
Every cache is a decision about how stale is acceptable, for whom. Prices, balances and permissions need invalidation (or no cache); a product description can live for an hour. Size the store for the day the cache is empty.