Caching and CDNs
After today, you can explain why the same request can be fast or slow, what a CDN does, and why changing something cached is harder than it looks.
Dip your toes 1 min
A cache is a saved copy kept closer or quicker to reach than the original, so the next request can skip the slow trip. A CDN, short for content delivery network, is a set of servers around the world that hold those copies near the people asking for them. When a new Netflix season drops, the episodes don’t stream from one faraway building. Netflix copies them ahead of time to its own servers inside or near internet providers, so pressing play usually pulls video from close by. The catch matters to designers: every copy is a snapshot of the past. After the original changes, people can keep seeing the old version until the copy expires. Decide early which values on your screen must always be current, like a price.
The full lesson 10 min
Why this exists
A new season of a popular show drops on Netflix, and millions of people press play within the same few hours. Each of them needs the same files: the same episodes, the same artwork, the same menus. If every one of those requests traveled to one building full of servers, two things would go wrong. The trip would be long for anyone far from that building. And the links between that building and the rest of the internet would choke on the same bytes sent over and over.
Distance is the first problem, and it’s physical. A signal in a fiber-optic cable still takes time to cross an ocean, and a request makes the trip there and back, often several times. A single round trip between Europe and Australia takes a noticeable fraction of a second. Day 13 showed latency as the cost of distance, and caching is the main way teams buy it back.
Repetition is the second. Most of the work a busy product does is answering a question it has already answered, for someone else, a moment ago. Doing that work again for every person wastes time and money that a copy would save.
Both problems have the same fix: keep a copy of the answer close by, and reuse it. That idea runs through every layer of the stack, from your browser to the servers in the middle to the database. It explains why the same page loads instantly one time and slowly the next. It also explains a whole family of bugs where people see something that has already changed. It’s the same trade as a saved export of a design file: quick to reuse, and out of date the moment the source changes.
What it is
A cache is a store of copies kept somewhere faster or closer than the original. When a request arrives, the cache checks whether it already holds a copy. If it does, that’s a cache hit, and the copy goes straight back. If it doesn’t, that’s a cache miss. The request continues to the original source, and the cache keeps a copy of the answer for next time.
That original source is called the origin, the server that holds the authoritative version. The origin is the only place where the file or answer is guaranteed current. For files like images and video, the origin is often the product’s own backend or a storage service. Every cache is a copy of something the origin said at some point. How long ago it said it is the question that matters most.
A miss isn’t a failure, only the slow path, and it fills the cache so the next request can take the fast one. Each copy usually comes with an expiry time, called its time to live, after which the cache throws it away and asks the origin again. Engineers choose that time for each kind of data. A product image might live in a cache for a year. A stock count might live for ten seconds, or not be cached at all.
Teams measure a cache by its hit rate, the share of requests answered from a copy. A hit rate of 95 percent means only one request in twenty reaches the origin. Small changes in that number move a lot of load. Drop to 90 percent, and the origin’s traffic doubles. That’s why a cache that fails can take the origin down with it: the origin suddenly gets traffic it was never sized for.
A CDN, or content delivery network, puts this idea on a map. It’s a set of servers in many locations, often called edge servers, that keep copies of files close to the people who request them. Your browser fetches images, videos, scripts, and stylesheets from the nearest edge, not from the origin. Most large sites rent a CDN from companies like Cloudflare, Akamai, or Amazon. A CDN works best for files that are the same for everyone, so answers that differ per person usually aren’t cached there.
Netflix built its own, called Open Connect. Netflix offers its servers to qualifying internet providers at no charge, and many sit inside or directly next to those providers’ networks. In a 2016 post, Netflix said Open Connect delivered all of its video traffic. It copies files to those servers during off-peak hours, before anyone asks for them. When you press play, the video usually travels a short distance, from a server in or near your own internet provider.
Copying ahead of time is what makes Netflix’s setup unusual. Most CDNs fill on demand: the first viewer in a city gets a miss, and everyone after gets a hit. Netflix knows its catalog and roughly what people will watch, so it fills the shelves before the store opens. A big release night becomes mostly hits. It also keeps the busiest evening hours free for streaming instead of copying.
How it shows up in your work
A bug report comes in: a shipped fix still shows the old version. The fix is live on the origin, and someone’s screen still shows the old file. That’s almost always a cache, and there are several it could be. Each layer has its own copy, its own expiry, and its own way of being cleared.
So a refresh that fixes it for you proves only that your browser had an old copy. Other people may still be getting an old copy from a CDN edge until it expires. In a phone app, the app’s own saved copy is a fifth place; signing out or reinstalling may be the only reset.
When you report this kind of bug, say what you did and what changed: a refresh, a private window, a different device. That tells an engineer which layer to look at. Teams often name files with a version code, so a new release gets a new address and old copies don’t matter.
What can be cached is the second place you’ll meet this, and it shapes screens. A show’s artwork is the same for everyone, so a CDN can serve one copy to millions. Your “Continue Watching” row is yours alone, and a shared cache serving it to the wrong person would be a privacy bug. Engineers often split a page into the shared parts, which get cached hard, and the personal parts, which are fetched fresh. A design that mixes the two in one block can make that split harder.
Launches are the third. The first visitors after a big change often get misses, because the caches hold nothing new yet, so the first minutes can be the slowest. Engineers sometimes warm the cache, filling it on purpose before traffic arrives, the way Netflix fills its servers overnight. If a launch moment matters, like a timed reveal, ask whether the caches will be ready for it. A ticket drop can hit the origin hard if the page that matters most can’t be cached.
The one thing people get wrong
“Put a cache in front of it” can sound like free speed. Turn it on, everything gets faster, done. But every cached copy is a snapshot of the past. Between a change at the origin and the copy expiring, people see something that’s no longer true. That gap is the stale window, and every cache has one.
The natural fix is to clear the copies the moment the original changes. That’s cache invalidation, often called a purge, and it’s famously hard. A line credited to the programmer Phil Karlton says there are only two hard things in computer science, and this is one. Copies live in many places at once, from hundreds of CDN edges to browsers you don’t control. Clearing all of them reliably, at the right moment, without throwing away copies that are still good, is a real engineering problem.
So the length of the stale window is a design question, not only an engineering one. A long window makes pages fast and cheap to serve, and a short one keeps them current. Artwork can be stale for a day and nobody minds. A price, a sale’s end time, or a promo code that has run out can’t be stale for a minute without someone being misled. Decide which values must stay current, the same call Day 12 asked you to make, and tell engineers before they pick expiry times.
Design has tools for the window, too. A label like “Updated two minutes ago” tells people how fresh a number is. Critical values can be checked again at the moment they matter. A checkout, for example, can confirm the price with the origin even when the product page was cached. A cart that says a price has changed since you added the item is the same idea: checking a saved copy against the original.
Ask an engineer this
“For this screen, which parts are cached, for how long, and what happens to those copies when the original changes?”
Ask about a screen that mixes content everyone shares with numbers that change, like a product page with prices and stock. Expect the engineer to name the layers involved and give rough expiry times for each piece. Then find out how a change at the origin reaches the caches: by waiting for expiry, by a purge, or by a new file address. Follow up with what happens if a purge fails. Together, those tell you how long your screen can be wrong, and whether that’s acceptable for what it shows.
Terms from today
- Cache
- A store of copies kept somewhere faster or closer than the original, so repeated requests can skip the slow trip.
- CDN
- A content delivery network, a set of servers in many locations that keeps copies of files close to the people who request them.
- Cache hit
- A request answered from a cached copy, without going back to the original source.
- Origin
- The server that holds the original version of a file or answer, the one every cache copies from.
- Cache invalidation
- Removing or replacing cached copies when the original changes, so people stop seeing the old version.