Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 408 of 8026. Kubernetes Traffic and ScalingFree lesson

Scaling Applications Based on Memory Usage

Configure HPA policies to trigger pod replication scaling events when memory consumption exceeds specific thresholds to prevent out-of-memory application crashes.

In this lesson, you will learn how to configure the Horizontal Pod Autoscaler (HPA) to automatically scale your application instances based on memory utilization, ensuring your services stay stable before hitting memory limits.

Why Scale by Memory?

While scaling based on CPU is common, many applications—especially those processing large datasets or handling complex JSON payloads—are "memory-bound." If your application exceeds its allocated limits.memory, Kubernetes will trigger an Out-of-Memory (OOM) kill, causing the pod to crash and restart. By configuring the HPA to monitor memory usage, we can trigger additional pod replicas before the application reaches its breaking point.

Configuring Memory-Based HPA

To scale based on memory, you must have the metrics-server installed and define resources.requests in your deployment. The HPA uses these requests to calculate the percentage of memory currently being utilized.

Consider the following HorizontalPodAutoscaler manifest:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: memory-autoscaler
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 70

In this configuration, the HPA constantly monitors the average memory usage across your pods. If the pods consume more than 70% of the memory defined in your container's resources.requests, the HPA will automatically provision new replicas to distribute the load and bring the average back down.

Key Considerations

  1. Requests are Required: Kubernetes cannot calculate "utilization" if you do not set resources.requests.memory. Without a request, the HPA cannot establish a baseline, and scaling will fail.
  2. Smoothing Spikes: Memory usage can sometimes spike briefly (e.g., during garbage collection). Set your averageUtilization threshold conservatively—usually between 60% and 80%—to avoid "flapping," where the HPA constantly scales pods up and down in rapid succession.

Key Takeaway: Scaling based on memory utilization allows you to proactively distribute application load, preventing OOM crashes by scaling horizontally before your memory usage hits critical limits.