Cache Behavior Configuration
What you'll learn: How to define rules that tell your CDN which content to cache, for how long, and where to send requests based on URL patterns.
The Core Idea
Cache behavior configuration is like setting up traffic rules for your CDN. Instead of treating all requests the same way, you define specific patterns that tell the CDN: "When someone requests a URL that looks like this, cache it for this long and apply these settings."
Think of it like organizing a library. You wouldn't store children's books, reference materials, and newspapers using the same checkout rules. Similarly, your CDN shouldn't treat your homepage HTML, product images, and API responses identically.
How Cache Behaviors Work
A cache behavior typically includes:
- Path pattern: Which URLs this rule applies to (e.g.,
/images/*,/api/*,*.jpg) - Origin target: Which backend server to fetch from
- Cache policy: How long to cache (TTL), which headers to respect
- Compression settings: Whether to compress responses
- Query string handling: Whether to include query parameters in cache keys
Common Configuration Patterns
Static assets like images or CSS might use:
- Pattern:
/assets/* - Long TTL (days or weeks)
- Strip query strings
API endpoints might use:
- Pattern:
/api/* - Short or no caching
- Forward all headers
- Pass through query parameters
Default behavior catches everything else:
- Pattern:
*(wildcard) - Medium TTL with cache control headers respected
Why This Matters
Without proper cache behaviors, you risk caching dynamic content that should always be fresh, or failing to cache static assets that could dramatically reduce your origin load. Different content types need different treatment.
Key Takeaway: Cache behavior configuration lets you create routing rules that apply different caching, compression, and origin settings based on URL patterns—optimizing performance and cost for each content type.