🔧 DevOps

Scaling

Scaling

Adjusting system capacity. Vertical (bigger servers), horizontal (more servers).

Detailed Explanation

Scaling is adjusting a system's processing capacity to respond to changing load. There are two main approaches: Vertical Scaling (Scale-Up) and Horizontal Scaling (Scale-Out).

Vertical scaling increases the CPU, memory, and storage of a single server; it is simple but has physical limits. Horizontal scaling increases the number of server instances; in theory it allows unlimited expansion, but the application must be designed to suit a distributed environment. In cloud environments, Auto Scaling automatically adjusts instances according to traffic, achieving cost efficiency and performance at the same time.

Code Example

Kubernetes HPA (Horizontal Pod Autoscaler)

# hpa.yaml - CPU/메모리 기반 자동 스케일링
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-server-hpa
  namespace: production
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-server
  minReplicas: 3
  maxReplicas: 20
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80
  - 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
      - type: Pods
        value: 4
        periodSeconds: 15
      selectPolicy: Max

AWS Auto Scaling Configuration (Terraform)

# autoscaling.tf
resource "aws_autoscaling_group" "web" {
  name                = "web-asg"
  vpc_zone_identifier = var.private_subnets
  target_group_arns   = [aws_lb_target_group.web.arn]

  min_size         = 2
  max_size         = 10
  desired_capacity = 3

  health_check_type         = "ELB"
  health_check_grace_period = 300

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

  tag {
    key                 = "Name"
    value               = "web-server"
    propagate_at_launch = true
  }
}

resource "aws_autoscaling_policy" "scale_out" {
  name                   = "scale-out"
  autoscaling_group_name = aws_autoscaling_group.web.name
  adjustment_type        = "ChangeInCapacity"
  scaling_adjustment     = 2
  cooldown               = 300
}

resource "aws_cloudwatch_metric_alarm" "high_cpu" {
  alarm_name          = "high-cpu-utilization"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = 2
  metric_name         = "CPUUtilization"
  namespace           = "AWS/EC2"
  period              = 120
  statistic           = "Average"
  threshold           = 70
  alarm_actions       = [aws_autoscaling_policy.scale_out.arn]

  dimensions = {
    AutoScalingGroupName = aws_autoscaling_group.web.name
  }
}

Practical Conversation

CTO

"Can we handle a 10x increase in traffic for next month's Black Friday promotion?"

Infrastructure engineer

"Yes, we can raise maxReplicas to 50 in the current HPA configuration and also adjust the max of the Auto Scaling group for the node pool. We'll verify it in advance with load testing."

Finance Team

"Costs won't run high during normal times, will they?"

Infrastructure engineer

"Of course not. We keep minReplicas at 3, and when traffic drops, it automatically scales down. We set a 5-minute stabilization window in the scaleDown policy to prevent unnecessary scale-downs."

Dev team lead

"Doesn't the database also need to be prepared for scaling?"

Infrastructure engineer

"For the DB, we'll add 2 more Read Replicas in advance and strengthen connection pooling. Writes will be handled with vertical scaling, and reads will be distributed horizontally."

Cautions

  • Stateless design: For horizontal scaling, the application must not store session state on the server. Use Redis or an external session store.
  • Cold Start problem: Set the scale-up policy with some leeway, taking into account the time it takes for new instances to become ready. JVM-based apps in particular need warm-up.
  • Cost management: Setting maxReplicas to unlimited can cause costs to explode during an unexpected traffic surge.
  • Database bottleneck: Even if you scale only the app servers, you may hit the DB connection limit. Plan connection pooling and DB scaling together.
  • Scale-down caution: Scaling down too abruptly can put load on the remaining instances. Set the stabilization window appropriately.

Learn More