Quick revision
The short answer
The pod is being CPU throttled. With a limit of 1 CPU, the kernel gives the container 100 ms of CPU time every 100 ms, and request threads, GC threads and JIT compiler threads together use that up early in a busy period, so every thread then waits for the next period. On top of that, the JVM sees one CPU and picks the Serial collector, which stops all threads for each collection. We fix it by raising or removing the CPU limit, setting requests to real usage, choosing the GC and processor count explicitly, and proving it with throttling metrics and JFR.
What to remember
In this answer, we saw that a CPU limit is a quota per 100 ms window, that a multi-threaded JVM spends it in bursts and then waits for the rest of the window, and that a JVM seeing one CPU picks the Serial collector and stops everything to collect. We fixed it by setting requests to measured usage, removing or raising the CPU limit, choosing G1 and the processor count explicitly, and confirming with the throttled-periods ratio and a JFR recording.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The pod is being CPU throttled. With a limit of 1 CPU, the kernel gives the container 100 ms of CPU time every 100 ms, and request threads, GC threads and JIT compiler threads together use that up early in a busy period, so every thread then waits for the next period. On top of that, the JVM sees one CPU and picks the Serial collector, which stops all threads for each collection. We fix it by raising or removing the CPU limit, setting requests to real usage, choosing the GC and processor count explicitly, and proving it with throttling metrics and JFR.
Why this question is asked
This scenario comes up all the time when a Java service moves from virtual machines to Kubernetes. The service was fast on a VM with four cores, the GC logs look healthy, and yet p99 latency jumps every few seconds. Interviewers ask it because the cause is not in the application code at all: it sits in the meeting point between the Linux scheduler and JVM ergonomics. In this answer, we will recap how CPU requests and limits work, see how throttling produces the spikes, look at the GC the JVM picks on one CPU, and then go through the changes and how to confirm them.
1A Quick Recap of CPU Requests and Limits
A pod's CPU request is what the scheduler reserves for it when placing it on a node. A request of 0.5 means the node must have half a core free. At runtime the request also becomes a weight: when the node is busy, containers share CPU in proportion to their requests.
The CPU limit is different. It is enforced by the Linux Completely Fair Scheduler (CFS) as a quota per period. The default period is 100 ms, so a limit of 1 CPU means the container may use 100 ms of CPU time in each 100 ms window, summed across all its threads. If eight threads run in parallel, they burn through 100 ms of quota in about 12.5 ms of wall-clock time. After that the kernel throttles the whole container: no thread runs until the next period starts, roughly 87 ms later.
So a limit is not a speed. It is a budget per window, and a multi-threaded process can spend it in a burst.
2How Throttling Turns Into p99 Spikes
A Spring Boot API is never one thread. Tomcat has up to 200 request threads, the JIT compiler runs its own threads that compile hot methods, and the garbage collector has worker threads. When a burst of requests arrives, request threads wake up, allocation rises, a GC starts, and the JIT may be compiling at the same moment. Together they exhaust the 100 ms quota in a fraction of the period.
Then every thread, including the one about to write a response, is frozen until the next period. A request that needed 5 ms of CPU now takes 5 ms plus up to 80 or 90 ms of waiting. If this happens in a few periods in a row, the delay stacks. That is the p99 spike. Notice that the GC logs only report how long the collector itself ran, so they show short pauses, while the requests saw much longer stalls. The gap between the GC pause and the latency spike is the clue that something outside the JVM was holding the threads.
7 messages in order. Play them, or step through them one at a time.
The container never ran out of real CPU on the node. It ran out of its quota for the window.
The reason it repeats every few seconds is that the triggers repeat: young collections happen at a steady rhythm under steady allocation, and bursts of traffic or periodic jobs line up with them.
3Why the JVM Picks the Serial Collector on One CPU
Since JDK 10 the JVM is container aware. It reads the cgroup limits and uses them for its own sizing. In current JDKs the active processor count comes from the CPU limit (the quota), not from the request, so a limit of 1 means the JVM believes it has exactly one processor.
JVM ergonomics then decides whether this is a server-class machine. If it sees fewer than 2 processors or less than about 1792 MB of memory, it does not choose G1. It chooses the Serial collector, which uses a single thread and stops every application thread for each collection.
So two effects stack on each other. Serial GC stops everything for each young collection, and throttling can stretch that stop further when the collection spends the quota.
4What We Would Change
The first change is the limit itself. For a latency-sensitive service, a CPU limit of 1 with a request of 0.5 is the worst combination: the scheduler places it as if it needs half a core, and the kernel cuts it off whenever it briefly needs more than one. Many teams remove the CPU limit for such services and set the request to the real steady usage, for example 1 or 1.5 CPU measured at p95. Without a limit, the container can borrow idle CPU on the node during bursts, and the request still protects its fair share. If the platform requires limits, raise the limit well above the request, for example request 1 and limit 3.
The second change is to stop relying on ergonomics. We set the collector and processor count explicitly, so the JVM behaves the same whatever the limit is.
Notice that the memory limit stays. Memory is not compressible: going over it means an OOM kill, so a memory limit equal to the request is the safe default. CPU is compressible, so a missing CPU limit only means the container may use spare capacity. ActiveProcessorCount=2 makes the JVM size its GC, compiler and common pool threads for two CPUs; we keep it modest so the JVM does not create more threads than the pod can really run.
5Confirming the Diagnosis Before and After
We do not change limits on a hunch. The kernel counts throttling per container, and cAdvisor exposes it to Prometheus as container_cpu_cfs_throttled_periods_total and container_cpu_cfs_periods_total. The ratio of the two tells us what share of periods were throttled. Anything above a few percent on a latency-sensitive service is a problem, and the spikes in the ratio should line up in time with the p99 spikes.
Inside the JVM, a JFR recording shows the other half. jcmd 1 JFR.start duration=120s filename=/tmp/api.jfr records GC events, CPU load and thread states. In JDK Mission Control we see short GC pauses next to request threads that were runnable but not running, with CPU load pinned at one core during bursts.
After the change we compare the same three signals: throttled ratio near zero, G1 in the GC log, and p99 latency back in line with p50.
6Follow-up questions
Does removing the CPU limit hurt other pods on the node?
Not their guaranteed share. CPU requests turn into scheduler weights, so when the node is busy every container still gets CPU in proportion to its request. A container without a limit only takes CPU that would otherwise sit idle. The real risk is that requests are set too low, so we set them from measured usage.
Could the same symptom come from something else?
Yes. Noisy neighbours, a slow downstream call, safepoint pauses for reasons other than GC, or a full stop for a heap dump can all cause periodic spikes. That is why we confirm with the throttling metrics and JFR before changing anything, rather than assuming throttling.
7Conclusion
In this answer, we saw that a CPU limit is a quota per 100 ms window, that a multi-threaded JVM spends it in bursts and then waits for the rest of the window, and that a JVM seeing one CPU picks the Serial collector and stops everything to collect. We fixed it by setting requests to measured usage, removing or raising the CPU limit, choosing G1 and the processor count explicitly, and confirming with the throttled-periods ratio and a JFR recording.
Open with
My first suspect here is CPU throttling, not the application. A limit of 1 CPU is enforced as 100 milliseconds of CPU time per 100 millisecond period across all threads.
Then, if they want more
A Spring Boot service has request threads, JIT compiler threads and GC threads, and during a burst they can use the whole quota in 20 milliseconds, after which every thread is frozen until the next period. That is why the GC logs show short pauses but requests see much longer stalls.
A second effect makes it worse: the JVM reads the limit, believes it has one CPU, and with fewer than two CPUs ergonomics picks the Serial collector, which stops all threads for each collection.
I would confirm it first, by looking at container_cpu_cfs_throttled_periods_total against total periods in Prometheus and checking that throttling lines up with the p99 spikes, and by taking a JFR recording to see GC events next to threads that were runnable but not running.
Then I would set the CPU request to the real p95 usage, say 1.5 cores, remove the CPU limit or raise it well above the request, keep the memory limit equal to the request, and set -XX:+UseG1GC and -XX:ActiveProcessorCount=2 so the JVM no longer depends on ergonomics.
After the change I would check that the throttled ratio is near zero and p99 is back close to p50.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 10
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Kubernetes documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A latency-sensitive Spring Boot API has p99 spikes every few seconds. The pod has a CPU...": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/8dea3aecf6/