Lesson 05

The Edge: CDNs as a Black Box

Building block

A content delivery network is a cache you rent, on machines near your readers. In an interview it is a black box with stated guarantees: it answers repeated requests before they reach you, carries bytes your servers could not, and spares a distant reader a long round trip. It is wrong in exactly the ways you choose through its cache key and its expiry times.

The first reason to draw one is bandwidth, and the arithmetic says so. How a CDN works inside is Networking Deep Dive's lesson on CDNs and edge caching. The headers one service sets for it are Backend Deep Dive's lesson on HTTP caching.

What the Edge Promises

The edge promises three things. A cached response comes from a location near the reader. The bytes travel on the CDN's network, not out of your servers. And a reader in Southeast Asia is spared the 225-millisecond round trip to an origin in US east.

It does not promise a hit, because each location keeps its own cache and the long tail misses. It does not cache what the key and the headers forbid. A purge does not reach every location at the same moment. And nothing about the edge keeps your origin up.

Bandwidth is why video starts at the edge. Assume a million viewers of one HD stream, at about 7 megabits a second each. That is 7 terabits a second. A 100-gigabit link carries a seventieth of it, and even a large cloud instance, at 400 gigabits, carries under a seventeenth. Here the edge is not an optimisation. Serving the stream from the origin would take seventy 100-gigabit links, and the edge already has that capacity, near the viewers; the video streaming problem in Chapter 19 starts from it.

Hits answered at the edge, misses through a shield to the origin
Viewersa million Edge locationanswers its hits Edge locationanswers its hits Edge locationone of 100 Shieldregional cache Originobject store nearest edge miss one fetch readcache miss

The Edge Hit Rate in Bytes

The arithmetic of Caching at Scale applies here in bytes. Of 7 terabits a second, a 95 percent edge hit rate leaves the origin 350 gigabits, three and a half 100-gigabit links. At 99 percent it leaves 70 gigabits, less than one link.

A million HD viewers, 7 terabits a second at the edge
95 percent edge hits1 byte in 20 from the origin
350 Gbps reach the origin: three and a half 100 Gbps links.
99 percent edge hits1 byte in 100 from the origin
70 Gbps reach the origin: less than one 100 Gbps link.

The cache key sets the hit rate. Two requests share an entry only when their keys match, so each header or cookie added to the key multiplies the entries, and a session cookie in it makes every entry private to one user. A personalised page splits in two: a shared shell served from the edge, and a small call per user that goes to the origin.

Getting Content Off the Edge

An object leaves the edge when it expires, when it is purged, or when nobody asks for it again because its name changed. Choose by whether the URL must stay stable. Assets a page refers to, its scripts and images, get versioned names and long expiry times, and are never purged: a new version is a new name. Objects whose URL is the product, a video ID or a shared image link, get a short expiry time and a purge for urgent changes.

A takedown with a deadline purges for speed and keeps the expiry time as the bound, because a purge reaches each location on the vendor's schedule, and copies already in browsers cannot be recalled at all. The takedown curveball in Chapter 7 is the same problem: a permanent redirect already kept by a browser cannot be recalled either. Private objects go out under signed URLs that expire.

What goes to the edge
The same bytes for everyone→A long expiry and a versioned name
The same for many readers, for seconds→A short expiry
Different for each user→The origin, or a shared shell plus an API call
Must vanish on demand→A short expiry and a purge
Private→A signed URL that expires

Shielding the Origin

Each location misses on its own, so a new object that is popular everywhere misses everywhere at once. Assume 100 edge locations. The first segment of a new episode is then 100 fetches from the origin in the same second.

A shield, a regional cache between the edges and the origin, makes that one fetch per shield. Collapsing concurrent misses at each layer, the edge's form of the coalescing in When the Cache Hurts, keeps it at one while the fetch is in flight. The origin is still sized for a cold edge, because a purge of a hot object, a change of CDN, or a failed edge region each send it the misses at once.

Pull on a Miss vs Push Ahead

Pull lets the edge fetch an object the first time a reader asks for it. There is nothing to manage; the first readers at each location pay the miss, and the origin behind its shield takes the burst. Use it for content whose popularity is not known in advance.

Push places objects at the edge, or requests them through it to warm it, before anyone asks. There is no first-miss burst, at the cost of storage and transfer for objects that may never be read, and a step in the publishing pipeline. Use it when the launch time and the audience are known, such as a scheduled premiere.

What the Edge Costs

The bill is per byte delivered, so egress goes into the estimate as bytes per view times views. An hour of HD is about 3 gigabytes, so a million viewer-hours is 3 petabytes. The long tail costs more per view, because each edge miss is also egress from the origin.

Responses that differ per user, and writes, stay off the edge. And an origin that cannot carry 7 terabits a second cannot be the CDN's fallback, so surviving a CDN outage takes a second CDN, with a degraded mode, such as a lower bitrate for everyone, to shrink what it must carry. The first costs a second vendor to integrate and pay; the second costs every viewer a worse picture.

How Interviewers Probe This

For the edge, start from what reaches the origin, then say which guarantee the edge does not give. For video, the opening line carries the whole argument. You might say: "A million viewers is 7 terabits a second, so the edge is the design and the origin only sees the misses."

The first probe is "A million viewers start the same video in the same minute. What does your origin see?" A good answer gives the hits at the edge, the misses collapsed at each location, one fetch per segment per shield, and the origin's bytes at a stated edge hit rate.

The second is "A file was published by mistake and must disappear now." A good answer purges for speed, names the expiry time as the bound, uses expiring signed URLs for anything private, and says plainly that copies already in browsers cannot be recalled.

The third is "Can you put the API behind the CDN?" A good answer sorts responses by who shares them. Shared ones are cached for seconds and per-user ones are not, though the edge still ends the connection near the reader.

Misconceptions
  • "The CDN caches everything." Only what the cache key and the headers allow. A session cookie in the key takes the hit rate to zero.
  • "A purge is instant." It reaches each location on its own schedule. A takedown with a deadline also needs a short expiry, or signed URLs that expire.
  • "With a CDN the origin can be tiny." It is sized for a cold edge and the long tail. A purge of a hot object makes every location miss on it at once, and only the shield and collapsed misses keep that to one fetch.
  • "A CDN is for static files." A response that is the same for many readers, such as a public product page or a top-ten list, can sit at the edge for seconds.
  • "The CDN removes the bandwidth bill." It turns it into a price per byte delivered, and the misses still cost egress from the origin.
Why It Matters
  • Say what is cacheable at the edge and what its cache key is, and keep cookies out of shared keys.
  • Give assets versioned names and long expiry times, and give stable URLs short expiry times and a purge path.
  • Put a shield in front of a small origin or large objects, and size the origin for a cold edge.
  • Put egress in the estimate, bytes per view times views, before deciding what is served from where.
RelatedNetworking Deep Dive the CDN insideBackend Deep Dive HTTP caching headersWhen the Cache Hurts coalescing and the cold tier

Knowledge Check

A million viewers watch one HD stream at about 7 megabits a second each. What reaches the origin at a 95 and at a 99 percent edge hit rate?

  • 350 gigabits a second at 95 percent, and 70 gigabits at 99 percent
  • 7 terabits at both rates, since the edge forwards every stream on
  • 35 gigabits a second at 95 percent, and 7 gigabits at 99 percent
  • About 70 gigabits at both, since the hit rate barely moves the bytes

A team adds the session cookie to the edge cache key for product pages. What happens?

  • Every entry becomes private to one user, and the hit rate falls to zero
  • Each user gets their own page faster, while the hit rate stays the same
  • The edge ignores the cookie, because cookies never enter a cache key
  • Signed-in users see fresher pages, and the origin load does not change

A site ships a new version of its main script. Which way gets it to every reader without a purge?

  • Give the file a new versioned name and keep its long expiry time
  • Keep the same name and purge it at every location after a release
  • Keep the same name and give every asset an expiry of a few seconds
  • Keep the same name and ask readers to reload with the cache cleared

A new episode launches behind 100 edge locations and one shield. What does the origin see for its first segment?

  • One fetch from the shield, while the edges wait on that fetch
  • 100 fetches, one from each edge location, all in one second
  • No fetch at all, since a shield holds every object in advance
  • One fetch per viewer until the first edge location has it

Why can the origin not take over serving the video when the CDN fails?

  • It cannot carry the 7 terabits a second that the edge was delivering
  • Its objects are stored in a form that only edge locations can read
  • Viewers' players refuse any stream that does not come from an edge location
  • It holds only the long tail, and never the episodes that are popular

You got correct