π Amazon CloudFront
CloudFront is AWS's Content Delivery Network (CDN). It caches your content at 400+ edge locations worldwide, so users get responses from a nearby server instead of your origin (S3, ALB, API GW). Result: lower latency, less load on your origin, and global performance.
π Covers: Edge Caching Β· Origins Β· Cache Behaviors Β· TTL Β· S3 Throttle ReductionWhat is CloudFront and Why Use It?
π Key CloudFront Components
π’ Origin
The original source of your content. Examples: S3 bucket, ALB, API Gateway, HTTP server, EC2 instance. CloudFront fetches content from origin on cache miss.
π Edge Location
CloudFront's globally distributed servers (~400+). User requests go to the nearest edge location. If content is cached there, it's served immediately (cache hit). If not, CloudFront fetches from origin and caches it (cache miss).
π Distribution
A CloudFront configuration. Defines: which origin to use, which edge locations are active, cache behaviors, TTL settings, HTTPS, WAF rules, etc.
Cache Behaviors and TTL
π TTL Strategy by Content Type
| Content Type | Recommended TTL | Why |
|---|---|---|
| Static images, fonts, CSS | 1β30 days | Rarely changes; long TTL = high cache hit rate |
| JavaScript bundles (versioned) | 1 year | New deployments use new file names β old cache fine |
| HTML pages | Minutes to hours | Content changes periodically |
| API responses (static data) | Minutes | Balance freshness vs. caching benefit |
| Dynamic API responses | 0 (no cache) | Each user gets different personalized data |
| Login/authenticated pages | 0 (no cache) | Never cache per-user data |
π Request-Header-Based Caching
CloudFront can cache different versions of the same URL based on request headers. Example: Cache one version for Accept-Language: en and another for Accept-Language: fr. Useful for language-specific content without changing URLs.
Answer: Place CloudFront in front of S3. CloudFront caches the frequently requested files at edge locations. Repeated requests are served from cache and never hit S3, eliminating throttling.
This is one of the most repeated exam patterns for CloudFront. The connection is: CloudFront β reduces S3 load β prevents throttling.
CloudFront + S3 β Static Content Optimization
The most common CloudFront use case is serving static content from S3 globally with low latency. Here's how to set it up correctly:
π Origin Access Control (OAC)
Keep your S3 bucket private. Only CloudFront can access it. Users cannot bypass CloudFront and go directly to S3. This ensures security and that all requests are logged/cached properly.
π¦ S3 Transfer Acceleration vs CloudFront
S3 Transfer Acceleration speeds up UPLOADS to S3. CloudFront speeds up DOWNLOADS from S3. For distributing content to global users β CloudFront. For users uploading large files to S3 β Transfer Acceleration.
/* invalidates all files but costs $0.005 per 1000 paths. Better practice: use versioned file names (bundle.v2.js) so new files bypass old cache automatically.