AlgoViz

Introduction to a CDN

easy 26 min

A content delivery network (CDN) is a set of servers in many places that keep copies of your content and answer users from nearby.

See it run

Follow one request from Sydney: the long trip, the first copy, the short trip, and the moment a copy expires.

Operations

Without a CDN

A user in Sydney, a server in Virginia. Assumed round trip: 200 ms.

Step 1 of 6

The user asks

200 ms round tripUser in SydneyDNSOriginVirginia
Time so far
0 ms

A picture is needed. Nothing is known yet.

Step 1: The user asks

dns = 20tcp = 200tls = 200request = 200 + 30total = dns + tcp + tls + request   # 650 ms
Time
650 ms
Origin work
every request
Long trips
3

Handshakes are round trips too. They cost as much as the answer.

In the real world

The same introduction to a cdn doing real jobs. Pick one and step through it.

Three readers on three continents.

Step 1 of 2

All cold

SydneyLondonSao PauloSydney edgemissLondon edgemissSao Paulo edgemissOrigin3 requests

3 trips to the origin

Every edge must fetch its own first copy.

Step 1: All cold

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • A personal page in a shared cache

    critical

    The next person at that edge receives somebody else's page.

    Wrong
    headers["Cache-Control"] = "public, max-age=600"   # account page
    Right
    headers["Cache-Control"] = "private, no-store"
  • No cache headers at all

    major

    Each CDN then uses its own default, which may surprise you.

    Wrong
    return Response(body)
    Right
    return Response(body, {"Cache-Control": "public, max-age=3600"})
  • Changing a file under the same name

    major

    Edges and browsers keep the earlier copy until it expires.

    Wrong
    upload("app.js")
    Right
    upload("app." + content_hash + ".js")
  • Origin open to the world

    critical

    An attacker, or a wrong link, goes around the CDN.

    Wrong
    allow_from = "anyone"
    Right
    allow_from = "the CDN, with a checked identity"

Interview tips

  • How to explain it

    • Kiosks near readers, one print shop.
    • Hit: from the shelf.
    • Miss: wait for the van once.
  • Memory trick: SPEED

    • Servers spread out, Proximity.
    • Edge caches, Everyone served fast.
    • Delivery from nearby.
  • Numbers

    • Three round trips for new HTTPS.
    • Origin load: R x (1 - h).
    • 95% to 90% doubles it.
  • Ask

    • Same for everybody?
    • How old may it be?
    • How will it change?