๐Ÿ“ Tutorials
ยท 4 min read
Last updated on

What is a CDN? A Simple Explanation for Developers


A CDN (Content Delivery Network) is a network of servers around the world that stores copies of your websiteโ€™s files and serves them from the location closest to each user.

Without a CDN, every user hits your single server โ€” which might be in Virginia. A user in Tokyo waits 200ms+ just for the data to travel there and back. With a CDN, that same user gets the files from a server in Tokyo in 20ms.

How it works

  1. You upload your site to your origin server (or deploy to Vercel, Netlify, etc.)
  2. The CDN copies your files to โ€œedgeโ€ servers worldwide (called PoPs โ€” Points of Presence)
  3. When a user visits your site, the CDN routes them to the nearest edge server
  4. The edge server returns the cached files โ€” fast

If the edge server doesnโ€™t have the file yet, it fetches it from your origin server, caches it, and serves it. Next request is instant.

What a CDN caches

  • Static files โ€” images, CSS, JavaScript, fonts, videos
  • HTML pages โ€” if your site is static (like Astro or Next.js with SSG)
  • API responses โ€” some CDNs can cache API responses too
CDNNotes
CloudflareFree tier, most popular, also does DNS and security
Vercel Edge NetworkBuilt into Vercel deployments
AWS CloudFrontAmazonโ€™s CDN, integrates with S3
FastlyUsed by GitHub, Stripe, Reddit
Bunny CDNCheap, simple, great performance

CDN vs hosting

Your hosting provider runs your origin server. A CDN sits in front of it:

User โ†’ CDN Edge (Tokyo) โ†’ [cache hit? return file]
                        โ†’ [cache miss? fetch from origin server in Virginia]

Most modern platforms (Vercel, Netlify, Cloudflare Pages) include a CDN automatically. If youโ€™re self-hosting, you add a CDN like Cloudflare in front of your server.

When you need a CDN

  • โœ… Your users are in multiple countries/regions
  • โœ… You serve large static files (images, videos)
  • โœ… You want faster page loads (better SEO, better UX)
  • โœ… You want DDoS protection (CDNs absorb traffic spikes)
  • โŒ You only have local users and a fast server nearby

Cache invalidation

The hardest part of using a CDN is cache invalidation โ€” telling edge servers to stop serving stale content when you update something.

Common strategies:

  • Cache-busting filenames โ€” append a hash to filenames (app.a3f2b1.js). When the file changes, the hash changes, so the CDN treats it as a new file. Build tools like Vite and Webpack do this automatically.
  • TTL (Time to Live) โ€” set how long the CDN should cache a file before checking the origin for updates. Short TTLs mean fresher content but more origin hits.
  • Purge API โ€” most CDNs let you manually invalidate specific URLs or entire cache zones via API when you deploy.
Cache-Control: public, max-age=31536000, immutable  # Static assets with hash
Cache-Control: public, max-age=60, s-maxage=300     # HTML pages, short cache

CDN security features

Modern CDNs do more than just caching:

  • DDoS protection โ€” absorb massive traffic spikes before they reach your server
  • WAF (Web Application Firewall) โ€” block malicious requests like SQL injection attempts
  • Bot detection โ€” distinguish real users from scrapers and bots
  • SSL/TLS termination โ€” handle HTTPS encryption at the edge so your origin doesnโ€™t have to

Cloudflareโ€™s free tier alone blocks billions of threats daily, which is why many developers put it in front of every project regardless of traffic levels.

Setting up Cloudflare (most common)

  1. Sign up at cloudflare.com
  2. Add your domain
  3. Change your nameservers to Cloudflareโ€™s
  4. Cloudflare automatically proxies and caches your traffic

Thatโ€™s it. No code changes needed. Your static assets are now served from 300+ locations worldwide.

FAQ

Whatโ€™s the difference between a CDN and edge computing?

A CDN primarily caches and serves static content (files, images, HTML) from distributed servers. Edge computing goes further by running dynamic code (authentication, personalization, API logic) at those distributed locations. Many CDN providers now offer both โ€” Cloudflare Workers is a CDN that also does edge computing.

Do I need a CDN if Iโ€™m using Vercel or Netlify?

No, these platforms include a CDN automatically. When you deploy to Vercel or Netlify, your static assets are already distributed globally across their edge network. You only need to set up a separate CDN if youโ€™re self-hosting on a VPS or bare-metal server.

Can a CDN cache API responses?

Yes, but you need to be careful. CDNs can cache GET requests that return the same data for all users (public data, product listings, blog posts). You should never cache personalized or authenticated responses. Use Cache-Control headers to tell the CDN which responses are safe to cache and for how long.

Related: What is CORS? A Simple Explanation for Developers

Related: What is DNS? A Simple Explanation for Developers