Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Explain how a Horizontal Pod Autoscaler scales a Spring Boot service on CPU and why JVM warmup can cause problems.

A Horizontal Pod Autoscaler (HPA) regularly compares the observed average CPU utilisation of a Deployment's pods with a target, such as 70 percent of their CPU request, and changes the replica count in proportion to the difference. With Spring Boot the trouble is warmup: a new JVM pod burns extra CPU on class loading and JIT compilation and answers slowly at first, so the average CPU can rise and trigger even more scaling. Startup and readiness probes keep cold pods out of traffic, and behaviour settings such as stabilization windows stop the replica count from flapping.

Why this question is asked

Autoscaling looks like a switch we turn on, but with Java services it often behaves strangely: a spike causes far more pods than needed, latency gets worse right after scale-out, or the replica count goes up and down every few minutes. Interviewers ask this question to see whether we understand both halves: the simple control loop the HPA runs, and how a JVM's startup behaviour feeds back into that loop. In this answer, we will recap how the HPA computes replicas, follow a scale-out step by step, see why JVM warmup disturbs it, and then use probes and behaviour settings to make it stable.

1A Quick Recap of How the HPA Decides

The HPA is a controller inside Kubernetes. Every 15 seconds by default it reads metrics for the pods of a target, usually a Deployment, from the metrics server. For CPU, utilisation is measured as a percentage of each pod's CPU request, not of the node. A pod requesting 1 CPU and using 0.7 CPU is at 70 percent.

The HPA then applies one formula: desired replicas equal current replicas times current utilisation divided by target utilisation, rounded up. It ignores small differences, within a tolerance of 10 percent by default, so it does not react to noise. Finally it clamps the result between minReplicas and maxReplicas.

YAML
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: { name: orders-api }
spec:
scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: orders-api }
minReplicas: 3
maxReplicas: 20
metrics:
- type: Resource
resource: { name: cpu, target: { type: Utilization, averageUtilization: 70 } }
# 4 pods at 105% average: ceil(4 * 105 / 70) = 6 replicas
# 6 pods at 60% average: ratio 0.86, outside tolerance, ceil(6 * 60 / 70) = 6 -> stays 6

Notice that the target is relative to the request. If the CPU request is badly set, the HPA's idea of busy is badly set too.

2Why a New JVM Pod Is Not a Normal Pod

A freshly started Spring Boot pod does a lot of work before it serves its first request efficiently. The JVM loads and verifies thousands of classes, Spring scans and creates beans, and the application starts in interpreted mode. As methods become hot, the JIT compiler, a part of the JVM that turns frequently run bytecode into optimised machine code, compiles them on background threads. During the first minute or two, the same request can take several times longer and cost several times more CPU than on a warm pod.

So a new pod is both expensive and slow. That would be fine on its own, but the HPA is watching CPU.

3How Warmup Feeds Back Into Scaling

Follow a spike. Traffic rises, the average reaches 105 percent, and the HPA scales from 4 to 6 pods. The two new pods start, use near their full CPU for class loading and JIT, and, once ready, receive a share of the traffic they handle slowly. The average CPU across 6 pods stays high or even rises, so on the next round the HPA scales again to 9 pods, which again start cold. The service ends up with more replicas than the traffic needs, and only after warmup does the CPU fall, after which the HPA later scales down. Meanwhile, users routed to cold pods see slow responses.

Step throughA scale-out with cold JVM pods
  1. Traffic spikeaverage CPU 105% of request
  2. HPA scales 4 -> 6new pods start
  3. New pods load classes and JIThigh CPU, slow responses
  4. Average CPU still above target?
    YesScale again, more cold podsNoHold replica count
  5. Pods warm upCPU per request falls
  6. Too many replicas nowscale down later

6 steps in order. Play them, or step through them one at a time.

Without probes and stabilization, warmup CPU can look like demand.

Kubernetes already softens part of this. During a CPU initialisation period after a pod starts (five minutes by default, a controller-manager setting), the HPA sets aside CPU samples from pods that are not ready yet, or whose sample was taken before they became ready. This is one more reason for an accurate readiness signal: it decides what the HPA counts.

4Keeping Cold Pods Out of Traffic

A startup probe tells Kubernetes when the application has finished starting; until it succeeds, the other probes are not run. A readiness probe tells Kubernetes whether the pod should receive traffic; until it passes, the pod is not added to the Service's endpoints. Spring Boot Actuator exposes /actuator/health/liveness and /actuator/health/readiness, and the readiness state only becomes ACCEPTING_TRAFFIC after the application context has started and the ApplicationReadyEvent has fired.

We can go further and warm the JVM before reporting ready, for example by running a few representative calls in an ApplicationRunner, which runs before the application is marked ready. The pod then joins traffic already partly compiled.

YAML
startupProbe:
httpGet: { path: /actuator/health/liveness, port: 8080 }
periodSeconds: 5
failureThreshold: 30 # up to 150 s to start before Kubernetes restarts it
readinessProbe:
httpGet: { path: /actuator/health/readiness, port: 8080 }
periodSeconds: 5
resources:
requests: { cpu: "1" } # the HPA percentage is measured against this

5Stabilization Windows to Stop Flapping

Flapping means the replica count rises and falls repeatedly as the metric oscillates around the target. The behavior section of autoscaling/v2 controls how fast the HPA may change. A stabilization window makes the HPA use the most cautious recommendation from the recent past: for scale-down, the highest replica count recommended during the window, so a short dip does not remove pods. By default scale-down has a 300 second window and scale-up has none. Policies limit the rate of change, for example at most 4 pods or 100 percent per minute.

YAML
behavior:
scaleUp:
stabilizationWindowSeconds: 60 # ignore a single high sample caused by warmup
policies: [ { type: Pods, value: 4, periodSeconds: 60 } ]
scaleDown:
stabilizationWindowSeconds: 600 # keep pods 10 minutes after a peak
policies: [ { type: Percent, value: 20, periodSeconds: 60 } ]

A short scale-up window filters out the warmup bump; a longer scale-down window avoids throwing away warm pods just before the next burst. We also keep minReplicas high enough that the service can absorb the start of a spike while new pods warm.

6Follow-up questions

Why is CPU utilisation measured against the request and not the limit?

Because the request is what the scheduler reserved for the pod, so it is the pod's planned capacity. This means a CPU request that is far too small makes pods look overloaded all the time, and one that is far too large hides real load. Right-sizing requests is a prerequisite for a well-behaved HPA.

Would scaling on request rate instead of CPU avoid the warmup problem?

Partly. A custom metric such as requests per second per pod, exposed through Micrometer and a Prometheus adapter, does not jump during warmup, so it avoids scaling on JIT CPU. Cold pods are still slow, so probes and warming remain necessary.

What else reduces JVM warmup?

Class data sharing or the JDK AOT cache shortens class loading, CRaC restores an already warmed JVM, and native images start without JIT warmup at all. Each has its own costs, so they are usually chosen for services where scale-out speed really matters.

7Conclusion

In this answer, we saw that the HPA multiplies the replica count by the ratio of observed to target utilisation, with CPU measured against the request. We followed a scale-out and saw how cold JVM pods, busy with class loading and JIT compilation, raise the average CPU and respond slowly, which can trigger more scaling. Startup and readiness probes keep cold pods out of traffic, warming before ready helps further, and stabilization windows and rate policies prevent flapping.

What to say in the interviewAbout 80 seconds aloud

Open with

The HPA is a control loop. Every 15 seconds it reads the average CPU utilisation of the pods, measured as a percentage of their CPU request, compares it with the target, say 70 percent, and sets replicas to current replicas times current over target, rounded up, within a 10 percent tolerance and between min and max replicas.

Then, if they want more

With Spring Boot the problem is warmup. A new JVM pod spends a lot of CPU on class loading, Spring context startup and JIT compilation, and its first requests are slow. If it takes traffic too early, the average CPU stays high or rises, the HPA scales again, and we overshoot while users see slow responses.

To handle it, I use a startup probe so slow starts are not killed, and a readiness probe on the Actuator readiness endpoint so pods only get traffic once the context is ready, possibly after a short warmup run. Readiness also matters because the HPA sets aside CPU from pods that are not ready during the initialisation period.

Then I configure behaviour: a short scale-up stabilization window to ignore warmup spikes, a longer scale-down window so we keep warm pods, and rate limits. Keeping minReplicas sensible covers the first part of a spike.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Kubernetes documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain how a Horizontal Pod Autoscaler scales a Spring Boot service on CPU and why JVM...": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/7acf725b7e/