⭐⭐⭐⭐ Very Emphasized for Optimization

🌐 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 Reduction

🌍 What is CloudFront and Why Use It?

CDN (Content Delivery Network)
A network of servers distributed globally. When a user requests content, it's served from the server closest to them (called an "edge location" or "Point of Presence"), not from a centralized data center. This reduces the physical distance data must travel, reducing latency dramatically.
Without CloudFront vs With CloudFront
πŸ‡―πŸ‡΅User in Tokyo
β†’ 200ms round trip β†’
πŸ‡ΊπŸ‡ΈS3 in US-East-1
πŸ‡―πŸ‡΅User in Tokyo
β†’ 5ms β†’
πŸ™οΈCloudFront Tokyo EdgeCache HIT! Served locally

πŸ”‘ 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

Cache Behavior
Rules that define HOW CloudFront handles requests for different URL patterns. You can have different cache settings for different paths. Example: /images/* can be cached for 7 days. /api/* bypasses cache completely (TTL=0). /static/* cached for 30 days.
TTL (Time-to-Live)
How long CloudFront keeps a cached object before fetching a fresh copy from origin. Set in cache behaviors using Min TTL, Max TTL, and Default TTL. The origin can also control TTL via Cache-Control and Expires HTTP headers in its response.

πŸ“Š TTL Strategy by Content Type

Content TypeRecommended TTLWhy
Static images, fonts, CSS1–30 daysRarely changes; long TTL = high cache hit rate
JavaScript bundles (versioned)1 yearNew deployments use new file names β†’ old cache fine
HTML pagesMinutes to hoursContent changes periodically
API responses (static data)MinutesBalance freshness vs. caching benefit
Dynamic API responses0 (no cache)Each user gets different personalized data
Login/authenticated pages0 (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.

🎯 Critical Exam Pattern β€” S3 Throttling
Scenario: "Your S3 bucket is receiving 503 SlowDown errors under high traffic. Users are repeatedly requesting the same files."
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.

πŸ’‘ Cache Invalidation
When you update content in S3, CloudFront's cached copies are still the old version until TTL expires. To force immediate refresh: create a CloudFront invalidation (forces edge locations to fetch fresh copies from origin). Invalidating /* 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.