Chương 09: CloudFront & Caching
Tổng Quan
Amazon CloudFront là dịch vụ CDN (Content Delivery Network) toàn cầu, phân phối nội dung qua 400+ Edge Locations trên toàn thế giới. Bạn đã quen với CloudFront — chương này bổ sung chi tiết nâng cao cho kỳ thi.
1. CloudFront Cơ Bản
1.1 Cách hoạt động
User (Vietnam) ──request──▶ Edge Location (Singapore)
│
Cache hit? ──Yes──▶ Return content (nhanh!)
│
No
│
Origin Server (us-east-1)
│
Return + Cache at Edge
1.2 Origins (Nguồn nội dung)
| Origin | Mô tả |
|---|---|
| S3 Bucket | Phân phối static files. Dùng Origin Access Control (OAC) để bảo mật. |
| ALB / EC2 | Dynamic content (API, web app). |
| Custom HTTP Origin | Bất kỳ HTTP server nào. |
| MediaStore / MediaPackage | Video streaming. |
1.3 Origin Access Control (OAC)
- Thay thế Origin Access Identity (OAI) — cũ.
- Đảm bảo S3 bucket chỉ được truy cập qua CloudFront (không truy cập S3 URL trực tiếp).
- Hỗ trợ SSE-KMS encryption (OAI không hỗ trợ).
2. CloudFront vs S3 Cross-Region Replication
| CloudFront | S3 CRR | |
|---|---|---|
| Mạng lưới | 400+ Edge Locations toàn cầu | Chỉ các Regions bạn chọn |
| Cacheable | ✅ Cache tại Edge (TTL) | ❌ Mỗi Region có full copy |
| Use case | Static content phân phối toàn cầu | Replication real-time cần ở regions cụ thể |
| Update | Has TTL (có thể stale) | Near real-time, always up-to-date |
3. Signed URL / Signed Cookies
Kiểm soát ai có thể truy cập nội dung qua CloudFront.
| Signed URL | Signed Cookies | |
|---|---|---|
| Scope | 1 file cụ thể | Nhiều files (VD: toàn bộ video course) |
| Use case | Download 1 file premium | Truy cập nhóm restricted content |
CloudFront Signed URL ≠ S3 Pre-signed URL:
- CloudFront Signed URL: Truy cập qua CloudFront distribution, có caching.
- S3 Pre-signed URL: Truy cập trực tiếp S3, dùng IAM credentials của người tạo URL.
4. CloudFront Functions vs Lambda@Edge
| CloudFront Functions | Lambda@Edge | |
|---|---|---|
| Runtime | JavaScript | Node.js, Python |
| Thời gian chạy | < 1ms | 5-30 giây |
| Trigger | Viewer Request/Response | Viewer + Origin Request/Response |
| Scale | Millions req/s | Thousands req/s |
| Network/File access | ❌ | ✅ |
| Use case | URL rewrite, header manipulation, cache key | Authentication, image transformation, A/B testing |
Client ──▶ [CF Function: Viewer Request] ──▶ CloudFront ──▶ [Lambda@Edge: Origin Request] ──▶ Origin
Client ◀── [CF Function: Viewer Response] ◀── CloudFront ◀── [Lambda@Edge: Origin Response] ◀── Origin
5. CloudFront Caching & Invalidation
5.1 Cache Key
- Mặc định: hostname + resource path.
- Tùy chỉnh bằng Cache Policy: thêm headers, cookies, query strings vào cache key.
5.2 Cache Invalidation
- Xóa cache tại Edge Locations trước khi TTL hết hạn.
- Path:
/*(toàn bộ) hoặc/images/*(chỉ folder cụ thể). - Tính phí cho mỗi invalidation request.
- Best practice: Dùng versioned file names (
style-v2.css) thay vì invalidation.
6. AWS Global Accelerator
| CloudFront | Global Accelerator | |
|---|---|---|
| Mục đích | Cache content tại Edge | Tối ưu network path đến application |
| Protocol | HTTP/HTTPS | TCP, UDP |
| Caching | ✅ | ❌ |
| IP | DNS-based | 2 Anycast static IPs |
| Use case | Static/dynamic web content | Gaming, IoT, VoIP, HTTP cần static IP |
💡 Exam Tip: Cần static IP + global performance → Global Accelerator. Cần cache + CDN → CloudFront.
Exam Tips 💡
- S3 + CloudFront + OAC là combo chuẩn cho static website phân phối toàn cầu.
- Signed URL cho 1 file, Signed Cookies cho nhiều files.
- CloudFront Functions cho logic đơn giản (URL rewrite). Lambda@Edge cho logic phức tạp (auth, image resize).
- Invalidation tốn phí → dùng versioned file names thay thế.
- Global Accelerator ≠ CloudFront. GA tối ưu network, không cache.
Câu Hỏi Ôn Tập 📝
Câu 1: Bạn muốn bảo mật S3 bucket chỉ cho phép truy cập qua CloudFront. Dùng gì?
Xem đáp án
Origin Access Control (OAC) trên CloudFront + S3 Bucket Policy chỉ cho phép CloudFront distribution.
Câu 2: Ứng dụng video streaming cần cung cấp truy cập premium cho subscriber. Mỗi subscriber truy cập nhiều video. Dùng gì?
Xem đáp án
CloudFront Signed Cookies. Cho phép truy cập nhiều files (videos) cùng lúc, phù hợp cho nhóm restricted content.
Câu 3: Ứng dụng gaming cần global performance và 2 static IP addresses. CloudFront hay Global Accelerator?
Xem đáp án
AWS Global Accelerator. Cung cấp 2 Anycast static IPs, tối ưu network path cho TCP/UDP, phù hợp gaming.