HTTP, TLS & Web Performance

How a CDN Speeds Up a Website

See how a content delivery network shortens the distance to visitors, what it can cache, and how to set one up without surprises.

Key takeaways

  • A CDN serves cached copies from a location near the visitor, which cuts round-trip time and spares your origin server.
  • Even uncached requests can benefit, because the secure connection ends nearby and the CDN reuses connections to your origin.
  • What gets cached depends on your headers and the CDN's rules, and HTML is often not cached unless you ask for it.
  • The cache key decides which requests share a stored copy, so a careless key causes either poor hit rates or leaked content.
  • Your origin still needs protection, since attackers who find its address can bypass the network.

A content delivery network, or CDN, is a set of servers spread across many locations that sits between your visitors and your own server. It speeds up a site mainly in two ways: it keeps copies of your content close to people, and it handles connection setup nearby so less of the conversation has to cross long distances.

It can also absorb traffic spikes and reduce the load on your server. What it can't do is make slow application code fast on requests it has to pass straight through.

Distance is the problem

Data in optical fiber travels at roughly two thirds of the speed of light in a vacuum. That sounds instant, but loading a page takes many exchanges: a name lookup, a connection, a TLS setup, a request, and often several more in sequence. A server on another continent adds a noticeable delay to each.

No bandwidth upgrade removes that delay, as explained in latency and the speed of light. Moving the other end closer does.

A CDN's locations are called points of presence or edge locations. Your own server is the origin.

How visitors reach a nearby location

You usually point your hostname at the CDN, often with a CNAME record or by letting the provider run your DNS. Two techniques then steer people to a suitable site:

  • Anycast. Many locations announce the same IP address, and internet routing delivers each visitor to one that is close in network terms.
  • DNS-based steering. The CDN's name servers answer with different addresses depending on where the query seems to come from.

"Close" refers to network paths, not map distance, so a visitor occasionally lands somewhere farther than you'd expect.

A cache miss, then a hit
First visitor Edge location Origin server Next visitor Request an image 1 Not stored yet, so ask the origin 2 Image with caching headers 3 Deliver and keep a copy 4 Request the same image 5 Serve the stored copy 6 The origin is not contacted First visitor Edge location Origin server Next visitor Request an image 1 Not stored yet, so ask the origin 2 Image with caching headers 3 Deliver and keep a copy 4 Request the same image 5 Serve the stored copy 6 The origin is not contacted

Where the speed comes from

Cached content. A hit is answered from the edge. Images, scripts, stylesheets, fonts, downloads and video segments are the usual candidates.

Nearby connection setup. The TCP or QUIC handshake and TLS negotiation happen over a short path. The edge then forwards requests to your origin over connections it keeps open, avoiding a fresh long-distance handshake each time. This helps even personalized pages.

Modern protocols and features. Many CDNs offer HTTP/2 and HTTP/3 to visitors even if your origin speaks only HTTP/1.1. Compression and image resizing are common additions. See HTTP/1.1 vs. HTTP/2 vs. HTTP/3.

Capacity. A popular file is spread across a large network instead of squeezing through your server's uplink.

What gets cached

A CDN is a shared HTTP cache with extra controls. It generally respects Cache-Control, where s-maxage lets you give the edge a different lifetime from browsers. Some providers also have their own headers or rule settings that override yours.

Defaults differ. Many cache static file types automatically and leave HTML alone. Caching HTML can make the biggest difference to first-byte time, but you have to be sure the page is the same for everyone, or separate the versions correctly.

Should the edge cache it?
Static assets Yes. Version the filenames and cache for a long time. Public pages Often yes, for a short time with a purge plan. Personalized pages Not as shared content. Pass through to origin. Writes such as POST No. These must reach your application. Static assets Yes. Version the filenames and cache for a long time. Public pages Often yes, for a short time with a purge plan. Personalized pages Not as shared content. Pass through to origin. Writes such as POST No. These must reach your application.

The cache key

The cache key is the set of request properties that identifies a stored copy. Typically it includes the scheme, host, path and query string.

If irrelevant parameters are part of the key, such as campaign tracking tags, each variation misses the cache even though the content is identical. If a relevant property is left out, such as a language cookie that changes the page, visitors can receive someone else's version. Configure the key deliberately and keep it as small as correctness allows.

Many networks add a tiered cache or origin shield: edge locations ask an intermediate cache before the origin, so one miss somewhere doesn't become hundreds of origin requests.

Misses, collapsing and long tails

Not every request for cacheable content is a hit. Each location builds its cache from the requests it actually receives, and rarely requested items may be evicted to make room. A site with a huge catalog and light traffic will see many misses at the edge, which is where a shield layer helps most.

When a popular item expires, many visitors may ask for it in the same instant. Request collapsing lets the CDN send one fetch to your origin and make the others wait for the result. Combined with stale-while-revalidate, where supported, visitors keep receiving the previous copy while the new one is retrieved.

Large files and video benefit in a particular way. Streaming services cut video into short segments that each cache like ordinary files, so a live event watched by many people puts little repeated load on the source. Range requests let a cache serve parts of a large download.

You can also arrange for the edge to keep serving an old copy when the origin returns errors. That turns a brief server failure into slightly stale pages instead of an error screen for public content. It won't help sign-ins, checkouts or anything else that has to reach your application.

Setting one up

  1. List what you serve and decide on a lifetime for each type.
  2. Set headers at the origin, following Cache-Control practice.
  3. Configure TLS on both legs: visitor to edge, and edge to origin. Encrypt and verify the second one too.
  4. Test on a spare hostname before moving your main one.
  5. Lower your DNS TTL ahead of the switch so you can back out quickly.
  6. Pass the real client address to your application through the provider's documented header, and trust it only from the provider's addresses.
  7. Restrict the origin so it accepts web traffic only from the CDN, or requires a secret or client certificate from it.

Checking whether it helps

Look at response headers:

curl -I https://www.example.com/assets/photo.webp

Many providers add a header that reports a hit or miss, though its name varies. An Age header also suggests the response came from a cache. The first request to a location is often a miss, so repeat it.

The cache hit ratio is the share of requests or bytes served without going to the origin. A low figure usually points to short lifetimes, Set-Cookie headers on cacheable responses, noisy query strings or a broad Vary.

Measure real visitors before and after. If your audience is mostly in the same city as your server and your pages are uncacheable, the gain may be small. For finding where time goes, run a performance audit.

When it helps most and least

Situation Expected benefit
Image-heavy site with a widespread audience Large
Static site or documentation Large, since nearly everything caches
News or product pages that change occasionally Good, with short lifetimes and purging
Dashboard where every response is personal Modest, from connection handling and assets
Local audience, server already nearby Small for speed, still useful for spikes

A CDN also does nothing about front-end weight. If a page ships a very large script bundle, delivering it from nearby shortens the download, but the visitor's phone still has to run it. Treat the network as one tool alongside smaller files, sensible image formats and less code.

Likewise, if a pass-through page takes two seconds to generate in your application, the edge will faithfully deliver a response that took two seconds. Fix the slow queries, or find a way to cache the page safely.

Trade-offs and risks

A CDN adds another company to your delivery path. Its outages can affect you, its configuration is something new to get wrong, and in the usual setup it decrypts your traffic in order to cache and inspect it. Check its data handling terms if you carry sensitive information.

Costs vary widely, from free tiers to usage-based billing. Check current pricing, including charges for requests, transfer and extra features.

Keeping an eye on it

After launch, watch the provider's reports for hit ratio, error rates by status code and the volume reaching your origin. A sudden fall in hits after a release usually means a header changed or a new query parameter appeared on every link. Compare the CDN's logs with your server's when something looks odd: a request that appears at the edge and never at the origin was answered from cache or blocked, while one that appears in both with an error points back to your application.

What can go wrong

  • Updates don't appear. Plan purging and versioned URLs, described in cache invalidation.
  • Redirect loops. The edge talks plain HTTP to an origin that redirects to HTTPS. Use HTTPS on both legs.
  • Every visitor looks like the CDN. Logs and rate limits need the forwarded client address.
  • Gateway errors. The edge can't reach your origin, often because a firewall blocks its address ranges or a certificate has expired.
  • Private pages cached. Treat any rule that says "cache everything" with great care.

Common questions

Is a CDN the same as web hosting?

No. Hosting runs your origin. A CDN accelerates and protects delivery in front of it. Some platforms bundle both, especially for static sites.

Does a small site need one?

It helps if your visitors are spread out, your files are large or you expect bursts. Many hosts include one already. Providers can also filter hostile traffic, a topic covered in web application firewalls, and run code at their locations, covered in edge computing.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in HTTP, TLS & Web Performance →

Latest news on this topic