Sub topic 11
Choosing request and limit values
This chapter assumes you can write pod manifests, know the CPU and memory units (250m, 128Mi), and have read the earlier chapters on scheduling, throttling, OOM kills and QoS classes. It covers how to pick numbers instead of guessing them.
Start from measurement
A request is a scheduling reservation and a limit is a runtime ceiling. Picking either one without data tends to fail in one of two ways:
- Values set too high waste capacity. The scheduler reserves resources that the container never uses, so nodes look full while their real usage is low.
- Values set too low cause throttling, OOM kills and evictions.
Run the workload under realistic load first, then read its usage with kubectl top. Use a metrics system that keeps history if you have one, because a single snapshot misses peaks.
kubectl top pod web --containersA sizing procedure
The steps below work for most stateless services.
- Deploy with generous temporary values and apply realistic load, including startup and the busiest period you expect.
- Record the typical usage and the peak for CPU and for memory.
- Set the CPU request near typical usage, for example the 50th to 90th percentile. The request is what the pod is guaranteed under contention, so it should cover normal work.
- Set the memory request near the peak you observe. Memory cannot be taken back without killing the process, so under-requesting it is risky.
- Set the memory limit a little above the peak. Add headroom of about 20 to 30 percent for spikes.
- Decide whether to set a CPU limit at all (see below).
- Watch the workload for a few days and adjust.
Memory: request and limit usually match
Memory is not compressible. A container that goes over its limit is killed (exit code 137). Because of that, many teams set the memory request equal to the memory limit. The pod then reserves exactly what it may use, and the node cannot be overcommitted on memory by this container.
CPU: be careful with limits
CPU is compressible. A container above its limit is throttled, not killed. Throttling can add latency even when the node has idle CPU, because the limit is enforced in short scheduling periods. For latency-sensitive services, many teams set a CPU request and leave the CPU limit unset. The container can then use spare CPU on the node, and the request still guarantees its share when the node is busy.
Leaving out CPU limits has costs:
- The pod cannot be Guaranteed QoS, because that class requires requests to equal limits for both resources.
- A noisy container can use all spare CPU, although the request-based share still protects its neighbors.
- A LimitRange or ResourceQuota in the namespace may require a limit anyway.
Example: Burstable service with a memory limit
This pod requests CPU for normal load, caps memory at a measured peak plus headroom, and sets no CPU limit.
apiVersion: v1
kind: Pod
metadata:
name: api
spec:
containers:
- name: app
image: nginx:1.27
resources:
requests:
cpu: 300m
memory: 256Mi
limits:
memory: 256MiIts QoS class is Burstable. If it needs a stronger eviction guarantee, set both CPU values equal as well.
Decision table
| Workload | CPU request | CPU limit | Memory request and limit |
|---|---|---|---|
| Latency-sensitive API | Typical usage | Often none | Equal, above peak |
| Batch job | Average usage | Set, to protect neighbors | Equal, above peak |
| Critical database | Peak usage | Equal to request | Equal, above peak |
| Dev or test | Small | Small | Small, with a limit |
Common mistakes
- Copying values from another service. Usage depends on the application and its load.
- Sizing from idle usage. Measure startup and peak periods too.
- Setting a memory limit at the observed average. Any spike then causes an OOM kill.
- Never revisiting values. Traffic and code change, so review them regularly.
Exercise
A service uses about 200m CPU normally, peaks at 600m, and reaches 300Mi memory at peak. It is latency-sensitive. Propose requests and limits, and name its QoS class.
Show answer
For example, CPU request 200m with no CPU limit, memory request and limit 384Mi (the 300Mi peak plus roughly 25 percent headroom). Because the CPU limit is unset and requests do not equal limits for CPU, the pod is Burstable. If you must have Guaranteed, set the CPU request and limit equal, for example 600m, and accept more reserved capacity.