🎓 A1Learner
🔍
0 of 64 lessons completed

Design a CDN

A content delivery network is a network of edge servers that store copies of content close to the people who ask for it. A request goes to a nearby edge server instead of a distant origin server.

The ask

The system stores content once at an origin server.

Copies of popular content sit on edge servers in many places.

A person asks for a file or a page by its normal address.

The system sends each request to a nearby edge server.

The edge server answers at once when it holds a copy.

Estimate the scale

The origin holds 10,000,000 objects.

The network has 1,000 edge locations.

Requests reach 1,000,000,000 a day.

1,000,000,000 requests / 86,400 seconds = 11,574 requests per second.

Traffic at the origin

The edge answers 95% of requests.

The other 5% reach the origin.

1,000,000,000 requests x 5% = 50,000,000 requests a day reach the origin.

50,000,000 requests / 86,400 seconds = 579 requests per second at the origin.

API and data

A get request carries the address of the content it wants.

A response carries the content itself.

A response also carries a note on how long the copy may be kept.

Each stored object has a name, a size and a version mark.

The origin is the source of truth for every object.

An edge server stores the objects asked for near it.

High-level design

Anycast is a routing method where one address is announced from many locations and the network sends each request to the nearest healthy location.

The edge servers share one anycast address for incoming requests.

A request lands on the edge server for its area.

That server checks its own store for the object.

A hit is a request answered from the edge store.

Hits and misses

A miss is a request with no copy at the edge.

On a miss the edge server fetches the object from the origin.

The edge server stores the fetched copy for later requests.

Serving a request

Most requests end at the first edge server they reach.

The trip is short because the server is near the person.

A round trip across an ocean takes about 150 milliseconds.

A nearby edge server cuts most of that trip away.

The origin sees only the misses and the first asks in each area.

Keeping copies fresh

Each copy carries a time limit set by the origin.

A copy past its time limit is stale and is not served.

The edge server then asks the origin for a fresh copy.

A changed object gets a new version mark at the origin.

A purge is an order to drop a stored copy at once.

An urgent change sends a purge to every edge server.

Deep dive: the huge file

One release file holds 10 GB.

The file must reach thousands of machines at the same release moment.

Sending the file only when the first request arrives is too slow.

Pre-positioning means the file is copied to edge servers before the release time.

10 GB to each of 1,000 edges = 10 TB of placement traffic.

Placement starts hours before the release.

Each edge server confirms it holds the full file before the release.

Peer help for the huge file

Peer-assisted delivery means edge servers also fetch pieces of the file from each other.

One edge server can take a missing piece from a nearby edge server.

The origin then sends each piece far fewer times.

Each piece is checked against a fingerprint from the origin.

A bad piece is fetched again and is never served.

When an edge fails

Each edge server is watched with regular checks.

A failed edge server stops announcing its address.

Anycast sends later requests to the next nearest healthy location.

Traffic rises on the edge servers that remain in the area.

Headroom in each area absorbs that rise.

Evaluation and trade-offs

The main win is speed. Most requests end at a nearby server.

The origin is shielded. Only a small share of requests reaches it.

The cost is storage and copying. Popular content is stored many times.

Freshness is the hard trade-off.

A long time limit cuts origin traffic but serves older copies.

A short time limit keeps copies fresh but sends more requests to the origin.

Scale at a glance

ItemFigure
Requests a day1,000,000,000
Requests a second11,574
Reach the origin a day50,000,000
Reach the origin a second579
Edge locations1,000
Objects at the origin10,000,000

Exercise

Why does a request go to an edge server first?