Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

After a deployment, a Spring Boot service's CPU goes to 100 percent on all pods and stays there, and requests time out. How do you find the cause quickly?

If the deployment is the likely cause, I roll back first to restore service, after grabbing a little evidence from one pod. Then I find which threads burn the CPU, either with top -H matched to a thread dump or with a short async-profiler CPU profile, and check whether the GC threads are the busy ones, which would mean a heap near its limit. Typical causes introduced by a change are an infinite loop, a regular expression with catastrophic backtracking, or excessive logging, and comparing with a pod still running the previous version confirms it quickly.

Why this question is asked

A service pinned at 100 percent CPU on every pod is an emergency: requests time out, autoscaling adds pods that immediately saturate too, and health checks may start failing and restarting pods in a loop. Interviewers ask this to see whether we put recovery before curiosity, and whether we know how to go from a busy process to the exact line of code burning the CPU. In this answer, we will decide when to roll back, capture evidence, map hot operating-system threads to Java stack traces, use a profiler, check the GC, and look at the usual culprits with an example.

1Mitigate First: Roll Back

The timing tells us a lot. If CPU jumped right after a deployment and stayed there on all pods, the new version is the prime suspect, and the fastest way to stop customer impact is to roll back to the previous version. Debugging in production while checkout times out costs real money every minute.

But a rollback destroys the evidence on the pods it replaces. So before or during the rollback, I keep one bad pod around if the platform allows it, for example by removing its label so the Service stops sending it traffic, and collect thread dumps and a short profile from it. If that is not possible, a quick capture of two thread dumps and top output from one pod takes less than a minute and is worth it.

Text
# Keep one bad pod for investigation, out of traffic (label names depend on your setup)
kubectl label pod orders-7d9f-abcde app- --overwrite # Service selector no longer matches
kubectl rollout undo deployment/orders # restore the previous version

2Finding the Threads That Burn the CPU

A Java process has dozens of threads, and top shows only the process total. With top -H we see each operating-system thread separately. Each Java thread in a thread dump carries the same id in hexadecimal as nid, so we convert the decimal id from top and search for it in the dump.

YAML
top -H -b -n 1 -p 1 | head -12
# PID USER %CPU COMMAND
# 87 app 98.7 http-nio-8080-e
# 91 app 97.9 http-nio-8080-e
# 23 app 2.1 GC Thread#0
printf '%x\n' 87 # 57
jcmd 1 Thread.print > /tmp/td.txt
grep -A8 'nid=0x57 ' /tmp/td.txt
# "http-nio-8080-exec-3" ... nid=0x57 runnable
# at java.util.regex.Pattern$Loop.match(...)
# at java.util.regex.Pattern$GroupTail.match(...)
# at java.util.regex.Matcher.matches(...)
# at com.shop.orders.CouponValidator.isValid(CouponValidator.java:24)

Notice that the busy threads are request workers, not GC threads, and they are inside regex matching called from our own CouponValidator. Two or three dumps a few seconds apart showing the same frames make that conclusion solid.

3Using a Profiler for a Clearer Picture

Thread dumps are snapshots, so they can mislead when the CPU is spread across many code paths. A sampling profiler takes thousands of stack samples and shows where time actually goes. async-profiler is a common choice because it has low overhead and can attach to a running JVM; Java Flight Recorder, built into the JDK, works too.

Text
# 30-second CPU profile written as a flame graph
asprof -d 30 -e cpu -f /tmp/cpu.html 1
# Or with the JDK's own recorder
jcmd 1 JFR.start duration=30s filename=/tmp/cpu.jfr settings=profile

In a flame graph, the widest boxes are where most samples landed. If 85 percent of samples sit under CouponValidator.isValid, the case is closed. If instead most samples are in logging appenders, string formatting or a JSON serializer, the change probably added expensive work on every request.

4Checking the Garbage Collector

High CPU does not always come from our code. If the heap is close to its limit, the garbage collector runs back to back, each cycle freeing very little, and the GC threads (named like GC Thread#0 or G1 Conc#0) dominate top -H. The application barely runs, so requests time out. A new version can cause this by loading much more data per request or by a memory leak.

GC logs or JVM metrics confirm it: collections happening every second, heap after GC near the maximum, and GC time taking most of the wall clock. In that case the next step is the memory investigation, a heap dump and its dominator tree, rather than CPU profiling.

Step throughFrom 100 percent CPU to the cause
  1. Started with a deployment?
    YesKeep one pod aside, roll backNoLook at traffic and dependencies
  2. Run top -H, take thread dumpsMatch busy thread ids to nid values
  3. Are GC threads the busy ones?
    YesHeap near limit: analyse memoryNoLook at application frames
  4. Profile for 30 sasync-profiler or JFR flame graph
  5. Compare with the previous versionDiff the code behind the hot frames

5 steps in order. Play them, or step through them one at a time.

Recover first, then narrow the search with evidence from the kept pod.

5The Usual Culprits and an Example Fix

Changes that pin the CPU tend to be of a few kinds. An infinite loop, for example a while loop whose exit condition depends on data that never changes, keeps a thread at 100 percent forever. Catastrophic backtracking happens when a regular expression with nested repetition, such as (a+)+ or (\w+\s?)+, meets an input that almost matches: the regex engine tries an exponential number of ways to split the input. Excessive logging, such as logging a full request body at INFO inside a loop, burns CPU in formatting and I/O.

Here is the regex case from our dump.

Java
// Broken: nested quantifier, exponential on near-matches
private static final Pattern CODE = Pattern.compile("^([A-Z0-9]+-?)+$");
boolean isValid(String c) { return CODE.matcher(c).matches(); }
isValid("SUMMER-2026"); // true, instant
isValid("AAAAAAAAAAAAAAAAAAAAAAAAAAAA!"); // false, but takes seconds; longer inputs take minutes
// Fixed: one way to match each input, plus a length limit
private static final Pattern CODE = Pattern.compile("^[A-Z0-9]+(?:-[A-Z0-9]+)*$");
boolean isValid(String c) { return c.length() <= 40 && CODE.matcher(c).matches(); }
isValid("AAAAAAAAAAAAAAAAAAAAAAAAAAAA!"); // false, instant

The reason the first pattern explodes is that a run of letters can be split between iterations of the outer group in a huge number of ways, and the engine tries them all before giving up. The fixed pattern allows exactly one way to match each input. Finally, comparing with a pod still on the previous version, if one is running, confirms the culprit: the old version's profile will not have the hot frame at all.

6Follow-up questions

Why does autoscaling not help here?

Because each new pod runs the same faulty code and saturates as soon as it receives traffic. Scaling out just multiplies the cost and can overload shared dependencies. Autoscaling helps when load is the problem, not when every request is expensive because of a bug.

What if there was no deployment?

Then I look at what else changed: a traffic pattern such as a client sending unusual input, a configuration change, a dependency that started returning larger responses, or a data change that made a code path slower. The same tools, top -H with thread dumps and a profiler, still show where the CPU goes.

How would you prevent this class of problem?

Canary deployments with automated analysis would have caught a CPU jump on a small share of traffic. For regex, we can review patterns for nested quantifiers, limit input lengths and add tests with adversarial inputs. Load tests on the release candidate also show CPU per request rising before production does.

7Conclusion

In this answer, we saw that when CPU pins after a deployment we roll back first, keeping one pod or a few dumps as evidence. We map the busiest threads from top -H to the thread dump through their nid values, or use async-profiler or JFR for a flame graph, and we check whether GC threads are the busy ones because a heap near its limit looks like a CPU problem. The usual culprits are infinite loops, catastrophic regex backtracking and excessive logging, and comparing with the previous version confirms the cause.

What to say in the interviewAbout 90 seconds aloud

Open with

Since it started with the deployment and every pod is affected, my first move is to restore service by rolling back. Before the rollback finishes, I keep one bad pod out of traffic if I can, or at least grab two thread dumps and top output from one pod, because the rollback destroys the evidence.

Then, if they want more

Then I find which threads use the CPU. I run top -H on the Java process to see per-thread CPU, convert the busiest thread ids to hex, and find the matching nid in a thread dump from jcmd Thread.print. That gives me the stack trace of the hot threads.

If it is not clear from a few dumps, I take a 30-second async-profiler or JFR CPU profile and look at the flame graph. I also check whether the busy threads are GC threads, because a heap close to its limit causes back-to-back collections that look like a CPU problem, and then I would switch to memory analysis.

In application code, the usual suspects after a change are an infinite loop, a regular expression with nested quantifiers that backtracks catastrophically on certain input, or heavy logging added in a hot path. Comparing with a pod on the previous version, if one is still around, quickly confirms which frame is new.

After the fix, I would add a canary step so a CPU jump like this is caught on a small slice of traffic.

About this answer

Last updated
12 November 2026
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.
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 "After a deployment, a Spring Boot service's CPU goes to 100 percent on all pods and...": https://scrivio-interview-library.netlify.app/spring-boot/production-operations/807786abeb/