Layered caching: browser, CDN, Redis and database
· 9 min readcacherediscdn
“Add a cache” is probably the most common answer to “this is slow”. And it usually works. The problem is that “cache” isn’t one piece: it’s a chain of copies of the same data, spread between the user’s browser and the database’s disk. Every link saves latency and, at the same time, adds one more place where stale data can live.
This post is the full map: which layers there are, what belongs in each one, how they’re invalidated and when one of them is one too many.
The diagram
User
│
▼
┌──────────────┐ Cache-Control, ETag
│ Browser │ private, per user, can't be invalidated from outside
└──────┬───────┘
│ miss
▼
┌──────────────┐ s-maxage, cache key, invalidations
│ CDN │ shared, close to the user
└──────┬───────┘
│ miss
▼
┌──────────────┐
│ Application │──────▶ ┌──────────────┐ cache-aside, TTL, keys
│ │◀────── │ Redis │ shared across instances
└──────┬───────┘ └──────────────┘
│ miss
▼
┌──────────────┐ buffer pool, materialized views
│ Database │ the only source of truth
└──────────────┘
The higher up a request is resolved, the cheaper it is. A response from the browser costs nothing: it doesn’t even hit the network. One from the CDN is served from a nearby node without touching your infrastructure. One from Redis saves the query but has already gone through your application. And the database is the last resort, the only one that’s actually right.
| Layer | Who shares it | Typical latency | How it’s invalidated |
|---|---|---|---|
| Browser | A single user | 0 (no network) | It can’t be: it only expires or the URL changes |
| CDN | Every user in a region | Tens of ms | TTL or invalidation by path |
| Redis | Every application instance | ~1 ms inside the network | TTL or deleting the key |
| Database | The whole system | From ms to seconds | Not needed: it’s the truth |
What belongs in each layer
Browser: what doesn’t change, or what’s cheap to check
The browser is the fastest cache and the most dangerous one, because there’s no way to invalidate it from the server. Whatever you stored there with a one-day max-age stays there for a day, unless the user forces a reload.
So the rule is simple:
- Assets with a hash in the name (
app.3f9a1c.js,logo.8b2e.svg):Cache-Control: max-age=31536000, immutable. If the content changes, the name changes, and the old URL is no longer requested. They never need invalidating. - HTML and API responses: a very short or
0max-age, withETagorLast-Modified. The browser asks “has this changed?” and, if not, gets a304with no body. There’s still a round trip, but almost no transfer. - Responses with user data:
private(only the browser may store it, never a shared cache) orno-storeif it’s sensitive.
That’s what this very site does: assets under /_astro/ are immutable forever and the HTML is revalidated on every visit.
CDN: what’s the same for many users
The CDN is a shared cache. Its value is that the same response serves thousands of people, so what makes sense to store there is whatever doesn’t depend on who’s asking: public pages, images, catalogues, public API responses.
Two details make the difference:
s-maxageseparate frommax-age. It lets the CDN keep a page for an hour while the browser always revalidates it. That way, when you invalidate the CDN, the change reaches everyone right away.- The cache key. Every header, cookie or parameter that goes into the key multiplies the variants and sinks the hit rate. And the opposite is worse: if a personalised response ends up in the CDN under a key that doesn’t tell users apart, the next visitor sees the previous one’s data. It’s one of the most serious caching incidents there is, and it almost always starts with a
Cache-Controlheader nobody reviewed.
stale-while-revalidate and stale-if-error deserve a special mention: they let you serve the old copy while a new one is fetched, or while the origin is down. They turn the CDN into a resilience layer, not just a speed layer.
Redis: what’s expensive to compute and requested often
Redis lives inside your network and is shared by every instance of the application. It’s the place for whatever is costly to obtain and repeated a lot: heavy query results, responses from other services, sessions, counters, aggregates.
The usual pattern is cache-aside: the application checks Redis; if the value isn’t there, it fetches it from the database, stores it with a TTL and returns it.
read(key):
value = redis.get(key)
if value exists: return value
value = database.query(...)
redis.set(key, value, TTL + jitter)
return value
What separates a Redis cache that helps from one that causes trouble:
- TTL with jitter. If a thousand keys are written at once with the same TTL, they expire at once and all of them hit the database in the same second. A random margin spreads out the expiry.
- Stampede protection. When a very popular key expires, a hundred concurrent requests recompute it at the same time. A short lock (only one recomputes, the rest wait or serve the old copy) stops the database from receiving the same expensive query a hundred times.
- Negative caching. Also store “doesn’t exist” for a short time. Otherwise, every request for a missing resource goes through every layer down to the database.
- Bounded memory and an eviction policy. Redis without a memory limit eventually becomes an operations problem. With
maxmemoryandallkeys-lru, whatever is used least goes away on its own.
And one non-negotiable rule: Redis is not the source of truth. If it’s flushed tomorrow, the application has to keep working: slower, but correct.
Database: the cache you already have
Before adding layers on top, it’s worth remembering that the database already caches. MongoDB (with WiredTiger), PostgreSQL and MySQL keep the most-read pages in memory. If the data queried day to day fits in that memory, most reads never touch the disk.
The old “query cache”, which stored results by query text, has disappeared almost everywhere (MySQL removed it in version 8.0) because every write invalidated it and it scaled poorly. What does work in this layer:
- Indexes that match the real queries. A good index does more for latency than any cache.
- Enough memory for the working set. Sometimes the best cache is an instance with more RAM.
- Materialized views or precomputed tables for expensive aggregates that are read a lot and change little. It’s a cache, but under the database’s rules.
Invalidation: the real problem
Storing things is easy. The hard part is knowing when they stop being true.
TTLs add up
This is the detail people forget most. Imagine a price that goes through three layers:
Redis: TTL 10 min ─┐
CDN: s-maxage 5 min ─┼─▶ worst case: 10 + 5 + 1 = 16 min of stale price
Browser: max-age 1 min ─┘
The CDN can request the page right when Redis holds a value that’s almost ten minutes old, keep it for five more, and the browser can hold on to it for one more. The oldest data a user can see is the sum of the TTLs of every layer it passes through, not the TTL of the layer you’re looking at. When someone from the business asks “how long until a change shows up?”, the answer is that sum.
Three ways to invalidate
| Strategy | How it works | When to use it |
|---|---|---|
| Expiry (TTL) | The data dies on its own after a while | When a bounded delay is acceptable. It’s the default option. |
| Explicit invalidation | On write, the Redis key is deleted or the CDN path is invalidated | When the change must show up now. It requires knowing which keys depend on each piece of data. |
| Versioning | The new data lives under a new key or URL; the old one stops being used | Assets, configuration, anything with a version number. It’s the only one that works in the browser. |
Explicit invalidation looks like the best option, but it has a hidden cost: every write has to know every cache that depends on that data. Over time, dependencies nobody remembers appear, and a change that “should show up” doesn’t. That’s why I usually combine it with a safety TTL: if an invalidation gets lost, the stale data at least has an expiry date.
Order matters
When several layers need invalidating, it’s done from the inside out: first write to the database, then delete from Redis and finally invalidate the CDN. Done the other way round, the CDN can request the data again before Redis has been cleared and end up with the old version once more, now with a fresh TTL.
And even in the right order, another write can sneak in between reading from the database and writing to Redis. That’s why, with cache-aside, it’s safer on write to delete the key rather than update it: the next read will recompute it with the correct data.
When a layer is one too many
Every layer saves latency, but it also adds configuration, memory, monitoring and another way to serve stale data. Some signs that one is one too many:
- The hit rate is low. A cache that hits 20% of the time adds a network hop to 80% of requests in exchange for very little. If you don’t measure the hit rate, you don’t know whether the layer helps.
- The data is personal and changes often. A cart, a balance or an inbox isn’t shared between users and changes with every action. There the CDN adds nothing and Redis adds little.
- The database already answers fast. If the query takes 2 ms with a good index, putting Redis in front saves almost nothing and adds one more system that can fail.
- The data needs strong consistency. Stock at the moment of payment, permissions or a balance aren’t read from a cache. They can be cached for display, but the decision is made against the source of truth.
- The real problem is something else. Sometimes the cache hides a query without an index or a synchronous call that shouldn’t be on the critical path. In those cases, changing who owns the data solves more than caching it.
What I take away
Each layer answers a different question. The browser avoids the network, the CDN avoids your infrastructure, Redis avoids expensive work and the database keeps the truth. If you don’t know which question a layer answers, it’s probably one too many.
What’s immutable isn’t invalidated, it’s renamed. Everything that can be versioned (assets, configuration, exports) should be. It’s the only invalidation that never fails.
The maximum delay is a sum. Before promising “changes show up instantly”, add up the TTLs of every layer.
A cache is an optimisation, not a dependency. If turning off Redis or the CDN breaks the application instead of making it slower, the cache has become a source of truth without the guarantees of one.
A well-built cache is invisible: everything is fast and nobody wonders why. A badly built cache is invisible too, until the day someone sees a price that no longer exists, or another user’s data.
Prompt for your AI
Paste it into your coding assistant (Claude Code, Cursor, Lovable…) so it builds this with the decisions from the post.
Act as a senior backend architect: review my application's caching strategy layer by layer and propose concrete changes. Layers to review, from the outside in: - Browser: Cache-Control headers per resource type. Assets with hashed names: max-age=31536000, immutable. HTML and APIs: short or 0 max-age with ETag/Last-Modified to answer 304. Personal responses: private or no-store. - CDN: s-maxage separate from max-age, a minimal cache key (which headers, cookies and query params go in), correct Vary, and stale-while-revalidate and stale-if-error where they make sense. Never cache responses with a user's data in a shared cache. - Application cache (Redis): cache-aside pattern, versioned keys, TTL with jitter, stampede protection (only one process recomputes), negative caching for what doesn't exist and an eviction policy (e.g. allkeys-lru) with bounded memory. Redis is never the source of truth: if it's flushed, the application keeps working, just slower. - Database: the working set fits in memory (buffer pool), indexes match the real queries, and materialized views or precomputed tables for expensive data that is read a lot. For each cached piece of data, tell me: - Which layer it lives in and why there. - How stale it can get in the worst case (the sum of the TTLs of every layer it passes through). - How it's invalidated: TTL, explicit invalidation on write, or a versioned URL. Don't: - Add a layer without first measuring its hit rate and the latency it saves. - Cache data that needs strong consistency (balances, stock at checkout, permissions) without an explicit plan. Verify: hit rate per layer, what happens to the database if Redis is flushed all at once, and that a content change becomes visible within the time the business expects. Before starting, ask me whatever you can't infer (stack, CDN, which data tolerates delay) and explain each decision.