Mục tiêu học tập
- Hiểu sâu về EC2 pricing và optimization
- Nắm vững Spot Instances strategies
- Biết cách design cho cost và performance
1. EC2 Purchase Options Deep Dive
On-Demand vs Reserved vs Spot
2. Spot Instances Strategies
Spot Fleet
Spot Instance Interruption Handling
Interruption Warning (2 minutes):
1. CloudWatch Event / EventBridge
2. Instance metadata check
3. Graceful shutdown logic
Best Practices:
- Use Auto Scaling with mixed instances
- Checkpointing for long-running jobs
- SQS for job queues (visibility timeout)
- Diversify across instance types and AZs
3. EC2 Fleet
Mixed Instances Policy
{
"LaunchTemplateConfigs": [{
"LaunchTemplateSpecification": {
"LaunchTemplateId": "lt-xxxxx",
"Version": "$Latest"
},
"Overrides": [
{"InstanceType": "c5.large", "WeightedCapacity": 2},
{"InstanceType": "c5.xlarge", "WeightedCapacity": 4},
{"InstanceType": "m5.large", "WeightedCapacity": 2}
]
}],
"TargetCapacitySpecification": {
"TotalTargetCapacity": 100,
"OnDemandTargetCapacity": 20,
"SpotTargetCapacity": 80,
"DefaultTargetCapacityType": "spot"
}
}
4. Capacity Reservations
Types
On-Demand Capacity Reservations:
- Reserve capacity in specific AZ
- No term commitment
- Charged whether used or not
- Can share via AWS RAM
Capacity Reservation + Savings Plans/RI:
- Apply discounts to capacity reservations
- Best of both worlds
5. EC2 Placement Strategies
Comparison
| Strategy | Use Case | Limitation |
|---|---|---|
| Cluster | HPC, low latency | Same rack, single AZ |
| Spread | Critical instances | Max 7 per AZ |
| Partition | HDFS, Cassandra | Max 7 partitions per AZ |
6. Instance Families & Graviton
So sánh Graviton generations
| Generation | Families | Performance vs prev | Use case chính |
|---|---|---|---|
| Graviton2 (2019) | m6g, c6g, r6g, t4g | Baseline (vs x86) | Web app, containers, dev/test — cost-conscious |
| Graviton3 (2022) | m7g, c7g, r7g | +25% perf, +60% energy efficiency | General-purpose production workload |
| Graviton4 (2024) | m8g, c8g, r8g, x8g | +30% perf vs Graviton3 | Database (Aurora, RDS, MemoryDB), in-memory cache, large containers |
Graviton talking points cho SAP-C02:
- ARM-based — cần binary compile lại (hầu hết runtime hiện đại đã hỗ trợ: JVM, Python, Node.js, .NET 6+, Go, Rust). Native x86 binary KHÔNG chạy được.
- Pricing: 20-40% rẻ hơn x86 tương đương về vCPU/RAM.
- Sustainability Pillar: ARM = ít năng lượng hơn ⇒ scenario "giảm carbon footprint" thường có Graviton là đáp án đúng.
- Workload hợp nhất: stateless web tier, container (EKS/ECS với Fargate Graviton), open-source database (PostgreSQL, MySQL trên Aurora Graviton), Redis/Memcached.
- KHÔNG dùng cho: legacy app x86-only, GPU workload (vẫn dùng p4/p5), high-performance Windows-on-EC2 (ARM Windows support hạn chế).
Exam keyword
| Câu hỏi gợi | Đáp án |
|---|---|
| "Reduce cost AND carbon footprint cho web tier" | Graviton (m7g/m8g) hoặc Fargate Graviton |
| "Workload x86 + cần migrate sang ARM, app modern (Java/Python/Node)" | Graviton — recompile + test |
| "Legacy .NET Framework Windows app" | x86 (Graviton chưa support Windows .NET Framework đầy đủ) |
7. Câu hỏi ôn tập
-
Spot Fleet allocation strategies là gì?
Xem đáp án
5 strategies: (1) lowestPrice — launch từ pool có giá thấp nhất (rủi ro interruption cao hơn), (2) diversified — spread instances across tất cả specified pools (giảm interruption), (3) capacityOptimized — launch từ pool có capacity nhất, (4) capacityOptimizedPrioritized — ưu tiên pools bạn chỉ định nhưng vẫn optimize capacity, (5) priceCapacityOptimized — cân bằng giá và capacity. AWS hiện recommend
priceCapacityOptimizedcho hầu hết workloads (thay chocapacityOptimizedđược recommend trước đây) — đây cũng là đáp án cho câu hỏi "recommended strategy" trên đề hiện hành. -
Làm sao handle Spot interruption?
Xem đáp án
(1) Poll Instance Metadata cho
/latest/meta-data/spot/termination-timemỗi 5 giây — 2 phút trước terminate, (2) EventBridge rule listen cho EC2 Spot Instance Interruption Warning event, (3) Graceful shutdown: checkpoint work, drain connections, flush state to S3/EFS/DynamoDB, (4) ASG với Mixed Instances — replace Spot với On-Demand fallback tự động, (5) SQS Visibility Timeout > processing time để message không bị reprocessed sau interrupt. -
EC2 Fleet khác Spot Fleet như thế nào?
Xem đáp án
Spot Fleet: chỉ manage Spot Instances (và có thể thêm On-Demand). EC2 Fleet: manage Spot + On-Demand + Reserved Instances theo target capacity (vCPUs, memory, units). EC2 Fleet flexible hơn — có thể specify mix ratio (ví dụ: 80% Spot, 20% On-Demand), multiple instance types, và các allocation strategies. Cũng hỗ trợ instant request type (không maintain fleet, chỉ launch). EC2 Fleet là superset của Spot Fleet capabilities.
-
Capacity Reservations dùng khi nào?
Xem đáp án
Dùng khi cần đảm bảo EC2 capacity trong một AZ cụ thể: (1) Disaster Recovery — cần biết capacity sẵn sàng khi cần failover, (2) Compliance — phải có instances available trong specific AZ, (3) Planning large events — Black Friday, product launches cần guaranteed scale out. Tính phí On-Demand rate kể cả khi không dùng. Kết hợp với Savings Plans hoặc Zonal RI để vừa có capacity vừa có discount.
8. Bài tập thực hành
-
Tạo ASG với Mixed Instances Policy: tạo Launch Template, cấu hình Auto Scaling Group mix 20% On-Demand / 80% Spot với allocation strategy
priceCapacityOptimized, tối thiểu 3 instance types (ví dụ c5.large, c6i.large, m5.large). Quan sát trong console fleet chọn pool nào và tại sao. -
Xử lý Spot interruption: viết script (user data hoặc systemd service) poll
http://169.254.169.254/latest/meta-data/spot/instance-actionmỗi 5 giây; khi có warning, log ra CloudWatch Logs và mô phỏng graceful shutdown (drain job, upload checkpoint lên S3). Có thể test bằng Fault Injection Service (FIS) actionaws:ec2:send-spot-instance-interruptions. -
So sánh chi phí Graviton: launch song song m5.large (x86) và m7g.large (Graviton3), chạy cùng một benchmark đơn giản (ví dụ
sysbench cpu), so sánh price/performance. Ghi lại kết luận cho scenario "reduce cost and carbon footprint".
Tài liệu tham khảo chính thức
Ngày tiếp theo: Containers (ECS, EKS, Fargate)