CDN & Edge Computing — System Design

A CDN keeps copies of content at PoPs (Points of Presence), CDN server locations in cities near the users, so latency follows the distance to the PoP, not to…

TicketTune's server is in Virginia, USA. A round trip from Kuala Lumpur takes 230 ms. Yet when Budi in Kuala Lumpur opens the Archipelago Concert poster that someone else has already opened, it shows up in 8 ms. The poster isn't coming from Virginia.

It's like a bakery in one city. Branch shops in other cities keep the best-selling bread in stock, so buyers don't wait for bread to come from the bakery. Stock at a branch has an expiry date, and it can be pulled if the recipe changes. A birthday cake ordered in one person's name is still made at the bakery. The branch cashier can also handle small things alone, like checking a coupon, without phoning the bakery.

Those branch shops are a CDN (Content Delivery Network): a network of servers in many cities that keeps copies of your site's content close to its users. When those servers also run code, not just serve files, it's called edge computing.

Terms: origin, PoP, edge

Latency follows distance

A signal in fibre travels at most about 200 km per millisecond, and the route is never straight. The further away, the longer it takes, and no code optimisation can remove that. The Lab's example model, round trips from a buyer's phone. The Kuala Lumpur numbers are the same as in the Client-Server Architecture lesson:

Buyer in to Kuala Lumpur to Singapore to Bangkok to Virginia
Kuala Lumpur 8 ms 20 ms 30 ms 230 ms
Kuching 30 ms 15 ms 55 ms 245 ms
Hanoi 70 ms 85 ms 40 ms 290 ms

The Lab has three example PoPs: Kuala Lumpur, Singapore and Bangkok. Each buyer uses the nearest: Kuala Lumpur goes to PoP Kuala Lumpur (8 ms), Kuching to Singapore (15 ms), Hanoi to Bangkok (40 ms).

HIT, MISS and distance

A MISS travels three legs: buyer to PoP, PoP to origin over the CDN provider's network, then the origin's own work (2 ms for a file, 20 ms for a page). A HIT travels only the first leg. The poster from Kuala Lumpur, origin in Virginia:

Request Its legs Total
First: MISS 8 ms to PoP Kuala Lumpur + 220 ms PoP to Virginia + 2 ms 230 ms
Second: HIT 8 ms to PoP Kuala Lumpur 8 ms
No CDN 230 ms to Virginia + 2 ms 232 ms

So a poster already at PoP Kuala Lumpur arrives in 8 ms. Once the copy is at the PoP, how far away the origin is no longer matters. Only a MISS pays for the distance, and a MISS takes about as long as no CDN: 230 ms against 232 ms. The extra stop costs nothing here, because the route through the PoP over the CDN provider's network (8 + 220 = 228 ms) is no longer than the direct one (230 ms).

The gain multiplies because one copy is shared. During the ticket war, 10,000 people a second open the same poster. With a hit ratio (the share of requests that HIT) of 99%, the origin only gets 100 requests a second. The PoPs answer the rest.

What to cache: static or dynamic

The content that suits a CDN best is static assets that are the same for everyone and rarely change: images, CSS, JavaScript bundles, fonts. Content made for one person (a cart, a profile, an order history) or that changes every second must not be cached at the CDN: kept at a PoP, another buyer could receive Budi's cart.

The origin tells the CDN through the Cache-Control header:

// TicketTune's rules at the origin
const RULES = {
  '/img/poster-archipelago.jpg': 'public, max-age=86400', // static: 1 day
  '/events/42': 'public, s-maxage=60',                  // concert page: 60 seconds at the CDN
  '/api/cart': 'private, no-store',                     // one buyer's cart: never store
};

These rules hold for a CDN that follows the origin's headers. By default, Cloudflare caches by file extension and never caches HTML or JSON. So without a Cache Rule that makes the URL "Eligible for cache", /events/42 is not cached even with s-maxage=60, and both /events/42 and /api/cart show DYNAMIC. BYPASS appears only when a rule made the URL cacheable and the origin then sent private or no-store.

Every Cache-Control value in detail, hashed file names and purging by tag are in the CDN & Edge Caching lesson (Performance track).

TTL and purge: fresh or fast

The TTL (time to live) is how long a copy counts as fresh. During the TTL, the PoP doesn't ask the origin, so it can't know when the origin's data changes. In the Lab, the /events/42 page is cached at second 0, then its price drops from $75 to $70:

Second Request to the PoP Status Price shown
0 the first MISS $75
30 after the price dropped HIT $75, stale
61 the 60 s TTL is over EXPIRED $70

A purge deletes the copies at every PoP right now, so the next request is a MISS and fetches the new version. This is the trade-off: a long TTL means a high hit ratio and a relaxed origin, but stale data for longer. A short TTL means fresher data, but more requests to the origin. For data that is the same for everyone but can change, use a short TTL and purge when the data changes.

Edge computing: code that runs at the PoP

A modern CDN can run code at its PoPs: Cloudflare Workers, Vercel Edge Functions, Fastly Compute. Edge computing is running application logic on CDN servers close to the user, not just serving static files. A TicketTune example during the ticket war: checking the queue token before a request may go on to the origin.

// A Cloudflare Worker: runs at the PoP, not at the origin
export default {
  async fetch(request) {
    const cookie = request.headers.get('Cookie') || '';
    if (!cookie.includes('turn=ok')) {
      return new Response('You are in the queue. This page will try again shortly.');
    }
    return fetch(request); // your turn: pass it on to the cache or the origin
  },
};

The edge suits small logic: redirects by country, A/B tests, token checks, blocking bots. The data still lives at the origin, so logic that needs the database is still better there. More in the Cloudflare Workers & Edge Computing lesson (DevOps track).

🎯 A strategy per kind of content

Content Strategy
Hashed files (app.3f9a1c.js), posters, fonts cache long: max-age=31536000, immutable or a day
Public pages that can change (concert details) cache short (s-maxage=60) + purge on change
Personal data (cart, profile, order history) don't cache: private, no-store
Real-time data (seats left right now) don't cache, or a few seconds at most
Small logic before the origin (queue, redirects) an edge function

⚠️ Someone else's cart and stale prices

TL;DR: A CDN keeps copies of content at PoPs (Points of Presence), CDN server locations in cities near the users, so latency follows the distance to the PoP, not to the origin. In the TicketTune model, the poster from Kuala Lumpur with the origin in Virginia: MISS 230 ms, HIT 8 ms, no CDN 232 ms. With a 99% hit ratio, the origin only gets 1 in 100 requests. Cache static assets that are the same for everyone (images, CSS, JavaScript) for long; public pages that change with a short TTL plus purges; never cache personal or real-time data (private, no-store, BYPASS). On Cloudflare, HTML and JSON need a Cache Rule (without one: DYNAMIC). During the TTL a PoP can't know the origin changed, so it can serve an old price until the TTL runs out or it's purged. Edge computing runs application logic on CDN servers close to the user, such as a queue-token check in 41 ms from Hanoi instead of 310 ms to the origin.

⚡ A HIT only travels to the PoP

Guess first: TicketTune's origin is in Virginia, 230 ms from Kuala Lumpur. How many ms does a poster already at PoP Kuala Lumpur take to reach Budi?

8 ms. A HIT only travels from the buyer to the PoP. Only a MISS pays for the distance to the origin: 230 ms.

Meaning: A CDN keeps copies of content at PoPs, CDN server locations in cities near the users. With a 99% hit ratio, the origin only gets 1 in 100 requests.

⚡ The origin decides with Cache-Control

Write:

// TicketTune's rules at the origin
const RULES = {
  '/img/poster-archipelago.jpg': 'public, max-age=86400', // static: 1 day
  '/events/42': 'public, s-maxage=60',                  // concert page: 60 seconds at the CDN
  '/api/cart': 'private, no-store',                     // one buyer's cart: never store
};

Meaning: public, max-age may be stored by anyone. s-maxage is only for shared caches such as a CDN. private, no-store is never stored, so Budi's cart never reaches another buyer. On Cloudflare, HTML and JSON need a Cache Rule first.

⚡ During the TTL, the PoP doesn't know about changes

Meaning: The price drops at second 30, but the PoP still serves $75 until the 60-second TTL is over. A purge deletes the copies at every PoP right now. For public data that can change: a short TTL plus purges.

⚡ Edge computing

Meaning: Edge computing is running application logic on CDN servers close to the user, not just serving static files. A queue-token check from Hanoi takes 41 ms at the PoP, against 310 ms at the origin.

What you will learn