🎓 A1Learner
🔍
0 of 47 lessons completed

What a CDN Does

A CDN is a set of servers spread across many places that hold copies of files near the people asking for them. A CDN shortens the trip each file makes, because most requests end at a nearby copy.

No CDN, every request travels

10 requests ask for one file, and only the origin, the main server holding the first full copy, holds it. The origin answers all 10.

answers = {"origin": 0}

for _ in range(10):
    answers["origin"] += 1

print("origin answers:", answers["origin"])

With a CDN, the edge takes over

The first request fills the edge from the origin. The edge, a CDN server placed near users, then answers the rest itself.

answers = {"origin": 0, "edge": 0}
edge_has_file = False

for _ in range(10):
    if edge_has_file:
        answers["edge"] += 1
    else:
        answers["origin"] += 1
        edge_has_file = True

print("origin answers:", answers["origin"])
print("edge answers:", answers["edge"])

One fill per file

2 files are requested 5 times each. The origin answers 2 times in total, and the edge answers every repeat.

answers = {"origin": 0, "edge": 0}
at_edge = set()

for name in ["logo", "logo", "clip", "logo", "clip",
             "clip", "logo", "clip", "logo", "clip"]:
    if name in at_edge:
        answers["edge"] += 1
    else:
        answers["origin"] += 1
        at_edge.add(name)

print("origin answers:", answers["origin"])
print("edge answers:", answers["edge"])

Example

requests = ["song", "photo", "song", "guide", "photo",
              "song", "clip", "photo", "guide", "song",
              "photo", "clip", "song", "guide", "photo",
              "song", "clip", "photo", "guide", "song"]

print("without a CDN, origin answers:", len(requests))

at_edge = set()
origin = 0
edge = 0
for name in requests:
    if name in at_edge:
        edge += 1
    else:
        origin += 1
        at_edge.add(name)
print("with a CDN, origin answers:", origin)
print("with a CDN, edge answers:", edge)

Key points

  • CDN: short for content delivery network, the set of spread-out servers holding the copies.
  • Origin: the main server that holds the first full copy of every file.
  • Edge: a CDN server placed near users, holding copies of popular files.
  • Use a CDN to serve the same files to users in many places.

In this example, 20 requests cover only 4 different files. Without a CDN the origin answers all 20, while with a CDN the origin answers 4 and the edge answers the other 16.

Note

a round trip across an ocean takes about 150 milliseconds, so a near copy can remove most of that trip.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What is the origin in a CDN setup?