SproutStackSproutStack home

CDN & Edge Caching

6 min readBeginner-friendlyFree to read

Before this, it helps to know: Caching Basics, DNS, HTTP, TCP in 10 Minutes.

Read a little, play a little. No scary maths, and no rush.

Share:

Why Mumbai shouldn't wait on Virginia

Your server sits in Virginia. A user in Mumbai requests your 2 MB hero image — that data crosses an ocean, ~200 milliseconds each way, for every visitor, forever. A CDN (Content Delivery Network) parks copies of that image in hundreds of edge cities, so Mumbai fetches from Mumbai in 20 milliseconds. Same bytes, ten times faster, and your origin server barely notices the traffic.

first missfirst missUser in MumbaiMumbai edge~20 ms awayOrigin serverVirginiaUser in LondonLondon edgenearby
Each edge location fetches a file from the origin once, then serves every nearby visitor from its own copy.

What belongs on a CDN

Static, hot, and shareable: images, JavaScript, CSS, fonts, product photos, even whole public pages. The rule: if every user sees the same bytes, it belongs at the edge. Personalized or fast-changing data stays on your servers.

The fingerprint trick

The puzzle: if browsers cache app.js for a year, how do users ever get the new version? Answer: never reuse a filename. Build tools hash file contents into the name — app.a1b2c3.js — so each deploy is literally a new file:

Cache-Control: public, max-age=31536000, immutable

Old file cached forever? Fine — nobody references it anymore. This beats "purge everything on deploy" (slow, error-prone) every time.

APIs at the edge (carefully)

API responses can use short TTLs (30–60 seconds) or stale-while-revalidate: serve the cached copy instantly while refreshing in the background. Users get speed, data stays roughly fresh, and your database survives the front page of Hacker News.

A worked example: launch day

Your project hits the front page and 100,000 people visit in an hour, each loading about 2 MB of images and scripts.

  • Without a CDN, your origin sends all of it: 100,000 × 2 MB = 200 GB in an hour, from one region, to the whole world. Expect slow pages, a large bandwidth bill, and possibly an outage.
  • With a CDN, each edge location fetches each file from your origin roughly once, then serves every nearby visitor from its own copy. The origin handles a few thousand requests instead of millions, and visitors get their files from a nearby city.

The share of requests served at the edge without contacting your origin is the cache hit ratio. For static files with fingerprinted names it is typically very high.

Cache keys, and the trap of caching the wrong thing

A CDN decides whether two requests are "the same" by a cache key, usually the URL. Extras can fragment it: if every visitor's request includes a unique query string or cookie that the key respects, every request becomes a miss.

The opposite mistake is worse. If a page personalized for a logged-in user is cached publicly, the next visitor can be served someone else's page, name and all. Responses that contain personal data must be marked Cache-Control: private or no-store, so only the user's own browser may keep them. Get this right before turning on edge caching for anything dynamic.

Running code at the edge

Many CDNs can also run small functions at each edge location: redirects, A/B test assignment, simple authentication checks, or adding security headers. Keep that logic small and fast. Anything that needs your database still belongs at the origin.

Remember this

  • Static + shared → CDN with fingerprinted names and far-future expiry.
  • Never purge-by-wildcard on deploy; version your filenames instead.

Check your understanding

5 questions · first-try answers earn the most XP

1. Why does the chapter recommend fingerprinted filenames over purging the CDN on every deploy?
2. An API response varies per user. Why can it still benefit from edge caching?
3. Which of these payloads belongs on a CDN according to the chapter's rule?
4. Why does putting a CDN in front of assets help a user in Mumbai when the origin is in Virginia?
5. What does the `immutable` cache directive actually tell a browser?

My notes

Saved in this browser. Highlight a line above and save it, or write it in your own words.

Nothing saved yet. Your highlights will live here.

References

Finished reading?

Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.

Practice this in an incident

Try a design drill

End of Caching and Delivery

Take the section checkpoint

A short mixed quiz on all 2 chapters, to check you are ready to move on.

Related chapters

Spotted a mistake or want a topic covered? Report an issue

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.