{"course":{"slug":"cloud-and-shipping","title":"Shipping It","url":"https://addaly.com/learn/cloud-and-shipping","level":"intermediate","track":"build"},"lesson":{"slug":"the-edge-and-the-cdn","title":"Serving from near the user, and what bandwidth costs","url":"https://addaly.com/learn/cloud-and-shipping/the-edge-and-the-cdn","position":31,"of":74,"minutes":9,"answer":"A CDN wins twice (cached bytes never cross the ocean, and even uncacheable requests get a nearby handshake) and the number to watch is cache hit ratio.","keyIdea":"A CDN wins twice (cached bytes never cross the ocean, and even uncacheable requests get a nearby handshake) and the number to watch is cache hit ratio, because egress metered at cents per gigabyte is what turns one large image into a bill."},"body":{"format":"markdown","text":"## Distance is the part you cannot optimise away\n\nYour server is in one place. Your users are not. A request from Mumbai to a machine in Virginia crosses roughly 13,000 km of fibre, and light in glass manages about 200,000 km/s, so the round trip cannot be faster than about 130 ms; and in practice, with routing, it is 200 to 280 ms. Nothing you write in your handler changes that number.\n\nA content delivery network answers from a machine near the user instead. There are two separate wins in that, and people conflate them:\n\n1. **The cached response never travels far.** An image served from a point of presence in Mumbai arrives in 20 ms rather than 250 ms.\n2. **Even uncached requests get faster**, because the expensive TCP and TLS handshakes complete against the nearby edge, which then reuses a warm, long-lived connection back to your origin. Dynamic HTML that cannot be cached at all still improves, often by 100 ms or more.\n\nThe second point is why putting a CDN in front of a fully dynamic site is still worth doing.\n\n## Anycast, in one paragraph\n\nA CDN announces the same IP address from hundreds of locations, and the internet's routing sends each user to the nearest one. That is anycast. It is also why CDNs absorb volumetric attacks well: a flood aimed at one address is spread across every location that answers for it, instead of arriving at your single machine.\n\n## The number that tells you it is working\n\nCache hit ratio. If 95% of requests are answered at the edge, your origin sees one request in twenty and your bill and your CPU both reflect it. If it is 40%, something is wrong, and it is nearly always one of three things:\n\n- A `Set-Cookie` header on responses that should be anonymous. Most CDNs refuse to cache a response carrying one, which silently disables caching for your whole site.\n- `Cache-Control: private` or `no-store` applied globally by a framework default.\n- Query strings that vary per user: a tracking parameter appended to every link makes every URL unique.\n\nCheck the response headers your origin actually sends, not the ones you believe it sends:\n\n```bash\ncurl -sI https://example.com/logo.svg | grep -i -E 'cache-control|cf-cache-status|age|set-cookie'\n```\n\n`Age` tells you how long the edge has held the copy. A status header naming a hit or a miss is provided by most CDNs, and watching it change as you reload is the fastest way to learn your own configuration.\n\n## Bandwidth is where the money is\n\nCompute costs are predictable. Egress (data leaving a provider's network) is the line that surprises people, and the prices differ by more than an order of magnitude between vendors.\n\nAs a rough shape at the time of writing: the large clouds charge in the region of 8 to 12 US cents per gigabyte out to the internet, with the first 100 GB or so free each month. Several CDN and object-storage products charge nothing at all for egress to the internet as a deliberate competitive position, and some VPS providers bundle terabytes into a flat monthly fee.\n\nThe arithmetic that matters: one 2 MB hero image on a page viewed a million times a month is 2 TB of egress. On a metered provider that is a few hundred dollars for one image. Behind a CDN with a 95% hit rate and free egress, it is close to nothing.\n\nSo the order of operations for bandwidth cost is: make the assets smaller first (a WebP or AVIF version of that image is often 200 KB), then serve them from an edge that does not meter egress, then worry about the origin.\n\n> Figure: One hero image, one million views a month. Bars (US dollars a month for that one image): 2 MB original, metered egress 180; Same image as 200 KB WebP 18; Behind a CDN, 95% hit rate, metered origin 9; CDN with free egress 0. Two terabytes leaves a metered provider at about nine cents a gigabyte. Shrink the file first, then serve it from an edge that does not meter egress, then worry about the origin.\n\n## What a CDN does not fix\n\n- **Personalised HTML.** A page that says \"Hello, Priya\" cannot be shared between users. Either split it (cacheable shell, personalised fragment fetched separately) or accept an origin hit.\n- **Writes.** A POST goes to the origin, every time, from wherever the user is. Sign-up forms and checkouts feel the distance even on a heavily cached site.\n- **A slow origin.** A cache miss is your normal latency plus one extra hop.\n- **Correctness.** A CDN will faithfully serve a wrong response to the whole world for as long as you told it to. The cache-key and private-data warnings from the caching lesson apply with a much larger audience.\n\n## Edge compute\n\nSeveral providers let you run small pieces of code at the edge: Cloudflare Workers, Fastly Compute, Deno Deploy, Vercel and Netlify edge functions. It is useful for redirects, authentication checks, A/B splits, header rewriting and serving a cached page while revalidating behind it.\n\nThe constraint people meet is data. Your database is still in one region, so an edge function that queries it has crossed the ocean anyway and gained nothing. Edge code pays off when it can answer from the request itself or from edge-local storage; otherwise it is a slower place to run the same query.\n\n## The free path\n\nCloudflare's free plan gives anycast, TLS, HTTP/3, caching and basic bot protection at no cost, which is the single largest free upgrade available to a small site. BunnyCDN and Fastly are cheap alternatives. If you self-host, a nginx or Varnish cache in front of your app on the same machine gets you the caching but none of the geography, worth knowing, because it is often mistaken for having a CDN."},"check":{"question":"After putting a CDN in front of a site, the cache hit ratio sits at 30% even for static images, and the origin still handles most traffic. Which cause fits best?","options":[{"text":"The CDN only caches responses smaller than a fixed size, and the images exceed it","correct":false,"tempting":"Size limits exist for very large objects, but ordinary images are well under them; this would not affect the whole static set."},{"text":"Anycast is routing users to different points of presence, so each must fetch its own copy","correct":false,"tempting":"Each location does warm separately, which lowers the ratio briefly, but it converges quickly and cannot hold the ratio at 30% indefinitely."},{"text":"The application sets a session cookie on every response, so the CDN treats them as private and refuses to cache","correct":true},{"text":"HTTP/3 is enabled, and QUIC connections bypass the edge cache","correct":false,"tempting":"Confuses transport protocol with caching; the protocol carrying the bytes has no bearing on whether a response is cacheable."}]},"prev":{"slug":"caching-the-cheapest-server","title":"The request you never serve","url":"https://addaly.com/learn/cloud-and-shipping/caching-the-cheapest-server"},"next":{"slug":"where-it-falls-over","title":"Finding your ceiling before your users do","url":"https://addaly.com/learn/cloud-and-shipping/where-it-falls-over"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Exam keys checked blind by a second model: 3,665 of 3,666 agreed. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Serving from near the user, and what bandwidth costs', in Shipping It. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/cloud-and-shipping/the-edge-and-the-cdn (Accessed: 13 October 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Serving from near the user, and what bandwidth costs","courseTitle":"Shipping It","module":{"index":4,"slug":"load-cost-and-abuse","title":"Load, cost and the traffic that is not customers"},"answer":null,"keyIdea":"A CDN wins twice (cached bytes never cross the ocean, and even uncacheable requests get a nearby handshake) and the number to watch is cache hit ratio, because egress metered at cents per gigabyte is what turns one large image into a bill.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure","figure":{"type":"bars","unit":"US dollars a month for that one image","title":"One hero image, one million views a month","series":[{"label":"2 MB original, metered egress","value":180},{"label":"Same image as 200 KB WebP","value":18},{"label":"Behind a CDN, 95% hit rate, metered origin","value":9},{"label":"CDN with free egress","value":0,"highlight":true}],"caption":"Two terabytes leaves a metered provider at about nine cents a gigabyte. Shrink the file first, then serve it from an edge that does not meter egress, then worry about the origin."},"alt":"Figure: One hero image, one million views a month. Bars (US dollars a month for that one image): 2 MB original, metered egress 180; Same image as 200 KB WebP 18; Behind a CDN, 95% hit rate, metered origin 9; CDN with free egress 0. Two terabytes leaves a metered provider at about nine cents a gigabyte. Shrink the file first, then serve it from an edge that does not meter egress, then worry about the origin."}},"blocks":[{"t":"h2","id":"distance-is-the-part-you-cannot-optimise-away","runs":[{"text":"Distance is the part you cannot optimise away"}]},{"t":"p","runs":[{"text":"Your server is in one place. Your users are not. A request from Mumbai to a machine in Virginia crosses roughly 13,000 km of fibre, and light in glass manages about 200,000 km/s, so the round trip cannot be faster than about 130 ms; and in practice, with routing, it is 200 to 280 ms. Nothing you write in your handler changes that number."}]},{"t":"p","runs":[{"text":"A content delivery network answers from a machine near the user instead. There are two separate wins in that, and people conflate them:"}]},{"t":"ol","start":1,"items":[[{"text":"The cached response never travels far.","b":true},{"text":" An image served from a point of presence in Mumbai arrives in 20 ms rather than 250 ms."}],[{"text":"Even uncached requests get faster","b":true},{"text":", because the expensive TCP and TLS handshakes complete against the nearby edge, which then reuses a warm, long-lived connection back to your origin. Dynamic HTML that cannot be cached at all still improves, often by 100 ms or more."}]]},{"t":"p","runs":[{"text":"The second point is why putting a CDN in front of a fully dynamic site is still worth doing."}]},{"t":"h2","id":"anycast-in-one-paragraph","runs":[{"text":"Anycast, in one paragraph"}]},{"t":"p","runs":[{"text":"A CDN announces the same IP address from hundreds of locations, and the internet’s routing sends each user to the nearest one. That is anycast. It is also why CDNs absorb volumetric attacks well: a flood aimed at one address is spread across every location that answers for it, instead of arriving at your single machine."}]},{"t":"h2","id":"the-number-that-tells-you-it-is-working","runs":[{"text":"The number that tells you it is working"}]},{"t":"p","runs":[{"text":"Cache hit ratio. If 95% of requests are answered at the edge, your origin sees one request in twenty and your bill and your CPU both reflect it. If it is 40%, something is wrong, and it is nearly always one of three things:"}]},{"t":"ul","items":[[{"text":"A "},{"text":"Set-Cookie","code":true},{"text":" header on responses that should be anonymous. Most CDNs refuse to cache a response carrying one, which silently disables caching for your whole site."}],[{"text":"Cache-Control: private","code":true},{"text":" or "},{"text":"no-store","code":true},{"text":" applied globally by a framework default."}],[{"text":"Query strings that vary per user: a tracking parameter appended to every link makes every URL unique."}]]},{"t":"p","runs":[{"text":"Check the response headers your origin actually sends, not the ones you believe it sends:"}]},{"t":"code","lang":"bash","label":"Bash","text":"curl -sI https://example.com/logo.svg | grep -i -E 'cache-control|cf-cache-status|age|set-cookie'"},{"t":"p","runs":[{"text":"Age","code":true},{"text":" tells you how long the edge has held the copy. A status header naming a hit or a miss is provided by most CDNs, and watching it change as you reload is the fastest way to learn your own configuration."}]},{"t":"h2","id":"bandwidth-is-where-the-money-is","runs":[{"text":"Bandwidth is where the money is"}]},{"t":"p","runs":[{"text":"Compute costs are predictable. Egress (data leaving a provider’s network) is the line that surprises people, and the prices differ by more than an order of magnitude between vendors."}]},{"t":"p","runs":[{"text":"As a rough shape at the time of writing: the large clouds charge in the region of 8 to 12 US cents per gigabyte out to the internet, with the first 100 GB or so free each month. Several CDN and object-storage products charge nothing at all for egress to the internet as a deliberate competitive position, and some VPS providers bundle terabytes into a flat monthly fee."}]},{"t":"p","runs":[{"text":"The arithmetic that matters: one 2 MB hero image on a page viewed a million times a month is 2 TB of egress. On a metered provider that is a few hundred dollars for one image. Behind a CDN with a 95% hit rate and free egress, it is close to nothing."}]},{"t":"p","runs":[{"text":"So the order of operations for bandwidth cost is: make the assets smaller first (a WebP or AVIF version of that image is often 200 KB), then serve them from an edge that does not meter egress, then worry about the origin."}]},{"t":"h2","id":"what-a-cdn-does-not-fix","runs":[{"text":"What a CDN does not fix"}]},{"t":"ul","items":[[{"text":"Personalised HTML.","b":true},{"text":" A page that says “Hello, Priya” cannot be shared between users. Either split it (cacheable shell, personalised fragment fetched separately) or accept an origin hit."}],[{"text":"Writes.","b":true},{"text":" A POST goes to the origin, every time, from wherever the user is. Sign-up forms and checkouts feel the distance even on a heavily cached site."}],[{"text":"A slow origin.","b":true},{"text":" A cache miss is your normal latency plus one extra hop."}],[{"text":"Correctness.","b":true},{"text":" A CDN will faithfully serve a wrong response to the whole world for as long as you told it to. The cache-key and private-data warnings from the caching lesson apply with a much larger audience."}]]},{"t":"h2","id":"edge-compute","runs":[{"text":"Edge compute"}]},{"t":"p","runs":[{"text":"Several providers let you run small pieces of code at the edge: Cloudflare Workers, Fastly Compute, Deno Deploy, Vercel and Netlify edge functions. It is useful for redirects, authentication checks, A/B splits, header rewriting and serving a cached page while revalidating behind it."}]},{"t":"p","runs":[{"text":"The constraint people meet is data. Your database is still in one region, so an edge function that queries it has crossed the ocean anyway and gained nothing. Edge code pays off when it can answer from the request itself or from edge-local storage; otherwise it is a slower place to run the same query."}]},{"t":"h2","id":"the-free-path","runs":[{"text":"The free path"}],"role":"free"},{"t":"p","runs":[{"text":"Cloudflare’s free plan gives anycast, TLS, HTTP/3, caching and basic bot protection at no cost, which is the single largest free upgrade available to a small site. BunnyCDN and Fastly are cheap alternatives. If you self-host, a nginx or Varnish cache in front of your app on the same machine gets you the caching but none of the geography, worth knowing, because it is often mistaken for having a CDN."}]}],"check":{"question":[{"text":"After putting a CDN in front of a site, the cache hit ratio sits at 30% even for static images, and the origin still handles most traffic. Which cause fits best?"}],"options":[{"text":[{"text":"The CDN only caches responses smaller than a fixed size, and the images exceed it"}],"correct":false,"tempting":[{"text":"Size limits exist for very large objects, but ordinary images are well under them; this would not affect the whole static set."}]},{"text":[{"text":"Anycast is routing users to different points of presence, so each must fetch its own copy"}],"correct":false,"tempting":[{"text":"Each location does warm separately, which lowers the ratio briefly, but it converges quickly and cannot hold the ratio at 30% indefinitely."}]},{"text":[{"text":"The application sets a session cookie on every response, so the CDN treats them as private and refuses to cache"}],"correct":true,"tempting":null},{"text":[{"text":"HTTP/3 is enabled, and QUIC connections bypass the edge cache"}],"correct":false,"tempting":[{"text":"Confuses transport protocol with caching; the protocol carrying the bytes has no bearing on whether a response is cacheable."}]}],"order":[3,1,2,0],"why":null},"prev":{"slug":"caching-the-cheapest-server","title":"The request you never serve","minutes":9},"next":{"slug":"where-it-falls-over","title":"Finding your ceiling before your users do","minutes":10}}}