4.3.5 Cache Content Based on Request Headers

Cache Content Based on Request Headers

CloudFront Caching

What AWS service helps optimize availability and caching?

CloudFront có thể cache content based on request headers để separate versions của specified object based on header values trong viewer request.

CloudFront Cache Key

ComponentInclude?Effect
URL pathAlwaysBase cache key
Query stringsConfigurableMore = lower hit ratio
HeadersConfigurableMore = lower hit ratio
CookiesConfigurableMore = lower hit ratio

Cache Policies

PolicyMô tả
CachingOptimizedMinimal cache key = best hit ratio
CachingDisabledNo caching
CustomSelect specific headers/query strings

Header-Based Caching

Cache-Control: max-age=3600
→ CloudFront caches for 1 hour

Vary: Accept-Encoding
→ Separate cache entries per encoding (gzip, br)

Vary: Accept-Language
→ Separate cache entries per language

Use case: Serve different versions based on:

  • Device type (mobile vs desktop)
  • Language preference
  • Encoding support
  • Geographic location

API Gateway Caching

  • Stage-level, TTL 0-3600s (default 300s)
  • Cache capacity: 0.5GB → 237GB
  • Cache key: Query strings, headers
  • Invalidation: Cache-Control: max-age=0

Exam Tip:

  • CloudFront cache content based on request headers
  • More items in cache key = lower hit ratio
  • CachingOptimized = best performance
  • API Gateway cache = stage-level
  • Invalidation requires authorization