Prompt
What CDN setup would you suggest for an image-heavy site that publishes often and needs safe cache purging?
Latest observation
For an image-heavy site that publishes often and needs safe cache purging, I’d recommend a setup that minimizes the need to purge at all, and when you do purge, makes it precise and low-risk.
Recommended CDN setup
1) Use versioned image URLs whenever possible
Best practice is to avoid overwriting the same image path.
- Example:
/images/article-123/hero.v7.jpg- or
/images/hash/hero.8f3a1c.jpg
This lets you set very long cache lifetimes on the CDN because a changed image gets a new URL. That’s the safest way to publish frequently without worrying about stale content.
2) Put a CDN in front of an origin that supports cache tags
If you need to purge groups of images, use a CDN that supports tag-based purging or surrogate keys.
Good options:
- Fastly: very strong surrogate-key purging
- Cloudflare: supports cache purge by URL, prefix, and some advanced rules/workflows
- Akamai: enterprise-grade invalidation options
- Bunny CDN: simpler, cost-effective; purging is usually URL-based
For a publishing workflow, Fastly + surrogate keys is especially nice because you can tag all assets related to an article, author, or gallery and purge just that set.
3) Set long cache TTLs for images
For immutable image URLs:
Cache-Control: public, max-age=31536000, immutable
That makes the CDN and browsers keep images for a long time, which is ideal for performance.
4) Use cache purging by key/tag, not wildcard purge if possible
Safer purge strategy:
- Purge only the article/gallery/item that changed
- Avoid broad purges of
/images/*unless absolutely necessary
If your platform publishes often, broad purges can create:
- cache stampedes
- origin load spikes
- temporary stale/missing assets
5) Separate cache behavior by asset type
A common pattern:
- Images: long TTL, versioned URLs, purge by tag if needed
- HTML pages: shorter TTL or stale-while-revalidate
- API / dynamic content: usually no CDN cache unless carefully controlled
6) Enable origin shielding / tiered caching
This reduces load when many edges miss at once, especially after publishing.
Look for:
- Origin Shield (Cloudflare, Fastly, Akamai variants)
- Tiered cache / shield POP
This is especially helpful for image-heavy sites because a new publish can trigger lots of revalidation.
7) Use stale-while-revalidate / stale-if-error
These directives help keep images and pages available while cache refresh happens in the background.
Example:
Cache-Control: public, max-age=86400, stale-while-revalidate=3600, stale-if-error=86400
For images, this is usually less critical if URLs are versioned, but it still helps resilience.
Best-practice architecture
If you want the safest, most scalable model:
- Store originals in object storage (S3, R2, GCS)
- Serve through a CDN
- Generate versioned filenames at publish time
- Use tag-based purging only for rare exceptions
- Keep HTML separate from image caching rules
My practical recommendation
If you want the simplest reliable choice:
Cloudflare + versioned image URLs + long TTLs + targeted purge rules
If you want the strongest purge control for a publishing workflow:
Fastly + surrogate keys + versioned URLs
Example strategy
When an article is updated:
- New hero image uploaded as a new filename/hash
- Article HTML updated to reference the new URL
- Purge only the article HTML and any tagged related assets
- Do not purge the entire image library
Bottom line
The safest setup is:
- Versioned immutable image URLs
- Long CDN/browser cache TTLs
- Tag-based or key-based purge
- Avoid broad wildcard purges
- Use shielding/tiered cache
If you want, I can also suggest a setup tailored to a specific stack, like WordPress, Next.js, Cloudflare Images, S3 + CloudFront, or Fastly.