8 min readRishi

Kubernetes CPU Limits: The Process That Was Throttled, Not Slow

Kubernetes CPU Limits: The Process That Was Throttled, Not Slow

The checkout API sat at 40% CPU. Grafana was green. p99 jumped from 80ms to 900ms after the "tighten resources" PR. Nobody rolled back the code. The PR had set limits.cpu: 250m equal to the request, because that is how you get Guaranteed QoS, and Guaranteed sounded like the process would get what it asked for. A CPU limit is not a reservation. It is a CFS quota: 25 milliseconds of run time per 100-millisecond period, even when the node is idle. The scheduler packed the pod using the request. The kernel paused the process using the limit.

The probe post is which health check restarts you. The SIGTERM post is what happens when the kubelet already decided to stop you. This is the pause while you are still Ready and still serving the request that just stretched across two quota periods.

Requests place. Limits pause.

requests.cpu is what kube-scheduler uses to bin-pack, and what the cgroup weight uses when the node is actually contended (cpu.shares on v1, cpu.weight on v2). A 250m request among 4000m of requests on a 4-core node is entitled to about a sixteenth of the CPU when every pod wants to run. When neighbours are idle, a pod with a request and no limit can use a spare core.

limits.cpu writes a quota. Kubernetes' CFS period is 100ms. The quota in microseconds is millicores × 100. So:

limits.cpu: 250m  →  cpu.cfs_quota_us = 25000   (25ms / 100ms)
limits.cpu: "1"   →  cpu.cfs_quota_us = 100000  (100ms / 100ms)
limits.cpu: "2"   →  cpu.cfs_quota_us = 200000  (two cores, no pause at full run)

On cgroup v2 the same numbers land in cpu.max as 25000 100000. After 25ms of on-CPU time in a period, the kernel sleeps every thread in that cgroup until the period rolls. Spare cores on the node do not matter. The quota is local to the container.

resources:
  requests:
    cpu: 250m
    memory: 256Mi
  limits:
    cpu: 250m      # this is the quota, not a "max if needed"
    memory: 256Mi  # this one you still want; see below

Equal request and limit is Guaranteed QoS. Guaranteed changes eviction order under memory pressure. It does not disable CFS. The tutorial that said "set them equal for production" mixed those two facts.

A 50ms handler on a 250m limit

One POST needs 50ms of CPU: JSON, a HMAC, a serializer that does not yield. On a machine with a free core that is 50ms of wall time. Behind a 250m quota it cannot be:

t=0      thread scheduled, burns the 25ms quota
t=25ms   CFS quota exhausted → sleep until t=100ms
t=100ms  new period, remaining 25ms of work runs
t=125ms  handler returns

50ms of CPU became 125ms of wall clock, and that was one thread with no other work in the period. Two concurrent POSTs share the same 25ms. A Go or .NET process that wakes sixteen worker threads shares it sixteen ways. The CPU graph still reports ~250 millicores — you used the whole quota, which looks right-sized until you plot latency next to it.

kubectl top pod will not save you. Metrics Server reports usage, not stalls. The cgroup counters will. On v1 that is cpu.stat (nr_throttled, throttled_time); on v2, throttled_usec:

nr_periods 40321
nr_throttled 18804
throttled_usec 1880400000

From Prometheus, if cAdvisor is scraping:

sum by (pod) (
  rate(container_cpu_cfs_throttled_periods_total{container!=""}[5m])
)
/
sum by (pod) (
  rate(container_cpu_cfs_periods_total{container!=""}[5m])
)

A ratio that is not near zero is the incident. Utilization at 40% of limit can sit next to a 40% throttle ratio: the process is idle most of the period because it already spent the quota in the first 25ms. Averaging over a minute hides a spike that filled a slot.

The runtime still sees the node

Unless you turned on the CPU Manager static policy and asked for an integer number of CPUs on a Guaranteed pod, /proc/cpuinfo still lists every core on the node. The quota does not change the topology. Thread pools that size themselves from the host CPU count start more workers than 25ms of quota can feed:

# 16 logical CPUs on the node, 250m limit, GOMAXPROCS unset
16 runnable workers × one 25ms slot = 1.5ms each, then everyone sleeps

Pin the count to the millicores you actually gave the container, or to 1 when the limit is under a full core. uber-go/automaxprocs reads the cgroup. .NET honors DOTNET_PROCESSOR_COUNT. The JVM's container support does the same from the quota. None of that removes throttling. It only stops you from multiplying it. A single thread can still burn 25ms and stall. Exclusive CPUs from CPU Manager avoid the shared CFS pool; they require whole cores and Guaranteed QoS — the right tool when you will pay a core for latency, not for a 250m API replica.

HPA divides by the request

The Horizontal Pod Autoscaler default CPU metric is usage divided by request, not by limit. A dashboard that plots usage/limit will not match.

# request 250m, limit 1000m, target 80%
# HPA wants more replicas at 200m of usage
# the container still has 800m of quota headroom
metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 80

Two ways this goes wrong. Request much smaller than limit: HPA scales out while the process is nowhere near the quota. Request equal to limit, which was the Guaranteed PR: a brief 50ms burst that throttles also looks like a replica at capacity, so you scale out into more throttled pods. If you keep a CPU limit, keep request and limit in shouting distance, or give HPA a custom metric — p99, queue depth — that is not CFS in disguise.

What to set

Always set a CPU request. It is the packing number and the fair-share weight. Size it from actual usage at the latency you care about, not from a template. A request of 250m on a handler that regularly needs 50ms bursts is a request you will feel.

Treat a CPU limit as optional, and expensive. Omitting it lets the process use idle cores. Under contention it still falls back to shares from the request, so a runaway tight-loop cannot take all of a busy node — it can take unused capacity, which is the point of unused capacity. A neighbour's latency when this process goes feral on a quiet node is real. It is a cluster-level problem (noisy neighbour, a separate node pool for batch) more often than a reason to quota every API replica to 250m.

Keep the memory limit. Memory is not CFS. A process that allocates past limits.memory is OOMKilled. A process with no memory limit can exhaust the node and get everyone evicted. CPU throttle is a pause. Memory overcommit is a kill, and not always of the pod that leaked. Copying "request = limit" from memory onto CPU is how the checkout PR happened.

A working spec for a latency-sensitive service that is allowed to burst:

resources:
  requests:
    cpu: 500m
    memory: 256Mi
  limits:
    memory: 256Mi
    # cpu omitted on purpose

If you cannot omit the limit — a platform policy, a multi-tenant node, a batch job that has burned a node before — set it several times the request, not equal to it. request: 250m / limit: 2 is a quota a 50ms handler can finish in one period. It is no longer Guaranteed. That is the trade: eviction priority versus a CFS slot your p99 can fit in.

What this is not

A missing request. A pod with no CPU request is BestEffort (if memory is also unset) or Burstable that the scheduler treats as using almost nothing. It gets packed onto a full node and then starved by shares. The fix is a request, not a tighter limit.

A liveness probe on a throttled event loop. If /live is served by the same threads that just slept 75ms, a short periodSeconds plus failureThreshold: 1 turns CFS into a restart. The probe is not wrong that the process did not answer. The quota is why.

Node CPU of 100%. That is contention, and shares are doing what they should. Check nr_throttled and node saturation. Throttle on an idle node is the limit. Throttle on a saturated node is the neighbourhood. Only one of those is fixed by deleting limits.cpu.

If p99 moved and the CPU graph did not, read cpu.stat before you add replicas. The Deployment is Ready. The period ended, and the kernel had nothing left to give until the next one.

Keep reading

Newsletter

New posts, straight to your inbox

One email when a new post goes up. Double opt-in, unsubscribe anytime. Not a daily digest.

Comments

  • No comments yet. Be the first.