</>Học Dev
Bài học

Ngày 5: EC2 và Auto Scaling Review

Tuần 1 – Ngày 5

Ngày 5: EC2 và Auto Scaling Review

Mục tiêu học tập

  • Ôn tập EC2 instance types và pricing models
  • Hiểu sâu về Auto Scaling Groups
  • Nắm vững các scaling strategies

1. EC2 Instance Types

Naming Convention

m5.xlargeSize:nano,micro,small,medium,large,xlarge,2xlarge...Generation:scàngcaocàngmiFamily:m(general),c(compute),r(memory),etc.

Instance Families

FamilyOptimized forUse Cases
M (General)BalancedWeb servers, app servers
C (Compute)CPUBatch processing, gaming
R (Memory)RAMIn-memory databases, caching
X (Memory)Extreme RAMSAP HANA, large databases
I (Storage)High I/ONoSQL, data warehousing
D (Dense)HDD storageDistributed file systems
G/P (Accelerated)GPUML, graphics rendering
T (Burstable)Burst CPUVariable workloads

T Instance và CPU Credits

T3InstanceCPUCreditSystem:CPUUsage100%BurstSdngcredits20%BaselineEarncredits0%TimeKhihếtcredits:-T3Standard:Bgiihnbaseline-T3Unlimited:Tiếptcburst,trthêmphí

2. EC2 Pricing Models

So sánh các pricing models

ModelDiscountCommitmentBest For
On-Demand0%NoneShort-term, unpredictable
Reserved30-72%1 or 3 yearsSteady-state workloads
Savings Plans30-72%1 or 3 yearsFlexible compute usage
Spot60-90%NoneFault-tolerant, flexible
Dedicated HostVariesPer hostLicensing, compliance

Reserved Instances Types

Standard RI:
- Highest discount (up to 72%)
- Có thể bán trên Marketplace
- Không thể đổi instance family

Convertible RI:
- Lower discount (up to 66%)
- Có thể đổi instance family, OS, tenancy
- Không thể bán trên Marketplace

Savings Plans

Compute Savings Plans:
- Áp dụng cho EC2, Lambda, Fargate
- Flexible across regions, instance families
- Up to 66% discount

EC2 Instance Savings Plans:
- Specific to instance family in a Region
- Up to 72% discount
- Less flexible

Spot Instances

Spot Request Types:

1. One-time Request
   - Launched → Running → Terminated when interrupted

2. Persistent Request
   - Automatically re-requests if interrupted
   - Stays valid until canceled

Spot Fleet:
- Collection of Spot + On-Demand instances
- Automatically maintains target capacity
- Diversified allocation strategy

3. EC2 Placement Groups

Ba loại Placement Groups

1.CLUSTER(LowLatency)SameRack,SameAZEC2EC2EC2Use:HPC,Low-latencyapps2.SPREAD(HighAvailability)Rack1Rack2Rack3EC2EC2EC2Max:7instancesperAZUse:Criticalapps3.PARTITION(LargeDistributed)Partition1Partition2Partition3EC2EC2EC2EC2EC2EC2Use:HDFS,Cassandra,Kafka

4. Auto Scaling Groups (ASG)

Components

AutoScalingGroupLaunchTemplateScalingPolicies-AMI-TargetTracking-InstanceType-StepScaling-SecurityGrp-SimpleScaling-UserData-ScheduledCapacitySettings:-Minimum:2-Desired:4-Maximum:10

Launch Template vs Launch Configuration

FeatureLaunch TemplateLaunch Config
VersioningYesNo
Multiple instance typesYesNo
Spot + On-Demand mixYesNo
T2/T3 UnlimitedYesNo
RecommendedYesLegacy

5. Scaling Policies

Target Tracking (Khuyến nghị)

Policy: Keep Average CPU at 50%

CPU = 70% → Scale OUT (add instances)
CPU = 30% → Scale IN (remove instances)
CPU = 50% → No action

Step Scaling

CPU Usage    Action
0-40%        Remove 2 instances
40-60%       No change
60-80%       Add 1 instance
80-100%      Add 3 instances

Scheduled Scaling

{
  "ScheduledActionName": "scale-out-morning",
  "Recurrence": "0 9 * * MON-FRI",
  "MinSize": 5,
  "MaxSize": 20,
  "DesiredCapacity": 10
}

Predictive Scaling

  • Sử dụng ML để dự đoán traffic patterns
  • Proactive scaling trước khi load tăng
  • Kết hợp với dynamic scaling

6. Scaling Cooldowns

ScaleOutEventCooldownAdd2Inst(300sec)ReadyfornextscalePurpose:-Tránhscalequánhanh-Chmetricsnđnh-Default:300seconds

7. Health Checks

Types

TypeDefaultChecks
EC2YesInstance status
ELBOptionalHTTP health check
CustomOptionalExternal health system

Grace Period

  • Thời gian chờ sau khi instance launch
  • Không terminate instance mới trong grace period
  • Default: 300 seconds

8. Câu hỏi ôn tập

  1. Sự khác biệt giữa Standard và Convertible Reserved Instances?

    Xem đáp án

    Standard RI: discount cao hơn (~72%), không đổi instance family/OS/tenancy trong thời hạn, có thể bán trên RI Marketplace. Convertible RI: discount thấp hơn (~54%), có thể exchange sang RI khác cùng hoặc cao hơn giá trị (đổi family, OS, tenancy, Region), không bán được. Chọn Standard khi workload ổn định rõ ràng; Convertible khi cần flexibility vì technology roadmap chưa chắc.

  2. Khi nào nên dùng Spot Instances?

    Xem đáp án

    Spot phù hợp cho fault-tolerant, stateless workloads: batch processing, data analytics (Spark/Hadoop), ML training, CI/CD workers, image/video rendering, genomics. Key requirements: (1) App có thể checkpoint và resume, (2) Không cần guaranteed uptime, (3) Có thể handle 2-phút interruption warning. Không dùng cho: production databases, stateful web servers (không có fallback), real-time customer-facing APIs.

  3. Ba loại Placement Groups là gì và use cases của chúng?

    Xem đáp án

    (1) Cluster: cùng rack trong 1 AZ, low-latency 10+ Gbps network — cho HPC, ML training, big data (Hadoop). (2) Spread: mỗi instance trên hardware riêng biệt, cross-AZ, max 7 instances/AZ — cho critical apps cần max HA (tránh correlated hardware failures). (3) Partition: groups of instances, mỗi partition cách nhau về hardware — cho distributed databases (Cassandra, HDFS, HBase) cần topology-aware replication.

  4. Tại sao nên dùng Launch Template thay vì Launch Configuration?

    Xem đáp án

    Launch Template hỗ trợ: versioning (rollback về version cũ), multiple instance types (Mixed Instances Policy), Spot + On-Demand trong cùng ASG, T2/T3 unlimited, tất cả EC2 features mới. Launch Configuration là legacy (deprecated 2022) — không có versioning, chỉ 1 instance type, không tương thích nhiều features mới. AWS không cho phép tạo Launch Configuration mới.

  5. Target Tracking Scaling policy hoạt động như thế nào?

    Xem đáp án

    Target Tracking maintain một metric tại target value bạn set — ASG tự động scale out/in để giữ metric gần target. Ví dụ: target CPU = 50% → khi CPU > 50%, ASG thêm instances; khi CPU < 50%, ASG giảm instances (sau cooldown). Đơn giản nhất trong 4 scaling policies — không cần define thresholds phức tạp. AWS recommend dùng CPU, ALB Request Count per Target, hoặc custom metric từ CloudWatch.

9. Bài tập thực hành

  • Tạo Launch Template với User Data
  • Tạo Auto Scaling Group với Target Tracking policy
  • Test scaling bằng cách tạo CPU load
  • Xem scaling activities trong console

Tài liệu tham khảo chính thức


Ngày tiếp theo: S3 và Storage Review