SimuVedaSystem Design Lab Open in the lab

Distributed patterns / Beginner / 5 min / Updated

CDN edge caching: TTL, cache keys, purge and stale content

Tune the TTL, break the cache key with cookies, purge on deploy and turn on stale-while-revalidate. Watch hit ratio, origin load and staleness.

Start the simulation → Change the load, break a part, watch what happens. Free, no sign-up.

In short

What is CDN caching?

A CDN caches responses at edge servers near users for a TTL, so most requests never reach the origin.

Why does it matter?

It cuts latency and origin load for static assets, images, and cacheable pages and APIs.

What is the trade-off?

Long TTLs mean high hit ratios but stale content unless you purge; bad cache keys silently kill hit rates.

How do you explain it in an interview?

"Assets fingerprinted with a 1-year TTL, HTML 60 s with stale-while-revalidate, purge on deploy."

Check yourself

What does a longer CDN TTL trade?

Answer: Fewer origin requests for content that can be stale longer. Copies live longer at the edge, so the origin sees less traffic, but changes take longer to show unless you purge.

Why can a session cookie in the cache key ruin the hit ratio?

Answer: Every user gets a separate copy, so almost every request is a miss. The cache key decides what counts as the same object; per-user keys mean nothing is shared.

What does stale-while-revalidate do?

Answer: Serves the expired copy at once and refreshes it in the background. Users do not wait on the origin when a popular object expires.

See it happen in the simulation →

cdnedge cachettlcache keypurgestale-while-revalidatecloudflaredistributed patterns

More in distributed patterns