When fixing CFS tail latency spikes, remove tight fractional CPU limits and size pools from /sys/fs/cgroup/cpu.max.
TL;DR30-second simplified summary▾
Zero nr_throttled + 14ms flat p99.
The Multi-Threaded Quota Burn Trap.
16 parallel threads x 3.2ms = 51.2ms CPU time burned in 3.2ms wall time, triggering a 96.8ms kernel freeze.
Cgroup-Aware Thread Pool Sizing.
Parse /sys/fs/cgroup/cpu.max in Python or set -XX:ActiveProcessorCount in Java instead of trusting host core counts.
def cgroup_cpu_quota(default: int = 2) -> int:
try:
with open('/sys/fs/cgroup/cpu.max') as f:
q, p = f.read().split()
return max(1, int(q) // int(p)) if q != 'max' else default
except OSError:
return default
CPU Requests vs. Limits Trade-Off.
Keep CPU requests accurate for bin-packing and HPA, while removing fractional CPU limits (or using Guaranteed integer-core pods). [INSERT YOUR BENCHMARK / PROD NOTE] for p99 reduction.
- Throttled Periods: 45%-80%
- P99 Latency: 450-800 ms
- Throttled Periods: 0%
- P99 Latency: 14 ms
- Problem
- Latency-sensitive multi-threaded microservices in Kubernetes suffer 100ms-quantized p99 latency spikes under fractional CPU limits even when average CPU utilization is low.
- Naive Solution
- Keep tight fractional CPU limits (such as 500m) with default host-core-sized thread pools and monitor 30-second average CPU utilization.
- Why It Fails
- All threads in the cgroup share a single 100ms CFS quota; 16 parallel threads exhaust a 50ms quota in 3.2ms of wall time and freeze for the remaining 96.8ms, which 15-60s Prometheus averages hide.
- Alternatives
- Size thread pools from /sys/fs/cgroup/cpu.max or -XX:ActiveProcessorCount, remove fractional CPU limits while keeping accurate CPU requests, or use Guaranteed QoS pods with integer cores.
- Engineering Decision
- Keep accurate CPU requests for bin-packing and HPA, remove fractional CPU limits on latency-sensitive microservices (or use Guaranteed integer-core pods), and bound thread pools to cgroup quota.
- Trade-Offs
- Removing CPU limits allows pods to burst onto idle node CPU (mitigated by accurate requests and node autoscaling), whereas best-effort batch jobs still need strict CPU limits to prevent noisy-neighbor contention.
Why does a pod show 30% average CPU on Prometheus while still experiencing 800ms CFS throttling stalls?
| Choice | What it buys | What it costs |
|---|---|---|
| 500m limit + 16 threads | Strict noisy-neighbor CPU cap on node | 45%-80% throttled periods 450-800 ms P99 latency |
| Unthrottled / Quota-matched | 0% throttled periods 14 ms P99 latency | Requires accurate CPU requests and pod sizing |
Practice: Make the trade-off yourself.
If a pod has cpu: 500m (50ms per 100ms) and 10 threads run simultaneously, how fast does it hit CFS throttling?