Frontend System Design · Lesson 26 of 86 · 5 min
What Is a CDN and How Does It Work?
This is a foundational topic. If you can't explain how a CDN works, you will not pass a frontend system design interview.
The Core Problem: Physics
Your web server has a physical location. Let's say it's in a data center in Virginia. When a user in Tokyo requests your JavaScript bundle, that request has to travel halfway around the world and back. The speed of light is the hard limit, and network hops add more delay. This round-trip time is latency, and it makes your application feel slow.
A Content Delivery Network (CDN) solves this by not having one location, but hundreds. It's a global network of proxy servers, called edge servers or Points of Presence (PoPs).
You upload your static assets (JS, CSS, images, fonts) to a central origin server (like an AWS S3 bucket). The CDN then automatically distributes and caches copies of these assets on its edge servers around the world. When the user in Tokyo requests your bundle, they don't hit Virginia. They hit a PoP in Tokyo.
The distance is cut from thousands of miles to tens. Latency plummets.
The Request Flow: Cache Hit vs. Cache Miss
In an interview, you'll be expected to walk through the lifecycle of a request. It's a simple branching path.
- A user's browser requests an asset, like
https://cdn.myapp.com/assets/main.1a2b3c.js. - The request goes to a DNS server. Your CDN provider uses techniques like Anycast routing to resolve
cdn.myapp.comnot to a single IP address, but to the IP of the edge server that is geographically and topologically closest to the user. - The request arrives at the nearest edge server (e.g., the one in Tokyo).
- The edge server checks its local cache for
main.1a2b3c.js.
This is the critical fork in the road.
A cache hit is the goal. The edge server has the file and serves it immediately. This is the fast path. Your CDN's cache hit ratio is a primary metric for its performance and cost-effectiveness.
A cache miss means the edge server doesn't have the file. It must now act as a proxy, forwarding the request to your origin server to fetch the asset. Once it receives the file, it adds it to its own cache for next time and then serves it to the user. This first request is slower, but it "warms up" the cache for all other users in that region.
How a CDN Knows What and How to Cache
The CDN doesn't guess. You tell it how to behave using HTTP headers sent from your origin server. The most important one is Cache-Control.
When your origin server provides a file to the CDN during a cache miss, it includes headers. A typical header for a static, versioned asset looks like this:
Cache-Control: public, max-age=31536000, immutable
public: This asset can be cached by any proxy (like a CDN).max-age=31536000: Cache this for 31,536,000 seconds (1 year).immutable: A strong signal that this file will never, ever change.
The trap: What happens when you need to deploy a new version of your main.js? If you just overwrite the file on the origin, the CDN's edge servers will happily keep serving the old, cached version for up to a year. Users won't get your update.
The solution is not to manually purge the CDN cache, which is slow and expensive. The standard practice is cache busting. Instead of naming your file main.js, you embed a hash of its contents into the filename, like main.1a2b3c.js.
When you change the file's content, the hash changes (main.4d5e6f.js). This becomes a brand new URL. The CDN has never seen it before, resulting in a guaranteed cache miss. The new file is fetched from the origin and cached. All references in your HTML are automatically updated by your build tool. This is the single most important concept for deploying frontend assets.
Don't cache the wrong thing. CDNs are for public, static assets. Never cache an HTML page containing a user's personal information or an API response with
privatedata. Misconfiguring cache rules can lead to serious data leaks. If it's not shareable among all users, it probably doesn't belong on an edge server cache.
Connecting Your Frontend Build to a CDN
You don't manually rename files with hashes. Your build tool, like Webpack or Vite, handles this. You configure it to prepare your assets for CDN delivery.
The key is setting the publicPath. This tells the build tool to prefix all generated asset URLs with your CDN's domain.
Here is a typical Webpack configuration for a production build.
// webpack.config.js
const path = require('path');
module.exports = {
mode: 'production',
entry: './src/index.js',
output: {
// Generate filenames with content hashes for cache busting.
filename: 'js/[name].[contenthash].js',
path: path.resolve(__dirname, 'dist'),
// This is the critical part for CDN integration.
publicPath: 'https://cdn.myapp.com/assets/',
clean: true,
},
// ... other configurations for loaders, plugins, etc.
};
When Webpack builds your HTML, it will inject script tags like:
<script src="https://cdn.myapp.com/assets/js/main.1a2b3c.js"></script>
Your deployment process becomes:
- Run the build (
npm run build). - Upload the contents of the
distdirectory to your origin (e.g., an S3 bucket). - The CDN is configured to use that bucket as its origin. Done.
The trap: Your application is served from www.myapp.com, but your assets are on cdn.myapp.com. This is a cross-origin request. If your CDN doesn't send the right Cross-Origin Resource Sharing (CORS) headers, the browser will block the request. You must configure your CDN (or origin) to send an Access-Control-Allow-Origin header, typically Access-Control-Allow-Origin: https://www.myapp.com.
Try it
Open the developer tools on a major website (e.g., Stripe, GitHub, The New York Times). Go to the Network tab and filter by JS, CSS, and Img. Inspect the URLs and response headers for a few assets. Identify which domain is the CDN, and look for the Cache-Control header to see their caching strategy in action.
Next: We'll quantify the benefits of this setup in CDN Benefits: Latency, Availability, Offload.
Lock it in — apply this lesson under interview conditions.