Dynamic Content Caching at Edge
What you'll learn: How to cache personalized or frequently changing content at CDN edges using short TTLs and stale-while-revalidate patterns.
The Challenge with Dynamic Content
Traditional edge caching works beautifully for static assets—images, CSS, JavaScript—but what about content that changes per user or updates frequently? Product prices, inventory counts, personalized recommendations, and API responses can't sit in cache for hours. Yet even a few seconds of caching can dramatically reduce origin load when thousands of users request the same "dynamic" data simultaneously.
Core Strategy: Aggressive but Brief Caching
The key is caching dynamic content with very short Time-To-Live (TTL) values—often just seconds or a few minutes. A product page might cache for 30 seconds. During flash sales or traffic spikes, those 30 seconds mean thousands of requests hit cache instead of your database.
Cache-Control: public, max-age=30, s-maxage=30
Stale-While-Revalidate Pattern
This powerful pattern serves slightly stale content while fetching fresh data in the background:
Cache-Control: max-age=30, stale-while-revalidate=300
Here, content is fresh for 30 seconds. After that, the CDN can serve stale content for up to 5 more minutes while asynchronously requesting an update from your origin. Users get instant responses; your origin handles far fewer requests.
Think of it like a newspaper stand: The vendor displays yesterday's paper (stale but readable) while waiting for today's delivery truck. Customers aren't left waiting; they get something immediately.
When to Use This
Perfect for:
- Pricing and inventory with acceptable staleness windows
- Leaderboards and counters
- Personalized feeds with short freshness requirements
- API responses with high read-to-write ratios
Key Takeaway: Dynamic content can still benefit from edge caching by using short TTLs (seconds to minutes) and stale-while-revalidate patterns that serve slightly outdated content while refreshing asynchronously, dramatically reducing origin load during traffic spikes.