☁️ Cloud

Auto Scaling

Auto Scaling

A feature that automatically adjusts the number of servers according to load. Cost optimization and availability assurance.

📖 Detailed Explanation

Auto Scaling is a feature that automatically increases or decreases computing resources according to application load. When traffic surges, it adds instances to maintain availability, and when traffic drops, it removes instances to reduce costs.

Scale Out is horizontal scaling that increases the number of instances, and Scale Up is vertical scaling that increases the specifications of an instance. In the cloud, horizontal scaling is mainly used, and for this the application must be stateless.

Scaling policies include Target Tracking (maintaining CPU at 70%), Step Scaling (stepwise scaling by threshold), Scheduled Scaling (scheduled scaling), and Predictive Scaling (ML-based prediction). In most cases, Target Tracking is the most effective.

In Kubernetes, HPA (Horizontal Pod Autoscaler) automatically adjusts the number of Pods, VPA (Vertical Pod Autoscaler) adjusts resource requests, and Cluster Autoscaler adjusts the number of nodes. With KEDA, scaling is possible based on various metrics such as queue length and HTTP requests.

💻 Code Example

# Kubernetes HPA (Horizontal Pod Autoscaler)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-app-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-app
  minReplicas: 2
  maxReplicas: 20
  metrics:
    # CPU 기반 스케일링
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    # 커스텀 메트릭 (Prometheus)
    - type: Pods
      pods:
        metric:
          name: http_requests_per_second
        target:
          type: AverageValue
          averageValue: 1000
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300  # 5분 안정화 기간
      policies:
        - type: Percent
          value: 10
          periodSeconds: 60
    scaleUp:
      stabilizationWindowSeconds: 0
      policies:
        - type: Percent
          value: 100
          periodSeconds: 15

---
# AWS Auto Scaling Group (Terraform)
resource "aws_autoscaling_group" "app" {
  name                = "app-asg"
  vpc_zone_identifier = aws_subnet.private[*].id
  target_group_arns   = [aws_lb_target_group.app.arn]

  min_size         = 2
  max_size         = 20
  desired_capacity = 3

  launch_template {
    id      = aws_launch_template.app.id
    version = "$Latest"
  }

  health_check_type         = "ELB"
  health_check_grace_period = 300

  # 인스턴스 워밍업 시간
  default_instance_warmup = 120

  tag {
    key                 = "Name"
    value               = "app-instance"
    propagate_at_launch = true
  }
}

# Target Tracking 스케일링 정책
resource "aws_autoscaling_policy" "cpu" {
  name                   = "cpu-target-tracking"
  autoscaling_group_name = aws_autoscaling_group.app.name
  policy_type            = "TargetTrackingScaling"

  target_tracking_configuration {
    predefined_metric_specification {
      predefined_metric_type = "ASGAverageCPUUtilization"
    }
    target_value = 70.0
  }
}

🗣️ How It's Said in Practice

DevOps: "To prepare for Black Friday, I'll raise the min capacity from 5 to 10. We also need to take the warm-up time into account."

Developer: "We have Auto Scaling, so why do we need to raise it in advance?"

DevOps: "Scaling out takes 2-3 minutes. If traffic suddenly jumps 10x, errors can occur in the meantime."

Interviewer: "What should the application take into account in order to apply Auto Scaling?"

Candidate: "The application must be stateless. Sessions are stored in an external store such as Redis, and files are stored in S3. It should use graceful shutdown so that in-progress requests are completed before termination, and it must also implement a health check endpoint."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

Reviewer: "HPA's minReplicas is 1, but at least 2 are needed for HA. If one node fails, the service could go down."

Author: "You're right. I'll set minReplicas to 2 and also add a PodDisruptionBudget."

⚠️ Cautions

📚 Learn More