Skip to content

Setting reasonable CPU and memory limits on PaaS containers

Default container limits are either too generous or too tight, and both fail in production. Here is how we pick request and limit values that match the workload.

You deployed to managed PaaS, accepted the default CPU and memory limits, and now one of two things is happening. Either the autoscaler is adding nodes for what looks like noise, or your application is throttled at the CPU and killed at the memory ceiling under loads the old VM handled. Same root cause.

What’s happening

A PaaS container has two knobs per resource: the request (what the scheduler reserves and what the autoscaler measures against) and the limit (the hard ceiling at which you get throttled on CPU or OOM-killed on memory). Defaults are usually round numbers — 500m CPU / 512Mi memory — that bear no relationship to your application’s actual profile.

Two failure modes:

  • Request equal to limit. CPU utilization sits at 90% of request, the autoscaler scales at 70%, and you end up with twice the replicas you need. Wasted spend.
  • Limit far above request. The container looks idle to the scheduler, gets scheduled densely with neighbors, and during a real spike contends for CPU with other tenants. Latency degrades and the autoscaler does not respond.

Java and Node have a third trap: the runtime sees the host’s full CPU count, sizes its thread pool against it, and ignores the cgroup limit. The container is throttled long before the runtime thinks it is busy.

How to verify

Inside the container:

cat /sys/fs/cgroup/cpu.max          # CPU quota and period (cgroup v2)
cat /sys/fs/cgroup/memory.max       # memory limit
cat /sys/fs/cgroup/cpu.stat         # throttled_time grows when you're throttled

If throttled_time is increasing and latency is degrading, you are CPU-throttled. From the PaaS panel, plot container_memory_working_set_bytes against the limit for the last 24 hours. If working set sits above 80% of limit, you are one spike away from OOM.

For JVM: confirm -XX:+UseContainerSupport (default since JDK 10) and an explicit -Xmx below the limit. For Node: --max-old-space-size= to about 75% of limit.

The fix

  1. Measure first. Run the application under realistic load for an hour, observe p95 CPU and peak memory working set. Request is p95 + 20% headroom; limit is peak + 50% headroom.
  2. Set request and limit. Same value for memory (you cannot recover from OOM, so don’t oversubscribe); 2–3× request for CPU (CPU is throttleable, so headroom is safe).
  3. Tell the runtime about the cgroup. Java: -XX:MaxRAMPercentage=75. Node: --max-old-space-size. Python: nothing built-in, so just measure.
  4. Re-tune the autoscaler against the new request. If CPU request is 70% of peak instead of 100%, your scale-up threshold should follow.

The right limit is the one that matches the workload, not the template.

Stack Harbor’s Cloud PaaS service profiles container limits during onboarding — we can size yours.