CPU Throttled?
Kubernetes CPU Requests, Limits, and Throttling
Impacts on Application Performance, some Best Practices
Alexandru Lazarev
Role
Date: 2025-01�
CPU Usage vs �CPU Throttling
Agenda
High Level Overview
CPU Management in Kubernetes:
Limits, Requests, Throttling
CPU Requests vs. Limits
K8s CPU and MEM Resources packed onto Node
Key Terms and Concepts: CPU Throttling - Intro
Key Terms and Concepts: Intro
Further in this Doc:
Key Terms and Concepts: Intro
Key Terms and Concepts: Intro
Key Terms and Concepts: Intro
Key Terms and Concepts: Intro
Task Types in Relation to CPU Access
High Level Overview, again: credits
High Level Overview: CPU Requests vs K8s Scheduler
High Level Overview: CPU Requests vs Node
High Level Overview: CPU Requests vs Usage
High Level Overview: CPU �Requests vs More Usage
Both containers wants all the node CPU at the same time:
Requests distribution
Usage distribution
High Level Overview: CPU Requests vs more++ Usage
Use case 1: A is bursting but B is needing now only 100m CPU, so A can use the whole remaining spare CPU, until…
Use case 2: B is also bursting, so K8s will distribute all available CPU sparely and proportionally to their requests between them…
High Level Overview: CPU �Requests vs More++ Usage
Other use-case: “borrowing”
Requests distribution
High Level Overview: CPU Requests vs Limits
Let introduce limits for A and B:
High Level Overview: CPU Limits
High Level Overview: CPU Limits vs Throttling
2) Application has bigger CPU bursts each period, and CPU demand exceeds defined quota, so:
1) Application has only small (below quota) CPU bursts each period:
High Level Overview: CPU - Conclusions
CPU Requests!
High Level Overview: CPU - Conclusions
CPU Limits?
So…?
High Level Overview: In a picture
Deep dive into implementation details
Linux Kernel, Tasks Scheduler, cgroups
Key Terms and Concepts: CPU Schedulers
Fig 1. Tasks in a cgroup at different hierarchy levels.
src: https://blogs.oracle.com/linux/post/cfs-group-scheduling
Key Terms and Concepts: CFS
K8s enforces CPU Requests and Limits via Linux CFS and cgroups.
CFS or “Completely Fair Scheduler” is the CPU Tasks Scheduler, currently used by Linux Kernel (* from v.2.6.23 (October 2007) but may change in Kernel v >= 6.6x).
CFS features:
Key Terms and Concepts: cgroup
“cgroup” or Control Group:
Key Terms and Concepts: Cont’d
Task:
CFS sched_entity:
Key Terms and Concepts: Cont’d
Key Terms and Concepts: CFS params
These parameters are crucial in the Completely Fair Scheduler (CFS) for balancing fairness and performance in task scheduling.
Linux CFS CPU BWC (Bandwidth Control)
Key Terms and Concepts: CPU Throttling - Recap
Linux CFS and cgroups v2
Linux CFS and cgroups v2. Details, Cont’d
CFS CPU BWC Quota Period: Advanced Note
Linux CFS and cgroups v2. Details, Cont’d
cgroup v2 tips & trick:
cat /proc/943567/cgroup 0::/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podc8015755_6b4f_4051_a184_f4ad9912b8bb.slice/cri-containerd-39e75eaa5b7c0457a58e912831b856254301bd92be88214a11a543f1865323ad.scope |
CFS and cgroups v2. Details, Cont’d, Examples
Cgroup v2 “cpu.max” examples (applicable to containers too):
CFS and cgroups v2. Details, Cont’d, Examples
Examples on a K8s setup, where I’ve observed that 1 CPU ~= 40 weight units:
CFS, cgroups v2, throttling. Details, statistics.
# cpu limit = 14 of 16 available $ cat cpu.stat usage_usec 648812623954 user_usec 590367341252 system_usec 58445282702 nr_periods 16356166 nr_throttled 2032 throttled_usec 2869459826 nr_bursts 0 burst_usec 0 |
CFS, cgroups v2, throttling. Alternative formula.
# cpu.stat usage_usec 648812623954 nr_periods 16356166 nr_throttled 2032 throttled_usec 2869459826 |
CFS, cgroups v2, throttling. Some heuristics:
# cpu.stat usage_usec 648812623954 nr_periods 16356166 nr_throttled 2032 throttled_usec 2869459826 |
Throttling Explained
Agenda
Throttling Explained: Examples
For simplicity, following examples consider that there are missing … or make abstraction of:
Throttling Explained. Example#1: Single-Task
Throttling Explained. Example#1�Conceptual model: No CPU Limit�
Throttling Explained. Example#1
Total periods 5
Throttled period 4
Throttling Explained. Example#1. Calculus
Calculus page format (as on next page, incl. for other examples):
Throttling Explained. Same Example#1. Calculus
Time | run (ms) | throttled (ms) |
100 | 40 | 60 |
200 | 40 | 60 |
300 | 40 | 60 |
400 | 40 | 60 |
500 | 40 | |
# cpu.max
40_000 100_000
# cpu.stat
usage_usec 200_000
nr_periods 5
nr_throttled 4
throttled_usec 240_000
Throttling Explained. Example#1. Conclusions
Throttling Explained. Example#2
Use-cases variations:
Throttling Explained. Example#2.1: Calculus
Time | run (ms) | throttled (ms) |
100 | 90 | 10 |
200 | 90 | 10 |
300 | 90 | 10 |
400 | 90 | 10 |
500 | 90 | 10 |
600 | 90 | 10 |
700 | 90 | 10 |
800 | 90 | 10 |
900 | 90 | 10 |
1000 | 90 | 10 |
#cpu.max
90_000 100_000
# cpu.stat
usage_usec 900_000
nr_periods 10
nr_throttled 10
throttled_usec 100_000
Throttling Explained. Example#2.2: Calculus
Time | run (ms) | throttled (ms) |
100 | 10 | 90 |
200 | 10 | 90 |
300 | 10 | 90 |
400 | 10 | 90 |
500 | 10 | 90 |
600 | 10 | 90 |
700 | 10 | 90 |
800 | 10 | 90 |
900 | 10 | 90 |
1000 | 10 | 90 |
#cpu.max
10_000 100_000
# cpu.stat
usage_usec 100_000
nr_periods 10
nr_throttled 10
throttled_usec 900_000
Throttling Explained. Example#2. Conclusions
“These two types of throttling are the same—but oh, they're two big differences!”
Throttling Explained. Example#3: Multi-Tasks
Throttling Explained. Example#3: Multi-Tasks
Throttling Explained. Example#3.1.1: Multi-Tasks
Time (ms)
0
100
200
Thread#1 (100ms)
Thread#2 (100ms)
CPU#1
CPU#2
Throttling Explained. Example#3.1.2: Multi-Tasks
Time (ms)
0
100
200
Thread#1 (100ms)
Thread#2 (100ms)
CPU#1
CPU#2
Example#3.2.1: Multi-Tasks – Visual.�10 parallel tasks, each requiring 50ms CPU Time, each one running on different CPU. No CPU Limits.
Time (ms)
0
100
200
50
T#1. (50ms)
T#2. (50ms)
T#3.
(50ms)
…
T#10.
(50ms)
Requests started
Requests completed
CPU#3
CPU#2
CPU#10
CPU#1
Example#3.2.2: Multi-Tasks – Visual.�10 parallel tasks, each requiring 50ms CPU Time, each one running on different CPU. CPU Limits = 2.
Time (ms)
0
100
200
T#1 … (20ms)
T#2 … (20ms)
T#3 … (20ms)
…
T#10 … (20ms)
20
Threads Throttled
Idle for 80ms …
waiting next period
T#1 … (20ms)
T#2 … (20ms)
T#3 … (20ms)
…
T#10 … (20ms)
Threads Throttled
Idle for 80m …
waiting next period
T#1. (10ms)
T#2. (10ms)
T#3.
(10ms)
…
T#10.
(10ms)
Requests started
Requests completed
Quota Exceeded
CPU#3
CPU#2
CPU#10
CPU#1
Ex.3.2.1: 10 parallel Tasks, No CPU Limits. Texts
Ex.3.2.2: 10 parallel Tasks, CPU Quota = 2. Texts
Ex.3.2.2: 10 parallel Tasks, CPU Quota = 2. Calculus
Throttling Explained. Example#4
Throttling Explained. Example#4: Calculus
#cpu.max
190_000 100_000
# cpu.stat
usage_usec 1_900_000
nr_periods 10
nr_throttled 10
throttled_usec 100_000
Time | CPU1 run (ms) | CPU2 run (ms) | throttled (ms) |
100 | 100 | 90 | 10 |
200 | 90 | 100 | 10 |
300 | 100 | 90 | 10 |
400 | 100 | 90 | 10 |
500 | 95 | 95 | 10 |
600 | 100 | 90 | 10 |
700 | 100 | 90 | 10 |
800 | 100 | 90 | 10 |
900 | 100 | 90 | 10 |
1000 | 95 | 95 | 10 |
CPU Throttling
More explanations
Visual: Throttling use-cases
Visual: Throttling, other use-case: �CPU Low usage, High Throttling.
CPU Low usage, High Throttling:
Time �(s) | CPU Usage �(AVG 1s) | Throttled % �Period |
0 | 0.0817 | 30 |
1 | 0.093 | 60 |
2 | 0.0822 | 33 |
3 | 0.126 | 50 |
4 | 0.081 | 20 |
Below, in the table, are simulated samples for CPU usage (time) and Throttling at 1 second resolution:
CPU Low usage, High Throttling:
CPU Usage and Throttled periods per every 100ms CFS Quota Period.
CPU Request, Limits and Throttling
Further thoughts…
CPU Throttling: Impacts on Applications
CPU Usage vs Throttling. Grafana Reports
100 * (rate(container_cpu_cfs_throttled_periods_total{namespace="$namespace", pod="$pod"}[1m]) / rate(container_cpu_cfs_periods_total{namespace="$namespace", pod="$pod"}[1m]))
CPU Usage vs Throttling. Grafana Reports, cont’d
Valuable usage of time-based throttled metrics were proposed by Francesco Fabbrizio at Performance Summit 2021.
CPU Usage vs Throttling. Grafana Reports, cont’d
“Desired CPU” estimation, from the same presentation of Francesco Fabbrizio at Performance Summit 2021.
CPU Request, Limits and Throttling
Final Thoughts,
Best and/or Community Practices,
Recommendations.
Community Opinions: High Overview
This is one of the most debatable, complex and contradictory subject in community. There are 2 popular opposite-opinion popular articles on topic:
vs
Community Opinions: High Overview, Cont’d
Community Opinions: More sources
Grafana Labs highlights the drawbacks of CPU limits on their website, providing several articles that advocate for either removing CPU limits entirely or using them only in specific scenarios.
Community Opinions: more
“We can do many things to save cost, but throttling workloads in a way that hurts their performance and maybe even availability should be our last resort.
… (After Autoscalers introducing)… In this situation, we don’t need Limits – just accurate Requests for/on everything (which ensures the minimum each one requires via CPU shares to function) and the Cluster Autoscaler. This will ensure that no workload is throttled unnecessarily and that we have the right amount of capacity at any given time.”
src: https://sysdig.com/blog/kubernetes-cpu-requests-limits-autoscaling/
“Kubernetes CPU Limit Best Practices: Avoid CPU Limits for Performance: If performance is your goal, avoid setting CPU limits to allow your containers to utilize idle CPU resources. This approach maximizes the use of available resources and improves the overall performance of your applications.”�src: https://www.perfectscale.io/blog/kubernetes-cpu-limit-best-practises
“Disable CPU limits — unless you have a good use case”
src: https://learnk8s.io/production-best-practices
Cockroach Labs on CockroachDB: “Warning: While setting memory limits is strongly recommended, setting CPU limits can hurt tail latencies as currently implemented by Kubernetes. We recommend not setting CPU limits at all unless…”
src: https://www.cockroachlabs.com/docs/stable/kubernetes-performance.html#resource-requests-and-limits
Community Opinions: more+
�“Kubernetes: Make your services faster by removing CPU limits” by Eric Khun from Buffer.com.
Community Opinions: more++
OTOH, my LinkedIn polls…
Community Opinions: more++
And on reddit
CPU Limits. So what?
CPU Limits. Community Recommendations:
CPU Limits. Community Recommendations: Cont’d
Following article https://itnext.io/cpu-limits-and-requests-in-kubernetes-fa9d55948b7c (by Daniele Polencic from learnk8s.io) reflects one of common opinions:
CPU Limits. Community Recommendations: Cont’d
Kubecost (an IBM Company): You can classify applications into different availability tiers and apply these rules of thumb for targeting the appropriate level of availability:
Tier | Request | Limit |
Critical / Highly Available | 99.99th percentile + 100% headroom | 2x request or as higher if resources available |
Production / Non-critical | 99th + 50% headroom | 2x request |
Dev / Experimental | 95th or consider namespace quotas* | 1.5x request or consider namespace quotas* |
This best practice of analyzing historical usage patterns typically provides both a good representation of the future and is easy to understand/introspect. Applying extra headroom allows for fluctuations that may have been missed by your historical sampling. We recommend measuring usage over 1 week at a minimum and setting thresholds based on the specific availability requirements of your pod.
src: https://blog.kubecost.com/blog/requests-and-limits/
Community Opinions++: When to Set CPU Limits
Most common applicable use cases:
Community Opinions++: When to Set CPU Limits
Again Tim Hockin:
“If you want to do it scientifically:
Benchmark your app under a load that represents the high end of reality. If you are preparing for BFCM (Black Friday Cyber Monday), triple that.
For these benchmarks, set CPU request = limit.
Measure the critical indicators. Vary the CPU request (and limit) up or down until the indicators are where you want them (e.g. p95 latency < 100ms).”
Other Best Practices
CPU Request, Limits and Throttling
Inspiration Sources, References
and nice or must-read articles
Inspiration Sources: Intro into the CPU-stuff topics
vs
Sources: Delving into… Must Read
and
Sources: Delving into… Must/Strongly Recommended
A deep diving journey from Dave Chiluk, a Linux Kernel Contributor, exploring CPU Throttling from High Level Overview thought and till Kernel Code, Bugs and Fixes, Recommendations:
Sources: Delving into… Recommended conference
“Understanding and Measuring CPU Throttling in Containerized Environments”.
Sources: Delving into… Must Read
A series of posts by Daniele Polencic (Kubernetes consultant and teacher, learnk8s.io) exploring CPU management in Kubernetes, throttling, and recommendations. The content spans from foundational concepts to Linux Kernel insights, examining impacts on multithreading workloads, with an evolving perspective on CPU Limits:
Sources: Further/Recommended Readings
A “Kubernetes resources under the hood” series, from basics to advanced, from Shon Lev-Ran and Shir Monether (@ wand.cloud):
Sources: Kernel - references and advanced topics
Sources: Advanced topics
Advanced: Kernel CFS BWC “Burst Feature” in K8s
Below are series of references from Alibaba Cloud which implemented the Kernel CPU CFS BWC Feature in their custom K8s.
Advanced/Future Topics: Auto-scalers
Autoscaling Tools Overview
Advanced/Future Topics: Auto-scalers
Integrating Autoscaling Tools
By understanding and combining these tools, teams can create a flexible, efficient, and responsive Kubernetes environment tailored to their specific workload demands.
Closing Thoughts
Placeholder