Video in progress
How do you build a CDN strategy for a site with many personalized pages?
I split the page into shared and personal parts: shared HTML/JSON and static assets are cached on the CDN with a well-chosen cache key, while personal parts come through a separate, uncached API. I do invalidation by surrogate key (tag), not by URL.
📖 Answer
The most common mistake is "the page is personal, so we don't need a CDN". In fact even on a personal page, 80-90% is the same for everyone: product description, images, price. Only the cart count, the name and recommendations are personal.
The strategy:
- Static assets (JS, CSS, images): a hash in the file name,
Cache-Control: public, max-age=31536000, immutable. You never invalidate them; a new version gets a new name. - Shared content (product page, catalog): a short TTL on the CDN and
stale-while-revalidate. The personal parts are loaded later on the client or assembled at the edge (ESI or edge functions). - Personal API:
Cache-Control: private, no-store. Forgetting this header is a classic incident: one user's page shows up for another.
Choose the cache key carefully:
- take unneeded query parameters (
utm_*,fbclid) out of the key, otherwise the hit rate drops; - don't set
Vary: Cookie, since that means a separate copy per user; if you need it, use a normalized header with an exact value only (language, currency, country).
Invalidation: when a product's price changes, it shows up on 50 pages (category, search, home). Purging URLs one by one doesn't work. Put tags like Surrogate-Key: product-123 category-7 on the response, then purge by tag; many CDNs support this.
Origin protection: an origin shield (a single intermediate layer), closing direct access to the origin from anywhere but the CDN, and a plan for when the CDN goes down. The origin may never be able to carry the full traffic, and that belongs on the list of reliability risks.
Sign up to mark studied questions and save your progress.