Prompt
How do I cache images and videos closer to users with a CDN?
Latest observation
Oct 12, 2026
OpenAI APIWeb search: off
To cache images and videos closer to users with a CDN, you typically put a CDN in front of your media origin so users fetch files from edge locations instead of your main server or object storage bucket.
Basic setup
-
Store media in a durable origin
- Common choices: S3, Azure Blob, GCS, or your own media server.
- This is the source of truth.
-
Put a CDN in front
- Examples: Cloudflare, Fastly, CloudFront, Akamai, Bunny, etc.
- The CDN caches copies of files at edge nodes near users.
-
Serve media through CDN URLs
- Instead of:
https://your-bucket.s3.amazonaws.com/photo.jpg
- Use:
https://cdn.example.com/photo.jpg
- Instead of:
-
Set cache headers correctly
- For static media, use long-lived caching:
Cache-Control: public, max-age=31536000, immutable
- For files that may change, use versioned filenames:
photo.v2.jpg
- Then cache aggressively.
- For static media, use long-lived caching:
For images
Best practices
- Use responsive image variants:
- small, medium, large
- or use dynamic resizing on the CDN
- Prefer modern formats:
- WebP, AVIF
- Enable:
- compression
- image optimization
- lazy loading
Cache strategy
- Version image URLs when changing content:
avatar.abc123.webp
- Use long TTLs for immutable assets.
- Purge/invalidate only when necessary.
For videos
Best practices
- Serve videos as streaming-friendly formats
- HLS (
.m3u8+.tsor.m4s) - DASH (
.mpd)
- HLS (
- CDN caches:
- manifest files
- video segments/chunks
- Use adaptive bitrate streaming so users get smooth playback.
Cache strategy
- Cache manifests briefly if they change often.
- Cache segments longer if they are immutable.
- Split large videos into chunks so the CDN can cache and reuse them efficiently.
Important headers
Typical CDN-friendly headers:
Cache-Control: public, max-age=31536000, immutable
ETag: "abc123"
Content-Type: image/jpeg
For files that update:
Cache-Control: public, max-age=3600, must-revalidate
Invalidation approach
When a file changes, you have two options:
- Version the filename
Best option. Example:banner.2026-01-01.jpg
- Purge the CDN cache
- Useful if the URL must stay the same
- Can be slower and more operationally expensive
Example with S3 + CloudFront
- Upload images/videos to S3.
- Create a CloudFront distribution with S3 as origin.
- Point
cdn.example.comto CloudFront. - Configure object metadata or origin response headers for caching.
- Use CloudFront URLs in your app.
- Optionally enable:
- compression
- signed URLs/cookies for private media
- image optimization
Things to watch out for
- Don’t cache personalized media publicly.
- Avoid query-string cache fragmentation unless needed.
- Make sure CDN and origin support correct
Rangerequests for video seeking. - Don’t use short cache times for immutable assets.
- Make sure file names change when the content changes.
Rule of thumb
- Images: cache long, version filenames, use optimization and responsive variants.
- Videos: use streaming segments, cache manifests and chunks, and support range requests.
If you want, I can give you:
- a Cloudflare setup,
- an AWS CloudFront + S3 setup, or
- a sample cache-header configuration for images/videos.