Make the right trade-off / CHAPTER 29
CDN delivery versus direct origin serving
Decide when nearby reusable copies justify a CDN, and protect the original files and private access.
Builds on Caching fundamentals, Back-of-the-envelope estimation.
- Calculate origin traffic from edge misses.
- Choose cache keys and freshness rules.
- Plan invalidation and private-content delivery.
01What a content delivery network changes
A shop serves product thumbnails to viewers in several regions. Each request downloads a 50 KB image. Assume 20,000 requests/s: repeatedly sending the same files from the shop's servers consumes substantial bandwidth and makes distant viewers wait for a long network trip.
A content delivery network, or CDN, keeps copies at servers in many locations. Follow the names through that operation:
- Origin: the shop's server or storage service supplies the original published file.
- Edge server: a CDN server nearer a viewer may keep a copy and answer the request.
- Hit: the edge has a copy it is allowed to reuse.
- Miss: the edge must fetch the file or check its stored version with an upstream server.
Direct origin serving skips that distributed caching layer. It can be simpler for a small nearby audience with little reusable content. A CDN can also terminate connections and apply abuse controls, but those features do not make every request cacheable or replace private access checks.
02Decide which requests may reuse a copy
Trace one small thumbnail through the CDN
- The page requests
/products/42/thumb-v8-small.jpg. The URL identifies product42, version 8 and the small size. - The edge looks up its cache key. The key is the identifier under which it stores a response. Include fields that change the bytes, such as size or language. Otherwise a small image could answer a request for the large one.
- On a miss, the edge asks the protected origin for the file. It stores the response only if caching rules permit it. When several callers need the same file at once, one shared fetch can satisfy them; this is request coalescing.
- A later matching request reuses the copy. It avoids the origin fetch when the key matches, the response’s reuse period has not ended and any private-access check succeeds.
- The shop publishes changed bytes at version 9. Update the page to use the new URL. Version 8 still identifies its original unchanging bytes. Such a URL is called immutable: its bytes do not change in place, so old copies need not be rewritten.
For a URL whose bytes can change, specify how reuse ends
- A TTL, or time to live, permits reuse for a stated interval before checking again.
- An ETag identifies a response version. The cache sends it to ask whether that version is unchanged; confirmation can save transferring the full file again.
- A purge asks CDN caches to remove a copy. Handle delayed or failed purges; they do not update every location at one instant.
Avoid irrelevant tracking fields in the key: they create separate copies and reduce reuse. Keep private responses separated or uncached when different users must receive different data.
03Worked example: thumbnail traffic
Calculate traffic before claiming the CDN solves capacity
- Full delivery:
20,000 requests/s × 50 KB = 1 GB/s, using decimal units before protocol overhead. - At 95% hits: 1,000 requests/s still reach the origin, requiring about 50 MB/s.
- At a cold launch: usable copies may not exist yet. The origin could receive nearly the full 20,000/s and 1 GB/s until reuse develops.
- Protect those misses: limit simultaneous origin fetches, share identical fills and introduce new content gradually where possible. A global average hit rate can hide one cold region or a new large object.
Use different policies for different content
| Content | Candidate policy | Why that policy has a limit |
|---|---|---|
| Public unchanging thumbnail | Publish a versioned URL with a long reuse lifetime. | Pages must discover the new URL after an edit. |
| Public changing description | Set an allowed age and use validation or tested purges. | Readers may see an older permitted version. |
| Private original image | Verify access or issue restricted temporary credentials. | Cached delivery must still enforce those credentials. |
| Document requiring immediate revocation | Check current permission for each controlled download. | A previously issued reusable link or downloaded file cannot be universally recalled. |
A signed URL is a temporary permission, not automatic privacy. Check which file it permits, when it expires and whether reuse bypasses another caller's access rules. Keep the origin protected too; a public storage URL could evade the CDN's gate.
CDN delivery versus direct origin serving — decision flow
Follow the numbered arrows
- Viewers → Nearby CDN serverrequest a particular thumbnail URL
- Nearby CDN server → Shop originfetch a missing file or check its version
- Shop origin → Nearby CDN serverreturn the file and caching rules
- New file versions → Shop originpublish new bytes under a new URL
Trace the diagram in words
- The viewer asks the delivery hostname for a particular version and size of thumbnail.
- If the matching version/size is absent or no longer reusable, the edge asks the origin for the file and its cache rules.
- Publish changed bytes at a new versioned URL and update pages to reference it.
04Operate the edge and the origin together
Make the delivery choice with these questions:
- Do viewers repeatedly request the same bytes? Measure reuse by region and object size, rather than assuming one hit percentage covers all traffic.
- Does distance matter? Compare actual viewer response times through the origin and nearby delivery servers.
- What happens when the origin fails? Reuse a public logo while its cache lifetime permits. For a private document requiring immediate revocation, check permission on each download; if that check is unavailable, refuse the download rather than serve an old permission.
- Can direct fallback handle the load? It must preserve access checks and fit origin capacity; an unlimited fallback can spread the outage.
- What must operators observe? Track edge hits, copy age, origin requests and bytes, errors and purge completion.
Draw both a hit and a miss. Name where each gets its bytes and where permission is checked. A CDN is justified when the resulting distance, traffic or operational benefit exceeds its configuration and delivery cost, not at a universal user-count threshold.
DRAW. CALCULATE. EXPLAIN.
Budget a thumbnail launch
20 minutes · guided practice
A shop expects 8,000 requests/s for 40-KB thumbnails. Estimate full and origin payload rates at 90% hits. A release is initially cold. Assign policies for versioned public thumbnails and a private seller report.
Compare the worked solution
- Full delivery is 8,000 × 40 KB = 320 MB/s before overhead.
- At 90% hits, 800 misses/s require about 32 MB/s from the origin.
- A cold release can expose the origin to nearly the full rate. Share one fetch for matching misses, limit simultaneous fills and warm new content gradually if useful.
- Give changed public thumbnails new versioned URLs and update page references.
- Check that the signed-in seller owns the report. Keep its cached response separate from other sellers, or do not cache it. A public origin URL must not bypass that check during fallback.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01What does a CDN hit save?
- A nearby server reuses a matching file while its cache lifetime and access rules permit.
- The request avoids an origin fetch and often a longer trip.
- Missing copies still need origin capacity and correct access checks.
CARD 02How do cache keys affect correctness?
- A key identifies a reusable response.
- Include fields changing its bytes, such as image size.
- Missing fields can mix responses; irrelevant fields create separate copies and reduce hits.
CARD 03How should CDN updates work?
- Give changed bytes a new versioned URL.
- Update page references.
- If one URL can change, define expiry, validation or removal rules, including delayed updates at every cache layer.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.