{"canonical_url":"https://engineer.company/glossary/cdn/","content_markdown":"# CDN\n\n## What it is\n\nA CDN, a content delivery network, is a fleet of servers spread across many\ncities that stands between readers and the server a site really runs on, called\nthe origin, and hands out copies of what the origin publishes. A reader in\nSydney asking for a page whose origin sits in Toronto gets it from a machine in\nor near Sydney, and the request may never cross the Pacific at all.\n\nTwo mechanisms steer each reader to a nearby server. Some networks give a\ndifferent DNS answer depending on where the question comes from. Others\nannounce the same IP address from every location and let internet routing carry\neach packet to the closest one. Either way, the server that answers usually\nterminates TLS on the site's behalf, which means it holds a certificate for the\ndomain and reads each request in the clear before deciding what to do with it.\n\nWhat it does next is the job of a shared cache. If it already holds a fresh\ncopy, it answers at once, which is a hit. If not, it fetches the response from\nthe origin, keeps a copy and answers, which is a miss. How long a copy counts as\nfresh is the origin's decision, written in response headers that the HTTP\ncaching standard defines:\n\n```text\n# A fingerprinted stylesheet: never changes under this name.\nCache-Control: public, max-age=31536000, immutable\n\n# A page: browsers check back each time, the CDN keeps it ten minutes.\nCache-Control: no-cache\nCDN-Cache-Control: max-age=600\n\n# An account page: no cache may keep a copy.\nCache-Control: private, no-store\n```\n\n`max-age` gives a lifetime in seconds. `immutable` tells the browser not to\nrevalidate during that time. `no-cache` lets a cache store a response but makes\nit check with the origin before reusing it, while `private` keeps it out of\nshared caches altogether. `CDN-Cache-Control` is read only by CDNs, so a site\ncan let the edge hold a page longer than browsers do.\n\nThe same machinery now carries much more than static files. Most CDNs also\nproxy uncacheable requests to the origin, absorb floods of hostile traffic and\nrun small programs at the edge, which is why the term covers a broad range of\nservices that all start from the same idea of serving copies close to the\nreader.\n\n## Why it matters\n\nDistance costs time that no amount of server tuning can win back. Light in\nfibre covers roughly two hundred kilometres per millisecond, so a round trip\nbetween Europe and Australia takes well over a hundred milliseconds before any\nserver has done any work, and a new secure connection spends one or two of those\nround trips before the first request is sent. Serving from a nearby copy removes\nmost of that wait for every reader far from the origin.\n\nLoad is the second reason. When most requests are hits, the origin sees only a\nsmall share of the traffic, and a sudden spike lands on a network built for\nspikes rather than on one machine. With `stale-if-error` the edge can keep\nserving the last good copy while the origin is down, so a short outage stays\ninvisible to many readers.\n\nThe cost is less obvious than the invoice. A CDN in front of a site is another\ncompany in the middle of every visit. It sees each reader's IP address, every\nURL they request and, because it terminates TLS, the content of the exchange,\nand it handles all of it under its own policy. It is also a shared dependency:\nwhen a large provider has a bad hour, thousands of unrelated sites have it\ntogether.\n\nThe old argument for loading libraries and fonts from a popular public CDN has\nweakened too. Browsers now keep a separate cache for each site, so a file\nfetched for one site is fetched again for the next, and the shared copy brings\na third-party request with no cache benefit to show for it.\n\nA site whose readers sit mostly in one region, served from a fast machine in\nthat region, may simply not need a CDN. A site with a worldwide audience, heavy\nmedia or traffic that arrives in bursts usually does.\n\n## What it looks like in practice\n\nOn this site the CDN question was settled by trying one and switching it off.\nThe company's DNS work records that\n[proxying the two web faces through the provider's content network](/portfolio/reconciled-a-20-record-dns-zone-as-code/)\nwas built, worked, and was then turned back off.\nWith the proxy in place, another company handled every visitor's address and\nevery URL they asked for, before the site did and under that company's policy\nrather than this one's. For a business whose distinguishing claim is that\nnobody is watching, the page judges that a worse trade than the attacks the\nproxy defends against, and the reasoning is written down so that nobody\nreopens the question by accident.\n\nThe same position shows in what the pages load. The credits page names the five\nkinds of file that reach a reader's browser and explains why the typefaces come\nfrom the site's own machine:\n[a webfont from someone else's CDN is a third party watching you read](/credits/).\nCloudflare answers the site's DNS and does nothing more, the site is deliberately\nnot proxied, and a reader who opens the browser's network panel counts one host.\n\nWhere a CDN earns its place, the notes say so. The note on flash sales argues\nthat\n[turning a buyer away has to be the cheapest thing the system does](/notes/selling-limited-stock-without-overselling/).\nWith a million buyers and ten thousand units, 990,000 requests have to fail.\nAmong the ways it lists to shed them early are a waiting room, a gate sized to\nthe stock, a per-account rate limit and a `sold_out` flag that the CDN hands out\nwithout consulting anything behind it. Its advice on load testing makes the same\npoint from the other side: when the sold-out path is 99% of the traffic, that\ntraffic belongs at the edge.\n\n## Where it goes wrong\n\nThe edge does exactly what the headers and its configuration say, which is not\nalways what was meant.\n\n- Someone else's page is served to the next visitor. A response that shows an\n  account, a basket or a greeting is cached because nothing marked it private.\n  Setting a cookie does not stop caching, as the HTTP caching standard points\n  out, so the next reader gets the first reader's page. Mark every per-user\n  response `private` or `no-store`, and tell the CDN to bypass its cache when a\n  session cookie is present.\n- A deploy depends on a purge. New HTML goes live while some edges still hold\n  the old stylesheet, or the reverse, and pages render broken until every copy\n  expires. Purges take time to reach every location and vary between\n  providers. Put a hash of the content in each asset's file name, cache those\n  files for a year as `immutable`, and keep only the HTML short-lived.\n- The cache key is wrong in one direction or the other. Tracking parameters\n  such as `utm_source` turn every shared link into a separate cache entry, and\n  the hit ratio collapses. Leaving out a header that changes the response, such\n  as `Accept-Language`, serves one reader's language to everyone. Strip\n  parameters the origin ignores, and name in `Vary` the headers it reads.\n- A popular object expires and the origin is flooded. Thousands of readers miss\n  at the same instant and every one of those requests goes through. Request\n  collapsing at the CDN and `stale-while-revalidate`, which serves the old copy\n  while a single request fetches the new one, keep the origin's load flat.\n- The origin answers anyone who finds it. Protection at the edge means little\n  if attackers can reach the origin's address directly, and old DNS records,\n  mail headers or a forgotten subdomain often give it away. Accept traffic at\n  the origin only from the CDN, by network range or by an authenticated\n  connection.\n\n## Further reading\n\n- [RFC 6707, section 1.1: CDN terminology](https://www.rfc-editor.org/rfc/rfc6707#section-1.1)\n  — the IETF's definitions of a CDN, its surrogates and request routing.\n- [RFC 9111: HTTP Caching](https://www.rfc-editor.org/rfc/rfc9111) — what\n  shared caches may store, for how long, and what `private` means.\n- [RFC 9213: Targeted HTTP Cache Control](https://www.rfc-editor.org/rfc/rfc9213)\n  — the `CDN-Cache-Control` field that speaks only to CDNs.\n- [RFC 5861: HTTP Cache-Control Extensions for Stale Content](https://www.rfc-editor.org/rfc/rfc5861)\n  — `stale-while-revalidate` and `stale-if-error`.\n- [RFC 8246: HTTP Immutable Responses](https://www.rfc-editor.org/rfc/rfc8246)\n  — how a fingerprinted file avoids being checked again.\n\n\u003chttps://engineer.company/glossary/cdn/\u003e\n","date_modified":"2026-10-20T21:59:50Z","description":"How a CDN serves copies from servers near the reader, what it caches and for how long, what it sees on the way, and the mistakes that leak or go stale.","formats":{"gemtext":"https://engineer.company/glossary/cdn/index.gmi","gopher":"https://engineer.company/glossary/cdn/gophermap","html":"https://engineer.company/glossary/cdn/","json":"https://engineer.company/glossary/cdn/index.json","markdown":"https://engineer.company/glossary/cdn/index.md","pdf":"https://engineer.company/glossary/cdn/index.pdf"},"kind":"page","language":"en","title":"What is a CDN?","url":"https://engineer.company/glossary/cdn/","version":1}