簡介
如果伺服器位於新加坡,河內的使用者必須等待每個請求約 50 毫秒的往返時間。美國用戶必須等待約 200 毫秒。 CDN 透過將內容盡可能靠近使用者來解決這個問題。
1.什麼是CDN?
CDN 是一個全球分散式代理伺服器網絡,從距離用戶最近的位置提供內容。
Không có CDN:
User (Hà Nội) ──── 200ms ────► Origin Server (US)
Có CDN:
User (Hà Nội) ──── 10ms ────► CDN Edge (Hà Nội)
│ cache miss
▼
CDN Edge ── 200ms ──► Origin Server (US)
│ cache result
▼
User (Hà Nội) ──── 10ms ────► CDN Edge (Hà Nội) ← Lần sau = 10ms!
1.1 CDN架構
Origin Server
│
┌──────────┼──────────┐
▼ ▼ ▼
┌────────┐ ┌────────┐ ┌────────┐
│Regional│ │Regional│ │Regional│
│ PoP │ │ PoP │ │ PoP │
│ (Asia) │ │ (EU) │ │ (US) │
└───┬────┘ └───┬────┘ └───┬────┘
┌───┴───┐ ┌───┴───┐ ┌───┴───┐
│Edge │ │Edge │ │Edge │
│Servers│ │Servers│ │Servers│
└───────┘ └───────┘ └───────┘
▲ ▲ ▲
Users Users Users
2. 推送 CDN 與拉取 CDN
2.1 推送CDN
Origin Server ──push──► CDN Edge
Khi content thay đổi, origin chủ động push lên CDN
Flow:
1. Developer deploy new image
2. CI/CD pipeline push image to CDN
3. CDN distributes to all edges
4. User request → Edge server → Return immediately
| 優勢 | 缺點 |
|---|---|
| 內容始終可用 | 必須管理推播流程 |
| 沒有「第一次請求」延遲 | 消耗所有邊緣的儲存空間 |
| 完全控制 | 內容變化較大時會變得複雜 |
最適合: 幾乎沒有小變化的靜態內容
2.2 拉取CDN
User ──request──► CDN Edge
Edge: "Tôi không có content này"
Edge ──request──► Origin Server
Origin ──response──► Edge
Edge: cache content + return to user
Lần sau:
User ──request──► CDN Edge
Edge: "Tôi có rồi!" → Return from cache
| 優勢 | 缺點 |
|---|---|
| 自動,無需管理 | 第一次請求很慢(快取未命中) |
| 僅請求快取內容 | 需要TTL策略 |
| 輕鬆設定 | 快取踩踏風險 |
最適合: 動態內容、高流量
3. 快取失效策略
3.1 TTL(存活時間)
CDN cache product image với TTL = 1 giờ
T=0: User request → Cache MISS → Fetch from origin → Cache 1 giờ
T=30m: User request → Cache HIT → Return cached
T=61m: Cache expired → Fetch from origin → Cache lại
Trade-off: TTL ngắn = fresh hơn nhưng nhiều origin hits
TTL dài = nhanh hơn nhưng content có thể cũ
3.2 快取清除
CDN API: DELETE /cache?url=https://blog.xdev.asia/images/logo.png
→ Xóa content khỏi tất cả edge servers
Use case: Cập nhật logo, fix typo trong hình ảnh
3.3 快取清除
Thay đổi URL khi content thay đổi:
Trước: /style.css
Sau: /style.css?v=2
Hoặc: /style.a1b2c3d4.css (hash trong filename)
→ CDN coi đây là content mới → fetch from origin
3.4 重新驗證時過時
Cache expired, nhưng:
1. Trả cached content ngay (stale)
2. Đồng thời fetch fresh content từ origin
3. Update cache khi origin respond
→ User luôn nhận response nhanh
→ Content cuối cùng sẽ fresh
4. 邊緣運算
Traditional CDN:
Edge chỉ serve static files
Edge Computing:
Edge chạy code (logic) tại edge location
Ví dụ Cloudflare Workers:
- A/B testing tại edge
- Geolocation-based redirects
- Authentication tại edge
- Image resizing tại edge
- API response transformation
5. 比較 CDN 供應商
| 特點 | 雲端火炬 | AWS CloudFront | AWS CloudFront快點 |
|---|---|---|---|
| PoP | 300+ | 450+ | 90+ |
| 邊緣運算 | 工人 | Lambda@Edge | 運算@邊緣 |
| 免費套餐 | 慷慨 | 有限公司 | 無 |
| 價格 | $ | $$ | $$$ |
| DDoS 防護 | 內建 | AWS 盾 | 有限公司 |
| 最適合 | 普通、中小型 | AWS 生態系統 | 效能關鍵 |
6.CDN 反模式
| 反模式 | 問題 | 解決方案 |
|---|---|---|
| 快取個人化內容 | 使用者 A 查看資料 使用者 B | 快取控制:私有 |
| API 的 TTL 太長 | 資料過時 | 短 TTL + 重新驗證時過時 |
| API 請勿使用 CDN | 遠端 API 速度慢 | 在邊緣快取 GET API |
| CDN 無所不包 | 透過 CDN 發佈/放置/刪除 | CDN 僅適用於 GET 請求 |
總結
| 概念 | 重點 |
|---|---|
| CDN | 從最靠近使用者的位置提供內容 |
| 推與拉 | 推為靜態,拉為動態 |
| 快取失效 | TTL + 清除 + 快取清除 |
| 邊緣運算 | 在邊緣運行邏輯,減少延遲 |
練習
-
CDN策略: 為電子商務網站設計CDN策略:產品圖片(很少變化)、產品價格(經常變化)、使用者畫像(個人化)。
-
**快取失效:**部落格平台需要在作者編輯後立即更新貼文。設計緩存失效策略。
-
成本估算: 網站每天有 10M 頁面瀏覽量,每個頁面 2MB(500KB HTML + 1.5MB 圖片)。估計每月 CDN 頻寬和成本(CloudFront:0.085 美元/GB)。