Tuần 5 - Ngày 1: EC2 Advanced Concepts
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
4 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 (giảm interruption nhất), (4) capacityOptimizedPrioritized — ưu tiên pools bạn chỉ định nhưng vẫn optimize capacity.
capacityOptimizedlà recommended cho production workloads quan trọng. -
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.
Tài liệu tham khảo chính thức
Ngày tiếp theo: Containers (ECS, EKS, Fargate)