Skip to main content
CDN (Content Delivery Network)lesson 4 of 4 · 3 min read

Edge Compute

The awkward middle

That shared and personal split leaves an awkward middle: responses that are almost shared, differing only by a header tweak, a test bucket, or an auth check.

Count what round-tripping to your origin for a two-line decision costs. Two hundred milliseconds, to compute nothing.

Close the gap by running your code inside the location itself. Your function intercepts the request before the cache and can rewrite it, choose a response, or edit what goes back out.

Understand why these platforms look the way they do. A cold start has to be invisible at the edge, so one provider runs your code as lightweight isolates starting in under a millisecond, rather than containers taking hundreds.

Respect the constraints, which are real. Tight processor budgets per request, capped memory, no local disk. This is a place for decisions, not computation.

Put the right work there. Validating auth tokens, so bad ones bounce at the edge without touching your origin. Assigning test buckets, where the function hashes somebody and rewrites the request to fetch the right cached variant, keeping both variants cacheable.

Add routing and blocking by country, using what the location already knows, and any redirect or header change that would otherwise pay a full origin round trip.

State is the boundary

Respect state as the boundary. Your location holds your code and not your database, so any lookup either travels to a central store, giving back the latency you saved, or uses the edge-replicated storage.

Storage honestly: eventually consistent, with writes taking up to a minute to reach every location. Excellent for feature flags and redirect maps, wrong for a shopping cart.

Apply the same discipline as all caching. Decide which data tolerates being stale everywhere for seconds or minutes, push that to the edge, and keep everything else behind a single source of truth.

the shape of it
UserEdge functionunder 1 ms startEdge cacheOrigin1. request2. bucket A hit3. only if needed4. or decide here
step 1 of 4
Code at the edge answers the small decisions without a trip to the origin.

Worked example

Marta's streaming service gates video manifests by subscription tier, and the check used to hit origin: 180 ms from Sao Paulo, on every playlist refresh, every few seconds during playback. She moves it into a Cloudflare Worker. The JWT in each request already encodes the tier and expiry, so the worker verifies the signature with a public key stored in Workers KV, checks the tier claim, and either serves the manifest from the edge cache or returns a 403, all inside the PoP. Origin sees only token refreshes, about 2 percent of previous traffic on that path. Manifest latency in Brazil falls from 180 ms to 9 ms, and rebuffering complaints drop measurably. Key rotation is the one piece of ceremony: new public keys go into KV a day before use, since KV propagation is eventually consistent.

CDN (Content Delivery Network): wrapping up

In the real world

  • 01Netflix built its own CDN, Open Connect, placing storage appliances inside ISP networks and preloading popular titles during off-peak hours, so most streams never cross the public internet backbone.
  • 02Cloudflare operates 300 plus PoPs on anycast, meaning every location advertises the same IP addresses and BGP routing delivers each user to the nearest one, which also spreads DDoS traffic across the fleet.
  • 03Fastly's instant purge, roughly 150 ms globally, is central to how news sites like The Guardian cache full article pages while retaining the ability to correct a story immediately.
  • 04Shopify serves storefronts for millions of merchants through its CDN with short TTLs plus surrogate-key purging, so a merchant editing a product sees the change reflected within seconds.
  • 05The 2021 Fastly outage, where a customer config change triggered a latent bug, took down Reddit, Amazon, and gov.uk for about an hour, a reminder that a CDN is also a shared single point of failure.

Questions people ask

Does a CDN replace my application's Redis or memcached layer?

No, they cache different things at different distances. The CDN caches full HTTP responses near users and mostly helps shared, public content. Redis caches query results and objects next to your application servers and helps every request, including personalized ones. Mature systems run both: CDN for the shared outer layer, Redis for the per-request inner layer.

Push CDN or pull CDN, and when does the difference matter?

Pull is the default: edges fetch from origin on first request and cache, which fits most sites since only requested content occupies edge storage. Push means uploading content to the CDN ahead of demand, which suits large predictable files like video releases or game patches where the first user should not wait for an origin fetch. Many products, like Netflix preloading titles overnight, are push in spirit.

How do I cache pages when users can be logged in?

Split the response. Serve the parts identical for everyone, the page shell and content, from the CDN, and fetch personal fragments like the username and cart count with a separate uncached API call. Never let responses containing personal data or Set-Cookie headers into the shared cache; mark them private or no-store, and key the cache bypass off the session cookie.

Quick review

PoPs (Points of Presence):
200 to 300 edge locations globally. Cloudflare has 300+, Akamai has 4000+
Pull CDN:
first request fetches from origin and caches. Subsequent requests served from edge. Lazy
Push CDN:
proactively upload content to all edge nodes. Good for large, predictable files (video)
Cache-Control headers:
max-age (browser cache), s-maxage (CDN cache), must-revalidate, no-cache
Invalidation:
purge by URL, by tag (surrogate keys), or by prefix. Eventual consistency across edge nodes
Dynamic content acceleration:
CDN can also proxy API calls, optimize routing to origin, TLS termination at edge
Examples:
Cloudflare (free tier), AWS CloudFront, Fastly (API-first), Akamai (enterprise)
the trade-off

Stale content if invalidation is slow. Cache-busting (versioned URLs) is the standard workaround.

in the room

Any static asset (images, CSS, JS, video) served to global users. Also API caching for immutable responses.