Quick revision
The short answer
High p99 latency with CPU at 30% means requests are spending time waiting rather than computing, on locks, I/O or pools. To confirm contention, I take several thread dumps a few seconds apart during a spike and look for many threads BLOCKED or parked on the same lock; then I record JFR with jdk.JavaMonitorEnter and jdk.ThreadPark to see which locks cause the longest total waits, and use async-profiler's lock mode for a flame graph of contended locks. The fix is to shorten the critical section, split the lock, or remove the shared state, and then measure again under the same load.
What to remember
In this answer, we reasoned that high p99 with low CPU means waiting, on locks, I/O or pools, and confirmed lock contention with several thread dumps during a spike, counting threads waiting for the same lock address while discounting idle pool workers. We then measured the total waiting per lock with JFR's monitor enter and park events, used async-profiler's lock and wall-clock modes to see both the waiters and what the holder does, fixed the example by moving a remote call out of the critical section and publishing an immutable map, and confirmed the fix by measuring again under the same load.
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.
High p99 latency with CPU at 30% means requests are spending time waiting rather than computing, on locks, I/O or pools. To confirm contention, I take several thread dumps a few seconds apart during a spike and look for many threads BLOCKED or parked on the same lock; then I record JFR with jdk.JavaMonitorEnter and jdk.ThreadPark to see which locks cause the longest total waits, and use async-profiler's lock mode for a flame graph of contended locks. The fix is to shorten the critical section, split the lock, or remove the shared state, and then measure again under the same load.
Why this question is asked
Latency spikes without a resource that looks busy are frustrating, because dashboards show a healthy machine while users wait. They are also a good test of method: the tempting move is to guess and change something, and the right move is to gather evidence that points to one place. Interviewers ask this scenario to see whether we can reason from the symptoms to a class of causes and then confirm with tools. In this answer, we will reason from the symptoms, separate lock contention from its look-alikes, confirm it with thread dumps, find the exact lock with JFR and async-profiler, and finally fix it and prove the fix.
1Reading the Symptoms: Low CPU, High Latency
A request's latency is the time it spends running plus the time it spends waiting. If CPU were the limit, we would see it near 100%, and the fix would be about computation. At 30% CPU the machine has spare capacity, so the extra time in slow requests must be waiting. Spikes at p99 rather than a shift in the median also tell us something: most requests are fine, and a minority hits a queue of some kind.
There are three usual places to wait. Locks, where threads queue behind each other for shared state. I/O, where threads wait on a database, another service or the disk. And pools, where threads wait for a connection, a worker or a permit. We suspect locks, but we confirm before acting, because the tools that find each kind are different.
- Is CPU near its limit?YesProfile CPU insteadNoRequests are waiting
- Take 3 to 5 thread dumps during a spike5 seconds apart
- Many threads BLOCKED or parked on one lock?YesLock contention: find the lock with JFRNoLook for I/O or pool waits
- Record JFR lock events and async-profiler lock modesum wait time by lock and stack
- Fix, then measure again under the same loadp99 and lock wait totals
5 steps in order. Play them, or step through them one at a time.
2Thread Dumps During a Spike
Timing matters: a dump taken while things are calm shows nothing. So I take three to five dumps, about five seconds apart, while the latency is high, either by hand with jcmd <pid> Thread.print -l, or with a small loop triggered by the alert. Then I count threads by state and by the lock they wait for.
Notice how the counts separate real contention from noise. 112 waits on the same RateTable monitor across five dumps is a hot lock: on average more than 20 request threads stuck behind it at any moment. The 410 parks on a LinkedBlockingQueue are idle executor threads waiting for work, which is normal. If the dumps had instead shown many threads in socket reads or in HikariPool.getConnection, the problem would be I/O or a pool, not a lock. Searching for the owner, the thread with '- locked <0x00000006c1a2b3d0>', shows what it does while holding the lock.
3Measure Which Lock Costs the Most With JFR
Dumps tell us that a lock is hot at a few instants; JFR tells us how much waiting each lock causes over minutes. I record for five minutes covering a few spikes, with the monitor enter and park thresholds lowered to a few milliseconds, and then sum the durations by monitor class and stack.
So in five minutes, request threads spent 412 seconds in total waiting for the RateTable monitor, and the longest single wait was almost two seconds, which on its own explains a p99 spike. The events' stack traces show which method takes the lock, and the previousOwner field points us at what the holder was doing. Adding jdk.ThreadPark covers ReentrantLock and similar, in case the hot lock is not a monitor.
4A Flame Graph With async-profiler
For a second view I run async-profiler in lock mode for a minute during the spikes. It samples every contended acquisition and weighs it by waiting time, so the flame graph's widest tower shows where the waiting comes from, including short waits below JFR's threshold. Running it also in wall-clock mode shows the holder's side: what code runs inside the lock. In our example, the holder of RateTable spends most of its time in reload(), which reads rates from a remote service every few seconds while holding the monitor.
5Fix the Critical Section and Measure Again
Once we know the lock and what happens inside it, the fix follows one of three patterns. Shorten the critical section, by doing slow work such as remote calls, I/O or logging outside the lock. Split the lock, so unrelated data does not share one lock, for example with ConcurrentHashMap instead of a synchronized map, or lock striping by key. Or remove the shared mutable state, for example by building a new immutable snapshot and swapping a reference.
Then we measure again under the same load, ideally with the same load test that reproduced the spikes. The proof is twofold: p99 drops back near the median, and the JFR totals for RateTable fall from hundreds of seconds to near zero. If p99 improves only partly, the next lock or wait in the ranking is the next thing to look at.
6Follow-up questions
How do you tell lock contention from GC pauses, which also cause spikes with modest CPU?
GC pauses stop all threads at once, so latency spikes line up with pause events in GC logs or JFR's GC events, and thread dumps taken during a pause cannot be taken until it ends. Lock contention affects only the threads using that lock, and dumps show them BLOCKED or parked. Checking the GC log for long pauses at the spike times is a quick first step to rule it out.
Could the lock be inside a library rather than our code?
Often it is: a logging appender writing synchronously, a connection pool, a metrics registry, or a cache library. The same tools apply, since the monitor class and stack name the library. The fix might be configuration, such as an asynchronous appender, or a library upgrade, rather than a code change.
7Conclusion
In this answer, we reasoned that high p99 with low CPU means waiting, on locks, I/O or pools, and confirmed lock contention with several thread dumps during a spike, counting threads waiting for the same lock address while discounting idle pool workers. We then measured the total waiting per lock with JFR's monitor enter and park events, used async-profiler's lock and wall-clock modes to see both the waiters and what the holder does, fixed the example by moving a remote call out of the critical section and publishing an immutable map, and confirmed the fix by measuring again under the same load.
Open with
Low CPU with high p99 tells me the slow requests are waiting, not computing, so the candidates are locks, I/O and pools. To confirm contention I would take three to five thread dumps a few seconds apart during a spike with jcmd Thread.print -l and count threads by state and by the lock address they wait on.
Then, if they want more
If a hundred threads across the dumps are waiting to lock the same monitor, or parked on the same ReentrantLock, that is contention, and I can find the owner by searching for the locked line with that address. I would ignore threads parked on their executor queue, since those are just idle.
If the dumps instead show threads in socket reads or in getConnection, I would switch to I/O or the pool.
To locate the lock precisely and measure its cost, I would record JFR for a few minutes with lower thresholds for jdk.JavaMonitorEnter and jdk.ThreadPark, sum the waiting time by lock class and stack, and look at the previous owner to see what the holder does. async-profiler in lock mode gives a flame graph of contended acquisitions, and wall-clock mode shows the code inside the lock.
Typical fixes are moving slow work like remote calls or logging out of the critical section, splitting the lock or using a concurrent structure, or replacing shared mutable state with an immutable snapshot behind a volatile reference. Then I would rerun the same load and check that both p99 and the lock wait totals drop.
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.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 service shows p99 latency spikes while CPU usage stays at 30%. You suspect lock...": https://scrivio-interview-library.netlify.app/java/concurrency-problems/0abdf9e0aa/