Below the Fold

A designer’s path to creative technologist. Thirty days, ten minutes each, on how software actually gets made.

Copies around the globe A globe with pins marking copies of the same files stored around the world, the one nearest you in red.

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 cache hit and a cache missTwo paths. On a cache hit, the app asks the cache, which has a copy and answers directly. On a cache miss, the app asks the cache, which has no copy, so the request goes on to the origin; the answer comes back through the cache, which keeps a copy. Below, two bars compare illustrative times: about 20 milliseconds for the hit and about 220 for the miss.Cache hitIllustrative numbersCache missAppCacheAppCacheOriginHas a copyNo copy yetThe originalKeeps a copy for next timeHitMiss20 ms220 ms
Figure 1. The same request answered from a nearby copy, and from the origin, with how long each takes. Look at the hit. A copy close by answers in a fraction of the time, which is the whole reason caches exist.

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.

An origin and three edge serversOne origin at the top holds the original files. Dashed lines copy those files ahead of time, overnight, to three edge servers near Lisbon, Chicago, and Tokyo. Viewers below each edge server get video from the edge nearest them; one of them is you.OriginEdge serverEdge serverEdge serverThe original filesNear LisbonNear ChicagoNear TokyoCopied ahead, overnightYouViewersViewers
Figure 2. A simplified content delivery network: one origin, and edge servers near the people watching. Look at the edge server near you. When you press play, the video comes from there, and the origin isn’t involved at all.

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.

Where an old copy can hideFour stacked layers, from top to bottom: your browser, a CDN edge, a server cache, and the origin. Beside each is what clears it: a hard refresh for the browser, expiry or a purge for the CDN edge, expiry or an engineer for the server cache, and nothing for the origin, which holds the original.Where an old copy can liveWhat clears itYour browserCDN edgeServer cacheOriginFiles you already loadedCopies near youAnswers the backend savedThe originalA hard refreshExpiry, or a purgeExpiry, or an engineerNothing to clear
Figure 3. Four places an old copy of a page or file can live, and what clears each one. Look at your browser. It’s the only cache a refresh on your own machine can clear, and every layer below it needs time or an engineer.

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 stale windowA timeline from a few minutes before 9:00 to 9:15. On the origin, the old price changes to the new price at 9:00. In the cache, the old price stays until 9:10, when the cached copy expires. The ten minutes between are marked as the window when people still see the old price.Price changesCache expires9:009:10OriginCacheOld priceNew priceOld priceNew pricePeople still see the old priceIllustrative timing
Figure 4. A price changes at the origin while a cached page keeps showing the old one for ten minutes. Look at the stale window. Between the change and the cache expiring, everyone served from the cache sees the old price.

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.
Next

Day 20: Third-party services

Payments, email, maps, and search as rented services, and the work that stays with your team after you plug one in.

19

Caching and CDNs

All 30 days