Video in progress

What is the difference between resources.requests and resources.limits in Kubernetes?

requests is the guaranteed amount for the scheduler: a pod is placed only on a node that has that much free. limits is the upper bound: if CPU goes over the limit the pod is throttled, and if memory goes over the limit the container is killed with OOMKilled.

📖 Answer

resources:
  requests:
    cpu: "500m"       # half a core guaranteed
    memory: "768Mi"
  limits:
    memory: "768Mi"   # equal to requests, no surprise spikes

CPU and memory behave differently when they go over the limit. CPU is a compressible resource: the process slows down but stays alive. Memory is not compressible: go one byte over the limit and the kernel kills the process (exit code 137).

Practical tips for Java:

  • for memory, set requests = limits and give the heap as a percentage of that limit (MaxRAMPercentage);
  • do not set the CPU limit too low. Spring Boot eats several cores at startup, and with a 500m limit, startup can take a minute instead of 15 seconds;
  • some teams set no CPU limit at all and work with requests only. That depends on the cluster policy.

If you write requests far below actual usage, too many pods get packed onto a node and they all squeeze each other. Take the numbers from real usage in Prometheus/Grafana, not from guesses.