Caching in Application Code
The cache-aside pattern as you actually write it in a request handler, and why invalidation is the hard part, not the lookup.
Caching in Application Code
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Cache-aside means your application code, not the cache itself, is responsible for the logic: check the cache first, and only go to the real data source on a miss.
- A TTL (time-to-live) is the cheap, blunt safety net for staleness — it bounds how long a wrong answer can live, even if you forget to invalidate it explicitly.
- Cache invalidation — deciding what to evict or update when the underlying data changes — is the genuinely hard part of this pattern, which is why it's one half of the famous 'two hard problems in computer science' line.
- An in-process memory cache is simpler and faster but only helps the single process holding it, while an external cache like Redis is shared across every instance of your service at the cost of a network hop.
The problem caching actually solves
Some piece of data in your request handler is expensive to produce — a query that joins three tables, an aggregate computed over a lot of rows, a call to a slow third-party API — and you’re computing it over and over for requests that, most of the time, would get the exact same answer. Caching is just: compute it once, remember the answer, and hand out the remembered answer to everyone who asks again, instead of redoing the expensive work every time.
The pattern almost every backend developer reaches for first, because it’s the one that fits naturally into a request handler without any special infrastructure, is called cache-aside.
Cache-aside, step by step
Your application code owns the logic. It isn’t a feature the cache provides for you — it’s a sequence of decisions you make in your own handler:
def get_product(product_id):
cached = cache.get(f"product:{product_id}")
if cached is not None:
return cached # cache hit
product = db.query(
"SELECT * FROM products WHERE id = %s", product_id
) # cache miss — go to the real source
cache.set(f"product:{product_id}", product, ttl=300)
return product
Check the cache first. If it’s there, you’re done — no database touched at all. If it’s not, fall through to the real source, get the answer, and before returning it, stash a copy in the cache so the next request for the same key doesn’t have to repeat the work. The name “cache-aside” comes from exactly this shape: the cache sits beside the real data source, and your code decides when to use it, rather than the data source being wrapped by a caching layer that intercepts every call automatically.
The ttl=300 matters more than it looks. A time-to-live is the simplest safety net against staleness you can build: whatever you cached expires automatically after five minutes, no matter what. It doesn’t solve the problem of a cached value being wrong before it expires, but it bounds the damage — even if nothing else ever tells the cache this entry changed, the entry can’t be wrong forever. Pick a TTL by thinking about how stale an answer you can tolerate: a product’s price listing can probably live for minutes, a user’s current session validity probably shouldn’t.
The part that’s actually hard: invalidation
The read path above is the easy half of caching. The hard half shows up the moment the underlying data changes while a cached copy of the old value is still sitting around. Someone updates the product’s price in the database — does the cached copy from two minutes ago get updated too? If your write path doesn’t explicitly touch the cache, the answer is no, and now you’re serving a wrong price until the TTL expires on its own.
This is the real substance behind the famous line, usually attributed to Phil Karlton: “there are only two hard things in computer science: cache invalidation and naming things.” It’s a joke with a real point. The read side of caching is conceptually trivial — check, miss, populate, return. The write side has to account for every place that could change the data a cache entry depends on, and get all of them to agree on when that entry is no longer valid.
The simplest, and usually safest, invalidation strategy is: when you write, delete the cache key, don’t try to update it in place.
def update_product_price(product_id, new_price):
db.execute(
"UPDATE products SET price = %s WHERE id = %s",
new_price, product_id,
)
cache.delete(f"product:{product_id}")
Deleting is safer than overwriting the cache with the new value directly, because overwriting assumes your write path always has the complete, correctly-shaped value the cache expects — and any mismatch there silently corrupts what every subsequent reader sees. Deleting just falls back to the normal cache-aside miss path: the next read recomputes the value from the source of truth and repopulates the cache correctly. It costs one extra cache miss; it buys you not having two different code paths (the normal populate-on-miss path, and a separate update-cache-on-write path) that can drift out of sync with each other.
Even this isn’t airtight. A slow read that started before a write finishes can still land its stale result in the cache after the write’s delete has already run — the read was already past the “it’s a miss, let me query the source” step before the data changed underneath it, and it writes what it found moments ago as if it were still current. This is a genuine race condition, not a bug in the code above; at scale, teams handle it with techniques like short TTLs as a backstop, versioned cache keys, or event-driven invalidation pipelines — which is exactly where this lesson stops and the distributed-systems version of this problem begins.
Where you put the cache: in-process versus external
There are two common places this pattern lives. An in-process memory cache — a dictionary or an LRU cache living in your application’s own memory — is the simplest option: no network call, extremely fast, nothing extra to deploy. Its limitation is that it’s local to one process. If your service runs as five instances behind a load balancer, each instance builds its own separate cache, and a write handled by instance 2 does nothing to the stale copy sitting in instance 4’s memory.
An external cache like Redis or Memcached solves that by being a separate, shared service every instance of your app talks to over the network. Every instance sees the same cached value and the same invalidation, at the cost of a network round trip on every cache access instead of a plain memory lookup. For a single-instance toy project, an in-process cache is often enough. For anything running multiple instances — which is most real production services — an external cache is usually the only way the cache stays consistent across them.
Where this lesson stops
Everything above is about writing this pattern correctly inside your own request handler, for one service with one cache. What happens when you need a CDN caching responses at the edge, cache topology spread across many machines, or eviction policies deciding what to drop under memory pressure, is a different, larger problem — covered in caching and CDNs, caching strategies at scale, and distributed caching systems. This lesson is the version you’ll actually write by hand, probably this week, in a handler like the one above.
Check yourself
What is the cache-aside pattern, step by step?
On a read, check the cache first. If the key is present (a hit), return it directly without touching the real data source. If it's absent (a miss), query the real source, store the result in the cache with a TTL, and return it. The cache is only ever populated lazily, in response to an actual miss — nothing pre-loads it.
Why is a TTL necessary even if you also invalidate the cache explicitly on writes?
Explicit invalidation depends on every single code path that changes the underlying data remembering to also touch the cache — a batch job, a direct database edit, or a new code path that someone forgets to update can all silently skip it. A TTL is a safety net that doesn't depend on anyone remembering anything: even a missed invalidation only serves stale data for, at most, the TTL window, not forever.
Why is cache invalidation considered the hard problem, when the cache-aside read path is simple?
The read path only has to handle one cache, one key, one miss at a time, in isolation. Invalidation has to account for every write path that could change the underlying data, every other cached key that might derive from or aggregate that data, and race conditions where a write and a concurrent read can leave a stale value in the cache indefinitely if the order of operations goes wrong — none of which the simple hit/miss logic has to think about at all.
What's a concrete race condition that can leave a cache stale even with correct-looking invalidation code?
A read sees a cache miss and starts querying the real data source; before it finishes, a write updates that data and deletes the (currently empty) cache key; the slow read then finishes and writes its now-stale result into the cache, where it can sit untouched until the TTL expires, because nothing else triggers another invalidation.
When would you reach for an in-process memory cache instead of an external cache like Redis?
When the cached data is small, cheap to reconstruct, and it's acceptable for each process or server instance to have its own slightly different copy — a single process's local cache is faster because there's no network hop, but it isn't shared, so a write handled by one instance doesn't invalidate the copy sitting in another instance's memory unless you add a separate mechanism to broadcast that invalidation across instances.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.