Sub topic 4
CPU limits: throttling, not killing
This chapter assumes you can write a pod manifest, apply it with kubectl, and read CPU quantities such as 250m (a quarter of a core). It covers what happens when a container tries to use more CPU than its limit allows.
CPU is compressible
CPU is a compressible resource. If a container wants more than it is allowed, the kernel makes it wait. It does not kill the container. Memory behaves differently, and the next chapter covers it.
The limit in resources.limits.cpu is enforced by the Linux kernel through cgroups, using the Completely Fair Scheduler (CFS) bandwidth controller. The request in resources.requests.cpu plays a different role. It sets the container's relative weight (cpu.shares on cgroup v1, cpu.weight on cgroup v2) when the node is busy.
How the CFS quota works
The kernel divides time into periods, 100 ms by default. A limit of 500m becomes a quota of 50 ms of CPU time per 100 ms period. The quota is shared by all threads of the container.
- The container runs and consumes quota.
- When the quota for the current period is used up, the kernel pauses every thread of the container.
- The threads resume at the start of the next period.
A multi-threaded container can burn its quota quickly. With a 500m limit and four busy threads, the quota is gone after about 12.5 ms of wall-clock time. The container then waits for roughly 87.5 ms. The average CPU use is still 0.5 cores, but request latency spikes.
Example: a limit in practice
The first pod from the topic sets a request of 250m and a limit of 500m:
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: app
image: nginx:1.27
resources:
requests:
cpu: 250m
memory: 128Mi
limits:
cpu: 500m
memory: 256MiIf this container is CPU-bound, it never exceeds 0.5 cores of use. It is not restarted, and kubectl get pod web shows no change in status. The only symptom is that work takes longer.
Seeing throttling
Throttling is invisible in pod status, so you read it from the cgroup counters or from metrics. Inside the container on cgroup v2, the cpu.stat file reports it:
kubectl exec web -- cat /sys/fs/cgroup/cpu.statLook at these fields:
| Field | Meaning |
|---|---|
| `nr_periods` | Number of enforcement periods that have elapsed |
| `nr_throttled` | Periods in which the container hit its quota |
| `throttled_usec` | Total time the container spent paused, in microseconds |
If nr_throttled is a large share of nr_periods, the limit is slowing the workload. With Prometheus and cAdvisor metrics, the ratio of container_cpu_cfs_throttled_periods_total to container_cpu_cfs_periods_total gives the same signal.
A container can be throttled while kubectl top shows usage below the limit. Top reports an average over a window, and throttling happens inside 100 ms periods.
Requests versus limits under contention
When the node has spare CPU and a container has no limit, it can use all the idle capacity. When the node is saturated, the kernel shares CPU in proportion to requests. A container requesting 500m gets twice the share of one requesting 250m. This is why a container with a request but no limit is still protected from its neighbors.
Common mistakes
- Setting the limit very close to the request. Short bursts at startup or during garbage collection hit the quota and add latency.
- Setting a limit below what the application's threads need. A runtime that starts many threads sized to the node's core count will throttle heavily under a small limit.
- Reading throttling as an application bug. Check
cpu.statbefore you profile the code. - Omitting the request. Without it, the scheduler assumes the pod needs no CPU, and the container gets a minimal share under contention.
Exercise
A container has a CPU limit of 200m and runs four busy threads. How much CPU time does it get per 100 ms period, and what happens when it uses that time up? Does Kubernetes restart the container?
Show answer
A 200m limit gives a quota of 20 ms of CPU time per 100 ms period. With four busy threads the quota is used in about 5 ms of wall-clock time. The kernel then pauses all the container's threads until the next period begins. Kubernetes does not restart the container. It is throttled, not killed, and the only symptom is higher latency.