SkillSprout
Chapter 13 · Lesson 2
Intermediate
3 min read

Controlling the Edge Cache: Pull vs Push, TTL and Invalidation

Overview

A CDN is only useful if it caches the right things for the right time. This lesson shows how content reaches the edge, how long it stays, and how to update it quickly when it changes.

Detailed Notes

What is edge cache control

It is the set of rules that tell the CDN how content gets in, how requests are matched to cached copies, and how long a copy is trusted.

The problem it solves

Cache too long and users see an old site after you deploy. Cache too short and the CDN barely helps. You want long caching for speed and instant updates when you change something.

How it works

Pull vs push

  • Pull CDN: the edge fetches from your origin on the first miss. Easy; the first visitor in each region is a bit slower. This is what most websites use.
  • Push CDN: you upload content to the CDN in advance. Good for small, predictable sets such as game patches or big software releases, but you manage uploads and cleanup.

Cache key. The edge decides whether two requests are "the same" using a cache key, normally the URL (sometimes with chosen headers or query parameters). Careless keys hurt: ignoring a parameter that changes the content serves wrong data, while including useless parameters (like tracking IDs) creates many copies of the same file and lowers the hit ratio.

TTL. Your origin tells the CDN how long to keep a file using the HTTP header:

text
Cache-Control: public, max-age=31536000

That means any cache may keep it for one year.

Invalidation (purge). You can ask the CDN to delete a path from all edges. It works, but it takes time, can cost money, and is clumsy for many files.

Versioned URLs: the better trick. Put a content fingerprint in the filename, like app.8c1d9e.js. Cache these files for a year. When you deploy, the new build has a new name, so users fetch the new file at once and the old one simply stops being used. Keep the HTML page itself on a short TTL (for example 60 seconds), because it points to the current file names.

A simple example

A restaurant prints a new menu each season with the date on its cover. Old menus can stay in drawers forever; nobody mixes them up, because customers are always handed the one with the current date.

System design example

An online store's HTML page has a 60-second TTL. Its CSS and JavaScript have fingerprinted names with one-year TTLs. On Friday the team ships a fix: the new HTML points to site.9f2a.css. Within a minute, every region serves the new page, each edge fetches the new CSS once from the origin, and nothing needed purging.

Summary

  • Pull CDNs fetch on the first miss; push CDNs get uploads in advance.
  • The cache key decides which requests share a cached copy.
  • Cache-Control: max-age sets the TTL.
  • Purging works but is slow and clumsy; fingerprinted file names avoid the need.
  • Short TTL for HTML, long TTL for versioned assets.

Architecture Diagram

Fresh HTML, long-lived assets
Architecture
Browser

CDN edge

HTMLTTL 60 s
app.hash.jsTTL 1 year
Origin Storage

Short TTL for the entry page, long TTL for fingerprinted files.

  1. 1The browser loads the short-lived HTML page from the edge.
  2. 2The HTML references a versioned asset URL.
  3. 3The edge serves the asset from cache for a long time.
  4. 4A deploy changes the asset URL; the edge fetches the new file once.

Common Interview Questions

Chapter quiz

CDN · 5 questions

  1. 1.What is the main benefit of a CDN?

  2. 2.In a pull CDN, when does the edge first get a file?

  3. 3.What does the origin shield do?

  4. 4.Which approach updates cached JavaScript instantly after a deploy?

  5. 5.Which content is easiest to cache on a CDN?

0 of 5 answered

Finished this lesson?

0 of 91 lessons done · 0% of the course