02. Scaling / Lesson 5 of 5

Content Delivery

Put copies of your files in cities near the users.

  • 9 min
  • Beginner
  • 9 steps
  • Sydney user600 ms (bad)
  • Virginia user60 ms (good)
  • Origin load1,000/s (warning)
16,000 kmUsers in Sydney600 msOrigin (Virginia)1,000/s requestsUsers in Virginia60 ms
  • Request
  • Cache hit
  • Cache miss
Step 1/9

Step 1 of 9: The site is in Virginia, and some users are in Sydney

Read every step as text
  1. Step 1: The site is in Virginia, and some users are in Sydney

    All your servers (the origin) are in Virginia. Users nearby get pages quickly, but users in Sydney, about 16,000 km away, wait much longer for the same page.

    A new HTTPS connection plus the request takes 3 round trips. At 20 ms per round trip that is 60 ms in Virginia, and at 200 ms it is 600 ms in Sydney, before the server does any work.

    Faster servers do not help here. The time is spent on the road, not in the CPU.

  2. Step 2: Physics sets a floor you cannot buy your way past

    Light in an optical fibre travels about 200,000 km each second. So 16,000 km takes 80 ms one way and 160 ms there and back, even on a perfect straight cable.

    Real routes bend and pass through many routers, so say 200 ms per round trip. Three of them make 600 ms. The only way to lower this is to make the distance shorter.

    You cannot make light faster, so move the data closer to the user.

  3. Step 3: A CDN has servers in many cities

    A content delivery network (CDN) runs many small servers, called edge servers or points of presence, in cities all over the world. We put one in front of the site, and the users now connect to the edge in their own city.

    The connection setup is now with a server 10 ms away: 3 × 10 = 30 ms. DNS or anycast routing picks the nearest edge for each user.

    The edge is a cache that lives near the user. The origin is still the source of truth.

  4. Step 4: The first request is a miss: the edge asks the origin

    The edge starts empty. The first user in Sydney asks for /logo.png. The edge does not have it, so it fetches it from the origin over a connection it keeps open, one round trip of 200 ms, and stores a copy.

    That user waits 230 ms (30 ms to connect plus 200 ms to the origin), which is better than 600 ms. The Virginia edge is near the origin, so its miss costs only 35 ms.

    Someone always pays for the first request. A CDN can warm popular files in advance.

  5. Step 5: Every later request is a hit, served from the edge

    The next user in Sydney asks for the same file and gets the copy from the edge in Sydney. They wait 3 × 10 = 30 ms instead of 600 ms: 20 times faster.

    Say 95% of the 1,000/s requests for static files are hits. The origin now handles only 50/s, so it needs much less hardware.

    Users get faster pages, and the origin does a small part of the work. Both come from the same copies.

  6. Step 6: Only some content can be copied

    Static files are the same for everyone: images, CSS, JavaScript, fonts, video. They are ideal for a CDN. Dynamic responses differ per user or per moment: your cart, your feed, an API call.

    Say 20% of requests are dynamic. They still go to the origin, over the CDN's fast, already-open connection: 230 ms in Sydney. The origin now sees 200/s dynamic plus 40/s static misses: 240/s.

    A CDN also soaks up traffic spikes and some attacks, because the edges answer most requests themselves.

  7. Step 7: Headers tell the edge how long a copy is good

    The origin sends a Cache-Control header with each response. public, max-age=86400 means any cache may keep it for one day. private means only the user's browser may. no-store means keep nothing.

    A trick for files that change: put a hash in the name, such as app.3f9a1c.js, and cache it for a year. A new build gets a new name, so users always get the right one, and the HTML page (cached for a short time) points to it.

    Long max-age plus versioned file names gives the best of both: speed and instant updates.

  8. Step 8: When you must remove a copy right now

    Sometimes a wrong file is already cached in every city: a wrong price list, or a logo you must not show. Waiting for max-age to run out is too slow, so you send a purge request to the CDN.

    The CDN deletes that URL from all its edges, usually within seconds. The next request at each edge is a miss and loads the new version from the origin. Purging a few URLs is cheap; purging everything makes all edges miss at once, so the origin sees a spike.

    Prefer versioned names so you rarely need to purge. Keep purge for emergencies.

  9. Step 9: A CDN in one picture

    Distance costs time: 600 ms to load from Sydney when the origin is in Virginia. A CDN keeps copies in edge servers near the users, so a hit costs 30 ms.

    Static files are cached according to their headers, dynamic requests still go to the origin, and a purge or a new file name replaces old copies. The origin does far less work.