Course contentsShow
← System Design
Lesson 1570 of 1,91938. Real-World System: PastebinFree lesson

CDN Cache Control Headers

Setting appropriate Cache-Control, Expires, and ETag headers to align CDN caching with paste expiration policies.

CDN Cache Control Headers

What you'll learn: How to use HTTP headers to tell your CDN exactly how long to cache pastes and when to revalidate them, ensuring expired content doesn't linger at the edge.

The Cache Alignment Challenge

You've integrated a CDN to deliver paste content faster, but now you face a critical problem: how does the CDN know when a paste has expired? Without proper instructions, the CDN might serve a paste that should have been deleted hours ago, or constantly fetch fresh copies when it could safely cache them.

HTTP cache control headers are your communication protocol with the CDN, telling it your caching intentions.

Essential Headers for Pastebin

Cache-Control: The primary directive. For a paste with 24-hour TTL:

Cache-Control: public, max-age=86400

This tells the CDN "anyone can cache this, and it's fresh for 86,400 seconds (24 hours)."

For private pastes:

Cache-Control: private, no-store

Expires: A fallback for older systems. Set an absolute timestamp:

Expires: Wed, 21 Oct 2025 07:28:00 GMT

This provides the exact moment the cached copy becomes stale.

ETag: A content fingerprint. When the CDN revalidates, it sends the ETag back. If your server's ETag matches, you respond 304 Not Modified instead of resending the entire paste — saving bandwidth.

ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"

Aligning with Expiration Policies

If your paste expires in 7 days, set max-age=604800. When cleanup jobs delete expired pastes from storage, the CDN's cached copies naturally age out at the same time. No orphaned content, no premature eviction.

Key Takeaway: Set Cache-Control, Expires, and ETag headers that match your paste TTL to ensure CDNs cache content for the right duration, revalidate efficiently, and respect your expiration policies without serving stale data.