CDN

A network of servers spread around the world. Each one holds a copy of your site so users always hit one nearby.

Distance is the problem

Imagine your only server sits in a data center in Virginia. A user in Mumbai opens your homepage. Their request has to travel from Mumbai to London to New York to Virginia. The reply comes back the same way. That round trip alone is about 250 milliseconds. And that is just physics. You cannot make light travel faster.

That delay happens on every single page load. Every image. Every API call. If your homepage needs 50 different things to render, your user is sitting there for several seconds before they see anything.

You cannot beat the speed of light. But you can do the next best thing. Put copies of your content closer to the user.

A CDN puts servers everywhere

A CDN, short for Content Delivery Network, is a global system of servers spread across dozens or even hundreds of locations. Each location is called an "edge" or a "point of presence."

When a user opens your site, the DNS lookup is set up so it returns the address of the nearest edge, not your faraway origin server. That edge might be just 5 to 20 milliseconds away, instead of 250.

If the edge already has the content cached from a previous request, it serves it directly. If not, it fetches the content from your origin once, caches it, and serves the response. The next request from someone nearby is now served straight from the edge.

The biggest CDN providers are Cloudflare, Akamai, AWS CloudFront, and Fastly. They run edge servers in every major city around the world.

What can and cannot be cached

CDNs are great for static assets. Things that look the same for every user. Images. CSS files. JavaScript. Fonts. Video files. These can be cached at the edge for hours or days. A single Cloudflare edge in Tokyo can serve the same logo image to a million different users without ever bothering your origin.

Dynamic content is trickier. You cannot cache "Alice's home feed" at every edge because every user has a different feed. But you can cache for short windows. Even 10 seconds or 1 minute of caching can reduce origin load by a huge amount when you are getting lots of traffic.

Modern CDNs also let you run code at the edge. This is called edge functions. Cloudflare has Cloudflare Workers, AWS has Lambda@Edge, Fastly has Compute@Edge. You can do things like personalize responses or run A/B tests at the edge, without ever needing to hit your origin server.

How users find the closest edge

Two main techniques are used to send each user to the nearest CDN edge.

The first is DNS-based routing. The DNS server returns a different IP address depending on where the lookup came from. A user in Tokyo gets the address of the Tokyo edge. A user in São Paulo gets São Paulo. The DNS provider knows roughly where the user is based on which DNS server they used.

The second is Anycast. Every CDN edge advertises the same IP address on the internet. The internet's routers naturally deliver the packet to the topologically closest edge. The user does not know or care. They just send packets to one IP and the network puts them in the right place.

Cloudflare uses Anycast. AWS CloudFront uses DNS-based. Either way, the user does not have to do anything. They just open your site and packets find the closest copy.

You do not manage any of this manually. You sign up for a CDN, point your domain's DNS at them, and they handle the routing.

Now build it yourself →