Requests vs. Limits

Difficulty: Intermediate

Question

Explain resource requests vs resource limits in Kubernetes. What happens when a container exceeds its CPU or memory limit?

Answer

A request is the amount of CPU or memory the scheduler reserves for a container; a Pod is only placed on a node with enough unreserved capacity to cover its requests. A limit is the ceiling the container is allowed to use at runtime. Exceeding the CPU limit causes throttling (the process is slowed, not killed), while exceeding the memory limit gets the container OOM killed with exit code 137.

Requests: scheduling and fair share

  • The scheduler sums the requests of all Pods on a node and compares them against the node's allocatable capacity. Actual usage is not considered.
  • At runtime, CPU requests become relative weights. Under contention, a container with 500m gets roughly twice the CPU time of one with 250m.
  • Memory requests influence eviction: when a node runs low on memory, the kubelet evicts Pods using the most memory above their requests first.

Units: CPU in cores or millicores (250m is a quarter core). Memory in bytes, usually Mi/Gi (binary) rather than M/G (decimal).

Limits: hard ceilings

  • CPU is compressible. The Linux CFS quota caps usage per period, so a container hitting its limit gets throttled. Latency spikes are the symptom, not crashes.
  • Memory is not compressible. If the container's cgroup exceeds its limit, the kernel's OOM killer terminates a process, and kubectl describe shows Last State: Terminated, Reason: OOMKilled.

If you set a limit but no request, Kubernetes sets the request equal to the limit.

QoS classes

Requests and limits together decide the Pod's Quality of Service class, which affects eviction order:

QoS classRuleEvicted
GuaranteedEvery container has CPU and memory requests equal to limitsLast
BurstableAt least one request or limit set, but not GuaranteedMiddle
BestEffortNo requests or limits at allFirst

Practical guidance

  • Always set memory requests and limits, usually equal for stateful or latency-critical services, so memory behaviour is predictable.
  • CPU limits are debated. Many teams set CPU requests but no CPU limit, so services can use idle CPU and avoid throttling during traffic spikes such as a Flipkart sale. Others keep limits for strict multi-tenant fairness. Either is defensible if you explain the trade-off.
  • Size requests from real usage (Prometheus metrics, the Vertical Pod Autoscaler in recommendation mode) rather than guessing. Over-requesting wastes money; under-requesting packs nodes too tightly.
  • JVM and Node.js apps need heap settings aligned with the container limit, otherwise the runtime may grow past the limit and get OOM killed.
  • Use LimitRange for namespace defaults and ResourceQuota to cap a team's total requests.
  • Newer Kubernetes versions support in-place Pod resize (beta and on by default from 1.33), letting you change CPU and memory without recreating the Pod.
Requests vs limits at a glance. Requests drive placement; limits enforce the runtime ceiling.
RequestLimit
Used byScheduler and kubelet evictionKernel cgroups at runtime
CPU behaviourRelative share under contentionThrottled when exceeded
Memory behaviourReserved on the nodeOOMKilled when exceeded
If omittedDefaults to the limit if one is setNo ceiling, can use spare capacity

Code examples

Container with requests and limits

apiVersion: v1
kind: Pod
metadata:
  name: orders-api
spec:
  containers:
    - name: app
      image: myorg/orders-api:3.2.0
      resources:
        requests:
          cpu: "500m"        # half a core reserved for scheduling
          memory: "512Mi"
        limits:
          memory: "512Mi"    # equal to request: predictable memory
          # no CPU limit: can burst into idle CPU without throttling
      env:
        - name: NODE_OPTIONS
          value: "--max-old-space-size=384"  # keep heap below the limit

This Pod is Burstable. Memory is capped tightly, CPU can burst, and the Node.js heap is sized below the memory limit to avoid OOM kills.

Checking usage and OOM kills

kubectl top pod orders-api           # needs metrics-server
kubectl describe pod orders-api | grep -A3 'Last State'
#   Last State: Terminated
#     Reason:   OOMKilled
#     Exit Code: 137
kubectl get pod orders-api -o jsonpath='{.status.qosClass}'
# Burstable

Compare kubectl top against requests to right-size, and use describe to confirm whether restarts are OOM kills.

Key points

  • Requests = scheduling reservation; limits = runtime ceiling.
  • CPU over limit = throttled; memory over limit = OOMKilled (exit 137).
  • Limit set without request means request = limit.
  • QoS: Guaranteed (requests = limits), Burstable, BestEffort (none); BestEffort is evicted first.
  • Scheduler uses requests, not real usage, so bad requests cause waste or overcrowding.

Common follow-up questions

  • Why do some teams avoid setting CPU limits?
  • How does the kubelet decide which Pods to evict under memory pressure?
  • What do LimitRange and ResourceQuota do?
  • How would you right-size requests for a service?
  • How does the Horizontal Pod Autoscaler use requests?

Interview tips

  • Clearly separate the two outcomes: throttling for CPU, OOM kill for memory.
  • Bring up QoS classes unprompted; it shows depth.
  • Have an opinion on CPU limits and explain the trade-off rather than reciting one rule.

Concepts covered

Resource Requests, Resource Limits, QoS Classes, CPU Throttling, OOMKilled

More Kubernetes & Orchestration questions