Why is the container slow when the server is almost idle?
cgroups and CPU throttling
A CPU limit is a time budget per window. Spend it early with parallel workers and the container waits, even with free cores.
docker run --cpus=0.5 does not hand you half a physical core. Docker sets a cgroup limit: by default the container gets 50 ms of CPU time in every 100 ms window. It is a ceiling, not a guarantee. This is different from --cpuset-cpus, which chooses which CPUs may run the container. Limiting time and pinning to CPUs are two separate decisions.
When the budget runs out
Processes that want to keep running are throttled until the budget is renewed. That happens even if other CPUs are idle. The kernel enforces the limit you set; it is not impressed that the machine looks bored.
| Load in the container | Budget burn rate | 50 ms of CPU gone after | |---|---|---| | one busy thread | 1 ms CPU per 1 ms wall | about 50 ms | | two busy threads | 2 ms CPU per 1 ms wall | about 25 ms |
More parallelism under the same quota can make latency worse, because it exhausts the budget sooner.
What it looks like behind an API
A burst of CPU-heavy work uses the quota early, and a small request that arrives during the wait gets slow too. Low average CPU for the whole host does not rule out a bottleneck at one container's quota. When testing performance, compare latency, the quota and throttling counters over the same time window. Throttling alone does not prove it is the cause of all slowness.
Try it
On Docker with cgroup v2:
Expect 50000 100000 in cpu.max (microseconds). In cpu.stat, watch nrthrottled and throttledusec rise. They are cumulative, so compare before and after.
The trade-off
A quota protects neighbours from a noisy service, at the cost of response time during bursts. Raising it helps when the quota really is the problem. It will not fix expensive code or a real shortage of CPU.
Check yourself
One CPU of quota, 100 ms window, four workers on four free cores: the budget is gone after about 25 ms, and adding workers does not add budget.